dubbo(提供者、消费者)基于java的实现

2019-09-29 11:08:59 浏览数 (1)

1、安装好jdk、zookeeper以后可以尝试开发代码进行dubbo的学习和练习。

首先创建Dubbo的Provider配置。创建一个maven project工程。

RPC框架,不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consumer依赖这个项目时,就会知道实现类具体实现。

所以Provider和Consumer创建不同的项目。

然后接口项目包里面创建一个接口,并提供一个简单的接口方法。

2、然后创建Dubbo的Provider配置。创建一个maven project工程。

为了可以引用dubbo-service项目包,需要将dubbo-service的项目坐标加进来。

配置maven的pom.xml文件。

代码语言:javascript复制
 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 4     http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6     <groupId>com.bie</groupId>
 7     <artifactId>dubbo-service-impl</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9 
10     <!-- 为了可以引用dubbo-service项目包,需要将dubbo-service的项目坐标加进来 -->
11     <dependencies>
12         <dependency>
13             <groupId>com.bie</groupId>
14             <artifactId>dubbo-service</artifactId>
15             <version>0.0.1-SNAPSHOT</version>
16         </dependency>
17         <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
18         <!-- dubbo -->
19         <dependency>
20             <groupId>com.alibaba</groupId>
21             <artifactId>dubbo</artifactId>
22             <version>2.5.3</version>
23             <!-- 排除dubbot2.5.3版本的spring2.5.6版本,依赖dubbo,去掉老版本spring -->
24             <exclusions>
25                 <exclusion>
26                     <groupId>org.springframework</groupId>
27                     <artifactId>spring</artifactId>
28                 </exclusion>
29             </exclusions>
30         </dependency>
31         <!-- spring,依赖新版本spring -->
32         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
33         <dependency>
34             <groupId>org.springframework</groupId>
35             <artifactId>spring-webmvc</artifactId>
36             <version>4.1.6.RELEASE</version>
37         </dependency>
38         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
39         <dependency>
40             <groupId>org.springframework</groupId>
41             <artifactId>spring-context</artifactId>
42             <version>4.1.6.RELEASE</version>
43         </dependency>
44         <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
45         <dependency>
46             <groupId>org.springframework</groupId>
47             <artifactId>spring-core</artifactId>
48             <version>4.1.6.RELEASE</version>
49         </dependency>
50         <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
51         <dependency>
52             <groupId>org.springframework</groupId>
53             <artifactId>spring-web</artifactId>
54             <version>4.1.6.RELEASE</version>
55         </dependency>
56         <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
57         <dependency>
58             <groupId>org.springframework</groupId>
59             <artifactId>spring-beans</artifactId>
60             <version>4.1.6.RELEASE</version>
61         </dependency>
62         <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
63         <dependency>
64             <groupId>org.springframework</groupId>
65             <artifactId>spring-aop</artifactId>
66             <version>4.1.6.RELEASE</version>
67         </dependency>
68         <!-- <dependency>
69             <groupId>org.apache.zookeeper</groupId>
70             <artifactId>zookeeper</artifactId>
71             <version>3.4.7</version>
72         </dependency>
73         <dependency>
74             <groupId>com.github.sgroschupf</groupId>
75             <artifactId>zkclient</artifactId>
76             <version>0.1</version>
77         </dependency> -->
78         <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
79         <!-- 访问zookeeper客户端jar包,依赖zookeeper客户端工具zkClient -->
80         <dependency>
81             <groupId>com.101tec</groupId>
82             <artifactId>zkclient</artifactId>
83             <version>0.10</version>
84         </dependency>
85         
86     </dependencies>
87 
88 </project>

创建实现接口的实现类:

代码语言:javascript复制
 1 package com.bie.impl;
 2 
 3 import com.bie.service.UserService;
 4 
 5 /**
 6  * 
 7  * @author biehl
 8  *
 9  *         UserServiceImpl实现UserService接口
10  */
11 public class UserServiceImpl implements UserService {
12 
13     public String showUser(String name) {
14         String str = "您好,您的姓名是:"   name;
15         return str;
16     }
17 
18 }

配置applicationContext-dubbo.xml配置文件。

代码语言:javascript复制
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7     http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context 
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://code.alibabatech.com/schema/dubbo 
11         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
12 
13     <!-- 1、首先给当前的provider起一个名称,方便监控中心进行监控,给provider起名,在monitor或管理工具中区别是哪个provider -->
14     <dubbo:application name="dubbo-service-impl" />
15     <!-- 2、配置注册中心,protocol="zookeeper"代表了是dubbo协议,protocol使用哪种注册中心,地址是192.168.110.140:2181,address注册中心的ip和端口,配置注册中心 -->
16     <dubbo:registry address="192.168.110.140:2181"
17         protocol="zookeeper"></dubbo:registry>
18     <!-- 3、配置端口,name="dubbo"代表从注册中心获取到值,port="20880"是consumer访问provider使用的。配置协议,name 使用什么协议, port: consumer invoke provider时的端口号 -->
19     <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
20     <!-- 4、注册功能,注册是注册接口,不是注册实现类的,ref=""代表的是实现类的bean。注册接口, ref 引用接口实现类<bean>的id值 -->
21     <dubbo:service interface="com.bie.service.UserService"
22         ref="userServiceImpl"></dubbo:service>
23 
24     <!-- 注册UserService的bean对象 -->
25     <bean id="userServiceImpl" class="com.bie.impl.UserServiceImpl"></bean>
26 
27 </beans>        

测试类如下所示:

3、然后创建Dubbo的Consumer配置。创建一个maven project工程。

将提供者的pom文件直接拷贝过来就可以直接使用的。因为是war包,自己在webapp下面创建WEB-INF文件夹,然后在WEB-INF下面创建web.xml即可。

代码语言:javascript复制
 1 <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">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>com.bie</groupId>
 4   <artifactId>dubbo-consumer</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   <packaging>war</packaging>
 7   
 8   <!-- 为了可以引用dubbo-service项目包,需要将dubbo-service的项目坐标加进来 -->
 9     <dependencies>
10         <dependency>
11             <groupId>com.bie</groupId>
12             <artifactId>dubbo-service</artifactId>
13             <version>0.0.1-SNAPSHOT</version>
14         </dependency>
15         <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
16         <!-- dubbo -->
17         <dependency>
18             <groupId>com.alibaba</groupId>
19             <artifactId>dubbo</artifactId>
20             <version>2.5.3</version>
21             <!-- 排除dubbot2.5.3版本的spring2.5.6版本 -->
22             <exclusions>
23                 <exclusion>
24                     <groupId>org.springframework</groupId>
25                     <artifactId>spring</artifactId>
26                 </exclusion>
27             </exclusions>
28         </dependency>
29         <!-- spring -->
30         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
31         <dependency>
32             <groupId>org.springframework</groupId>
33             <artifactId>spring-webmvc</artifactId>
34             <version>4.1.6.RELEASE</version>
35         </dependency>
36         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
37         <dependency>
38             <groupId>org.springframework</groupId>
39             <artifactId>spring-context</artifactId>
40             <version>4.1.6.RELEASE</version>
41         </dependency>
42         <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
43         <dependency>
44             <groupId>org.springframework</groupId>
45             <artifactId>spring-core</artifactId>
46             <version>4.1.6.RELEASE</version>
47         </dependency>
48         <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
49         <dependency>
50             <groupId>org.springframework</groupId>
51             <artifactId>spring-web</artifactId>
52             <version>4.1.6.RELEASE</version>
53         </dependency>
54         <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
55         <dependency>
56             <groupId>org.springframework</groupId>
57             <artifactId>spring-beans</artifactId>
58             <version>4.1.6.RELEASE</version>
59         </dependency>
60         <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
61         <dependency>
62             <groupId>org.springframework</groupId>
63             <artifactId>spring-aop</artifactId>
64             <version>4.1.6.RELEASE</version>
65         </dependency>
66         <!-- <dependency>
67             <groupId>org.apache.zookeeper</groupId>
68             <artifactId>zookeeper</artifactId>
69             <version>3.4.7</version>
70         </dependency>
71         <dependency>
72             <groupId>com.github.sgroschupf</groupId>
73             <artifactId>zkclient</artifactId>
74             <version>0.1</version>
75         </dependency> -->
76         <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
77         <!-- 访问zookeeper客户端jar包 -->
78         <dependency>
79             <groupId>com.101tec</groupId>
80             <artifactId>zkclient</artifactId>
81             <version>0.10</version>
82         </dependency>
83         
84     </dependencies>
85 </project>

消费者的配置文件applicationContext-dubbo.xml如下所示:

代码语言:javascript复制
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7     http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context 
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://code.alibabatech.com/schema/dubbo 
11         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
12 
13     <!-- 配置注解驱动 -->
14     <dubbo:annotation package="com.bie.consumer.service.impl" />
15     
16     <!-- 1、首先给当前的provider起一个名称,方便监控中心进行监控 -->
17     <dubbo:application name="dubbo-consumer" />
18     <!-- 2、配置注册中心,protocol="dubbo"代表了是dubbo协议,地址是192.168.110.140:2181 -->
19     <dubbo:registry protocol="zookeeper"
20         address="192.168.110.140:2181"></dubbo:registry>
21     
22     <!-- 3、将bean注入到容器中 -->    
23     <bean id="dubboConsumerService" class="com.bie.consumer.service.impl.DubboConsumerServiceImpl"></bean>
24     
25 </beans>        

这个相当于调用服务层的,所以这个项目相当于是service层的,所以业务代码如下所示:

测试代码和测试效果如下所示:

注意:记得启动你的zookeeper哦。

待续......

0 人点赞