关于dubbo的使用,我们举个简单例子:
存在2个系统,A系统和B系统,A系统调用B系统的接口获取数据,用于查询用户列表。
在上一篇博文介绍了dubbo的创建,zookeeper的创建完成后,我们可以来使用dubbo和zookeeper了?
再回顾下dubbo的架构:
所以通过此图,我们看到就是服务的提供者将服务注册到注册中心,服务的消费者从注册中心获取服务,monitor监控服务的调用。
所谓框架无非就是配置文件+java代码,所以dubbo也同理:
(1)首先看下B服务的提供者的配置文件和代码:
搭建b系统:
建立一个maven的war工程,导入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <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>cn.itcast.dubbo</groupId> <artifactId>dubbo-b</artifactId> <version> 1.0 . 0 -SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version> 1.6 . 4 </version> </dependency><br data-filtered= "filtered" > //zookeeper的依赖需要导入,因为需要使用zkclient |
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version> 2.5 . 3 </version> <exclusions> <exclusion> <!-- 排除传递spring依赖 --> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version> 2.2 </version> <configuration> <port> 8081 </port> <path>/</path> </configuration> </plugin> </plugins> </build> </project> |
第二步:创建user对象:注意这里一定要实现序列化,并获取序列化的序号:
1 | //关于这个的获取,直接点击小黄色图标,既可以自动生成 |
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package cn.itcast.dubbo.pojo; // 使用dubbo要求传输的对象必须实现序列化接口 public class User implements java.io.Serializable { private static final long serialVersionUID = -2668999087589887337L;<br data-filtered= "filtered" > //关于这个的获取,直接点击小黄色图标,既可以自动生成 private Long id; private String username; private String password; private Integer age; public Long getId() { return id; } public void setId(Long id) { this .id = id; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } } |
创建服务接口和服务接口的实现类:
接口为了暴露服务的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 5.3 . 5 . 创建UserService(接口)提供查询服务 package cn.itcast.dubbo.service; import java.util.List; import cn.itcast.dubbo.pojo.User; public interface UserService { /** * 查询所有的用户数据 * * @return */ public List<User> queryAll(); } 5.3 . 6 . 创建UserServiceImpl实现类 package cn.itcast.dubbo.service.impl; import java.util.ArrayList; import java.util.List; import cn.itcast.dubbo.pojo.User; import cn.itcast.dubbo.service.UserService; public class UserServiceImpl implements UserService { /** * 实现查询,这里做模拟实现,不做具体的数据库查询 */ public List<User> queryAll() { List<User> list = new ArrayList<User>(); for ( int i = 0 ; i < 10 ; i++) { User user = new User(); user.setAge( 10 + i); user.setId(Long.valueOf(i + 1 )); user.setPassword( "123456" ); user.setUsername( "username_" + i); list.add(user); } return list; } } |
下面就是dubbo的服务提供者的配置文件了:
实际项目中,相当于服务的提供者的第一步:编写配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" 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-4.0.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http: //code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name= "dubbo-b-server" /> <!-- 这里使用的注册中心是zookeeper --> <dubbo:registry address= "zookeeper://127.0.0.1:2181" client= "zkclient" /> <!-- 用dubbo协议在 20880 端口暴露服务 --> <dubbo:protocol name= "dubbo" port= "20880" /> <!-- 将该接口暴露到dubbo中 --> <dubbo:service interface = "cn.itcast.dubbo.service.UserService" ref= "userServiceImpl" /> <!-- 将具体的实现类加入到Spring容器中 --> <bean id= "userServiceImpl" class = "cn.itcast.dubbo.service.impl.UserServiceImpl" /><br data-filtered= "filtered" > //监控的配置<br data-filtered="filtered"> <dubbo:monitor protocol="registry"></dubbo:monitor> </beans> |
这里spring与dubbo进行了无缝整合,所以这里进行了spring与dubbo的整合:
第二步:读取配置文件
我们需要在web.xml中将配置文件引入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version= "1.0" encoding= "UTF-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "WebApp_ID" version= "2.5" > <display-name>dubbo-b</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo/dubbo-*.xml</param-value> </context-param> <!--Spring的ApplicationContext 载入 --> <listener> <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class > </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
第三步:配置服务消费者:
这里我们建立a的系统:
建立一个maven的jar工程:
导入依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <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>cn.itcast.dubbo</groupId> <artifactId>dubbo-a</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <dependencies> <!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version> 1.6 . 4 </version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version> 2.5 . 3 </version> <exclusions> <exclusion> <!-- 排除传递spring依赖 --> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version> 3.3 . 3 </version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version> 0.1 </version> </dependency> </dependencies> </project> |
从b系统拷贝:
1.1.1. 从b系统中拷贝User对象、UserService接口道a系统
服务的消费者配置:
dubbo-consumer.xml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | dubbo-consumer.xml: <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" 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-4.0.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http: //code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name= "dubbo-a-consumer" /> <!-- 这里使用的注册中心是zookeeper --> <dubbo:registry address= "zookeeper://127.0.0.1:2181" client= "zkclient" /> <!-- 从注册中心中查找服务 --> <dubbo:reference id= "userService" interface = "cn.itcast.dubbo.service.UserService" /> </beans> |
编写测试:代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package cn.itcast.dubbo.service; import static org.junit.Assert.*; import java.util.List; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.dubbo.pojo.User; public class UserServiceTest { private UserService userService; //从这里我们可以看出,使用了dubbo后感觉调用服务是在自己的工程一样,但是其实我们是调用的b系统,因为b将服务接口暴露出来了,我们通过访问注册中心的服务接口来获取服务的实现。 @Before public void setUp() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:dubbo/*.xml" ); this .userService = applicationContext.getBean(UserService. class ); } @Test public void testQueryAll() { List<User> users = this .userService.queryAll(); for (User user : users) { System.out.println(user); } } } |
这个是dubbo的简单入门程序,具体在项目中也是同样的道理。
不过上面代码有重复,我们可以家里一个公共的maven来存放pojo和服务接口,然后ab工程添加依赖即可。
在我们的项目中,一般步骤如下;
(1)导入dubbo、zookeeper依赖
(2)在服务提供者端,编写服务接口,服务接口的实现类,编写配置文件
(3)修改web.xml读取配置文件
(4)在服务消费者,即客户端,调用服务接口,调用服务实现类,编写配置文件
这样系统间就可以互相通信,从而感觉像在本地使用一样。
使用dubbo不像httpclient一样json的转换,因为dubbo都自动进行了。