Dubbo基础使用

Posted 凭栏倚窗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo基础使用相关的知识,希望对你有一定的参考价值。

概述:本文Dubbo采用全Spring配置方式,透明化介入应用,只需用Spring加载Dubbo的配置即可,注册中心使用zookeeper,编辑器采用idea。
一、安装配置zookeeper
1、在官网http://zookeeper.apache.org 下载zookeeper,我使用的版本是3.4.14;
2、解压zookeeper压缩包至你想安装的路径;
3、ZooKeeper的安装模式分为三种,分别为:单机模式(stand-alone)、集群模式和集群伪分布模式。ZooKeeper 单机模式的安装相对比较简单,如果第一次接触ZooKeeper的话,建议安装ZooKeeper单机模式或者集群伪分布模式。
    安装单机模式
        (1)到zookeeper安装目录中的bin目录下复制zoo_simple.cfg并粘贴至当前目录下,重命名为zoo.cfg;
        (2)修改zoo.cfg配置
        (3)cmd命令下进入安装目录下的bin目录,运行zkServer.cmd,如下图所示:
        (4)可以在bin目录运行zkcli.cmd查看zookeeper节点信息

 

 

二、创建Dubbo项目
    1、创建父项目(Maven项目)

 

    2、在父项目中分别创建common(存放接口定义或bean)、provider(服务提供者)、consumer(服务消费者)的module子项目

    3、分别修改pom文件
        (1)父项目的pom文件,添加Dubbo和zookeeper等依赖
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.mysteel</groupId>
 8     <artifactId>test</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <modules>
12         <module>common</module>
13         <module>consumer</module>
14         <module>provider</module>
15     </modules>
16     
17         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
18         <dependency>
19             <groupId>org.springframework</groupId>
20             <artifactId>spring-context</artifactId>
21             <version>5.2.3.RELEASE</version>
22         </dependency>
23 
24         <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
25         <dependency>
26             <groupId>org.springframework</groupId>
27             <artifactId>spring-core</artifactId>
28             <version>5.2.3.RELEASE</version>
29         </dependency>
30 
31         <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
32         <dependency>
33             <groupId>org.springframework</groupId>
34             <artifactId>spring-beans</artifactId>
35             <version>5.2.3.RELEASE</version>
36         </dependency>
37 
38         <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
39         <dependency>
40             <groupId>org.springframework</groupId>
41             <artifactId>spring-expression</artifactId>
42             <version>5.2.3.RELEASE</version>
43         </dependency>
44 
45         <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
46         <dependency>
47             <groupId>org.springframework</groupId>
48             <artifactId>spring-aop</artifactId>
49             <version>5.2.3.RELEASE</version>
50         </dependency>
51 
52         <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
53         <dependency>
54             <groupId>org.apache.dubbo</groupId>
55             <artifactId>dubbo</artifactId>
56             <version>2.7.3</version>
57         </dependency>
58 
59         <!--增加zookeeper作为注册中心-->
60         <dependency>
61             <groupId>org.apache.zookeeper</groupId>
62             <artifactId>zookeeper</artifactId>
63             <version>3.3.3</version>
64             <type>pom</type>
65         </dependency>
66 
67         <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
68         <dependency>
69             <groupId>org.apache.curator</groupId>
70             <artifactId>curator-recipes</artifactId>
71             <version>2.12.0</version>
72         </dependency>
73 
74         <!--使用curator客户端-->
75         <dependency>
76             <groupId>com.netflix.curator</groupId>
77             <artifactId>curator-framework</artifactId>
78             <version>1.1.10</version>
79         </dependency>
80 
81         <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
82         <dependency>
83             <groupId>org.slf4j</groupId>
84             <artifactId>slf4j-api</artifactId>
85             <version>1.7.24</version>
86         </dependency>
87 
88     </dependencies>
89 
90 </project>        
        (2)分别在provider、consumer中添加common项目的依赖
<dependency>
    <groupId>com.mysteel</groupId>
    <artifactId>common</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
    4、在common接口中定义接口、bean(需要实现Serializable接口)
 1 public class Address implements Serializable {
 2     private int id;
 3     private int userId;
 4     private String address;
 5 
 6     public Address(int id, int userId, String address) {
 7         this.id = id;
 8         this.userId = userId;
 9         this.address = address;
10     }
11 
12     public int getId() {
13         return id;
14     }
15 
16     public void setId(int id) {
17         this.id = id;
18     }
19 
20     public int getUserId() {
21         return userId;
22     }
23 
24     public void setUserId(int userId) {
25         this.userId = userId;
26     }
27 
28     public String getAddress() {
29         return address;
30     }
31 
32     public void setAddress(String address) {
33         this.address = address;
34     }
35 
36     @Override
37     public String toString() {
38         return "Address{" +
39                 "id=" + id +
40                 ", userId=" + userId +
41                 ", address=\'" + address + \'\\\'\' +
42                 \'}\';
43     }
44 }
 1 public class Person implements Serializable {
 2     private int userId;
 3     private String name;
 4     private int age;
 5     private List<Address> addresses;
 6 
 7     public Person(int userId, String name, int age, List<Address> addresses) {
 8         this.userId = userId;
 9         this.name = name;
10         this.age = age;
11         this.addresses = addresses;
12     }
13 
14     public int getUserId() {
15         return userId;
16     }
17 
18     public void setUserId(int userId) {
19         this.userId = userId;
20     }
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public int getAge() {
31         return age;
32     }
33 
34     public void setAge(int age) {
35         this.age = age;
36     }
37 
38     public List<Address> getAddresses() {
39         return addresses;
40     }
41 
42     public void setAddresses(List<Address> addresses) {
43         this.addresses = addresses;
44     }
45 
46     @Override
47     public String toString() {
48         return "Person{" +
49                 "userId=" + userId +
50                 ", name=\'" + name + \'\\\'\' +
51                 ", age=" + age +
52                 ", addresses=" + addresses +
53                 \'}\';
54     }
55 }
public interface UserService {
    Person getUserAddressesByUserId(int userId);
}
  
   5、在provider模块中实现接口,并暴露服务
        实现接口
 1 public class UserServiceImpl implements UserService {
 2     public Person getUserAddressesByUserId(int userId) {
 3         Person[] people = new Person[2];
 4         Address address1 = new Address(1,1,"江西省吉安市吉州区XXXX");
 5         Address address2 = new Address(2,1,"福建省厦门市思明区XXXX");
 6         Address address3 = new Address(3,2,"江西省南昌市青山湖区XXXX");
 7         List<Address> addressList1 = new ArrayList<Address>();
 8         addressList1.add(address1);
 9         addressList1.add(address2);
10         List<Address> addressList2 = new ArrayList<Address>();
11         addressList2.add(address3);
12         Person person1 = new Person(1,"张三",22,addressList1);
13         Person person2 = new Person(2,"李四",22,addressList2);
14         people[0] = person1;
15         people[1] = person2;
16         for (int i = 0;i < people.length;i++){
17             if (people[i].getUserId() == userId){
18                 return people[i];
19             }
20         }
21        return null;
22     }
23 }
        用Spring 声明暴露服务
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="provider"/>

    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="curator"/>

    <!-- 用dubbo协议在2088端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="2088"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.mysteel.cominterface.UserService" ref="userService"/>

    <!-- 和本地bean一样实现服务(暴露接口的实现) -->
    <bean id="userService" class="com.mysteel.impl.UserServiceImpl"/>

</beans>
        加载Spring配置,启动提供者(需要先启动zookeeper服务)
1 public class Provider {
2     public static void main(String[] args) throws IOException {
3         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
4         context.start();
5         System.in.read();
6     }
7 }

    6、在consumer模块中订阅服务
        通过 Spring 配置引用远程服务
 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 5 
 6     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
 7     <dubbo:application name="consumer"/>
 8 
 9     <!-- 使用zookeeper广播注册中心暴露服务地址 -->
10     <dubbo:registry address="zookeeper://127.0.0.1:2181" client="curator"/>
11 
12     <!-- 生成远程服务代理,可以和本地bean一样使用userService,声明需要调用的接口 interface填写common中接口的全路径 -->
13     <dubbo:reference interface="com.mysteel.cominterface.UserService" id="userService"/>
14 </beans>
        加载Spring配置,并调用远程服务
1 public class Consumer {
2     public static void main(String[] args) {
3         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:consumer.xml");
4         context.start();
5         //获取远程服务代理
6         UserService userService = (UserService) context.getBean("userService");
7         System.out.println(userService.getUserAddressesByUserId(1));
8     }
9 }

 

 

以上是关于Dubbo基础使用的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo基础专题——第四章(Dubbo整合Nacos分析细节点)

Dubbo基础知识——Dubbo的分层结构和执行流程,以及和Spring Cloud的区别。

Dubbo基础使用

基础配置Dubbo的配置及使用

dubbo基础dubbo学习过程使用经验分享及实现原理简单介绍

JMeter基础 — 使用JMeter测试Dubbo接口