Dubbo 改造普通单体项目

1、新建普通maven项目

一、首先,新建3个普通maven商城项目,模拟以往常见的Java单体应用开发,mall-interface是存放接口和公共代码部分,order-service-consumer和user-service-provider的pom依赖于mall-interface。spring

 

二、在order-service-consumer和user-service-provider中分别实现接口,编写各自的实现类apache

 

 

 

以往,若是order-service-consumer和user-service-provider有相互调用,通常须要看成一个模块引用,部署到服务器的时候须要部署所有模块,分布部署多个服务器的话就很麻烦,而且大的项目模块多的放在一块儿不方便开发和管理。服务器

那么,把项目中各个模块分开部署那不就行了?可是若是直接把order-service-consumer 和user-service-provider分开部署不一样服务器上,显然他们不能相互调用业务接口。这时Dubbo做为RPC框架,它的用处显现出来了。简单的说,Dubbo能够远程调用部署在不一样服务器上的业务接口。app

经过Dubbo改造,即便order-service-consumer 和user-service-provider部署不一样机器,两个模块能够像调用本地接口同样调用远程服务。框架

 

2、经过Dubbo改造普通项目

一、改造user-service-provider项目,经过Dubbo发布服务

1.1 在pom.xml中引入Duoob依赖

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xg.xmall.dubbo</groupId>
    <artifactId>user-service-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.xg.xmall</groupId>
            <artifactId>mall-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 引入dubbo -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>
        <!-- 注册中心使用的是zookeeper,引入操做zookeeper的客户端端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

 

2.2 在资源文件夹新建Dubbo配置文件 provider.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 一、指定当前服务/应用的名字(一样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="user-service-provider"></dubbo:application>

    <!-- 二、指定注册中心的位置 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>

    <!-- 三、指定通讯规则(通讯协议?通讯端口) -->
    <dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>

    <!-- 四、暴露服务 ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="com.xg.xmall.dubbo.service.UserService" ref="userServiceImpl" timeout="1000" version="1.0.0">
        <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
    </dubbo:service>

    <!-- 服务的实现 -->
    <bean id="userServiceImpl" class="com.xg.xmall.dubbo.service.impl.UserServiceImpl"></bean>



</beans>

 

 

 

 

2.3 编写Dubbo服务启动类 MainProviderApplication,启动服务发布注册服务

 

发布服务以前须要启动 Zookeepermaven

 

经过Dubbo发布服务 idclook.net

 

从Dubbo 服务监控中能够看见服务发布成功ide

经过Dubbo发布服务 www.idclook.net

 

二、改造order-service-consumer项目,经过Dubbo访问服务

1.1 一样在项目下的pom.xml中引入Duoob依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xg.xmall.dubbo</groupId>
    <artifactId>user-service-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.xg.xmall</groupId>
            <artifactId>mall-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 引入dubbo -->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>
        <!-- 注册中心使用的是zookeeper,引入操做zookeeper的客户端端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2.2 在资源文件夹新建Dubbo配置文件 consumer.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 扫描组件 -->
    <context:component-scan base-package="com.xg.xmall.dubbo.service.impl"></context:component-scan>


    <dubbo:application name="order-service-consumer"></dubbo:application>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>

    <!--声明须要调用的远程服务的接口,生成远程服务代理 -->
    <dubbo:reference interface="com.xg.xmall.dubbo.service.UserService" id="userService" timeout="5000" retries="3" version="*">
    </dubbo:reference>



    <dubbo:monitor protocol="registry"></dubbo:monitor>

</beans>

 

 

 

 

 

 

2.3 编写Dubbo服务启动类 MainConsumerApplication,启动获取服务

 

 

order-service-consumer并无存放用户信息实现类,只是注入服务的接口,就能够获取其余项目提供的用户信息。可见,经过Dubbo调用远程服务成功ui

相关文章
相关标签/搜索