一、新建普通maven项目
1、首先,新建3个普通maven商城项目,模拟以往常见的Java单体应用开发,mall-interface是存放接口和公共代码部分,order-service-consumer和user-service-provider的pom依赖于mall-interface。
2、在order-service-consumer和user-service-provider中分别实现接口,编写各自的实现类
以往,如果order-service-consumer和user-service-provider有相互调用,一般需要当作一个模块引用,部署到服务器的时候需要部署全部模块,分布部署多个服务器的话就很麻烦,并且大的项目模块多的放在一起不方便开发和管理。
那么,把项目中各个模块分开部署那不就好了?但是如果直接把order-service-consumer 和user-service-provider分开部署不同服务器上,显然他们不能相互调用业务接口。这时Dubbo作为RPC框架,它的用处显现出来了。简单的说,Dubbo可以远程调用部署在不同服务器上的业务接口。
通过Dubbo改造,即使order-service-consumer 和user-service-provider部署不同机器,两个模块可以像调用本地接口一样调用远程服务。
二、通过Dubbo改造普通项目
1、改造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">
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="user-service-provider"></dubbo:application>
<!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
<!-- 4、暴露服务 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,启动服务发布注册服务
发布服务之前需要启动 Zookeeper
从Dubbo 服务监控中可以看见服务发布成功
2、改造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调用远程服务成功