1. 程式人生 > >ZooKeeper+Dubbo+SpringBoot 微服務Demo搭建

ZooKeeper+Dubbo+SpringBoot 微服務Demo搭建

mls 好的 pro sch 類型 OS 127.0.0.1 ddr odin

 1、 首先創建springBoot項目,springBoot是一堆組件的集合,在pom文件中對需要的組件進行配置。生成如下目錄結構

    技術分享圖片

    創建test項目,同步在test創建dubbo-api,dubbo-consume,dubbo-provider三個Model

    dubbo-provider 為服務的提供者,服務的實現層

    dubbo-api 為服務提供者dubbo-provider的接口層 (此處dubbo-provider,dubbo-api相當於一個微服務,dubbo-api為對外接口,由消費者dubbo-consume調用)

    dubbo-consume 為服務的消費者,消費由dubbo-api提供的服務

2、配置pom.xml

  test中的pom.xml是其他三個Module的父文件,所以公共的部分在test中配置即可。

  test 中 pom.xml部分配置  

技術分享圖片
 1 <modules>
 2         <module>dubbo-provider</module>
 3         <module>dubbo-consume</module>
 4         <module>dubbo-api</module>
 5     </modules>
 6 <
parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>2.0.3.RELEASE</version> 10 <relativePath/> <!-- lookup parent from repository --> 11 </parent
> 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 15 <java.version>1.8</java.version> 16 <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version> 17 <org.apache.zookeeper.version>3.4.12</org.apache.zookeeper.version> 18 <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version> 19 </properties> 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-test</artifactId> 29 <scope>test</scope> 30 </dependency> 31 </dependencies> 32 33 <build> 34 <plugins> 35 <plugin> 36 <groupId>org.springframework.boot</groupId> 37 <artifactId>spring-boot-maven-plugin</artifactId> 38 </plugin> 39 </plugins> 40 </build>
pom.xml

  dubbo-api中pom.xml

技術分享圖片
 1  <parent>
 2         <artifactId>test</artifactId>
 3         <groupId>com.example</groupId>
 4         <version>0.0.1-SNAPSHOT</version>
 5     </parent>
 6 
 7     <artifactId>dubbo-api</artifactId>
 8     <packaging>jar</packaging>
 9     <name>dubbo-api</name>
10     <dependencies>
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-starter-web</artifactId>
14         </dependency>
15     </dependencies>
16     <build>
17         <plugins>
18             <plugin>
19                 <groupId>org.springframework.boot</groupId>
20                 <artifactId>spring-boot-maven-plugin</artifactId>
21             </plugin>
22         </plugins>
23     </build>
pom.xml

  dubbo-provider中pom.xml

技術分享圖片
 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     <parent>
 7         <artifactId>test</artifactId>
 8         <groupId>com.example</groupId>
 9         <version>0.0.1-SNAPSHOT</version>
10     </parent>
11 
12     <artifactId>dubbo-provider</artifactId>
13     <packaging>jar</packaging>
14     <name>dubbo-provide</name>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>com.example</groupId>
22             <artifactId>dubbo-api</artifactId>
23             <version>0.0.1-SNAPSHOT</version>
24         </dependency>
25         <!-- dubbo -->
26         <dependency>
27             <groupId>com.alibaba</groupId>
28             <artifactId>dubbo</artifactId>
29             <exclusions>
30                 <exclusion>
31                     <groupId>org.springframework</groupId>
32                     <artifactId>spring</artifactId>
33                 </exclusion>
34             </exclusions>
35             <version>${com.alibaba.dubbo.version}</version>
36         </dependency>
37         <dependency>
38             <groupId>org.apache.zookeeper</groupId>
39             <artifactId>zookeeper</artifactId>
40             <version>${org.apache.zookeeper.version}</version>
41         </dependency>
42         <dependency>
43             <groupId>com.github.sgroschupf</groupId>
44             <artifactId>zkclient</artifactId>
45             <version>${com.github.sgroschupf.zkclient.version}</version>
46         </dependency>
47     </dependencies>
48     <build>
49         <plugins>
50             <plugin>
51                 <groupId>org.springframework.boot</groupId>
52                 <artifactId>spring-boot-maven-plugin</artifactId>
53             </plugin>
54         </plugins>
55     </build>
56 </project>
pom.xml

  dubbo-consume中pom.xml

技術分享圖片
 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     <parent>
 7         <artifactId>test</artifactId>
 8         <groupId>com.example</groupId>
 9         <version>0.0.1-SNAPSHOT</version>
10     </parent>
11 
12     <artifactId>dubbo-consume</artifactId>
13      <name>dubbo-consume</name>
14     <packaging>jar</packaging>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>com.example</groupId>
22             <artifactId>dubbo-api</artifactId>
23             <version>0.0.1-SNAPSHOT</version>
24         </dependency>
25         <!-- dubbo -->
26         <dependency>
27             <groupId>com.alibaba</groupId>
28             <artifactId>dubbo</artifactId>
29             <exclusions>
30                 <exclusion>
31                     <groupId>org.springframework</groupId>
32                     <artifactId>spring</artifactId>
33                 </exclusion>
34             </exclusions>
35             <version>${com.alibaba.dubbo.version}</version>
36         </dependency>
37         <dependency>
38             <groupId>org.apache.zookeeper</groupId>
39             <artifactId>zookeeper</artifactId>
40             <version>${org.apache.zookeeper.version}</version>
41         </dependency>
42         <dependency>
43             <groupId>com.github.sgroschupf</groupId>
44             <artifactId>zkclient</artifactId>
45             <version>${com.github.sgroschupf.zkclient.version}</version>
46         </dependency>
47     </dependencies>
48     <build>
49         <plugins>
50             <plugin>
51                 <groupId>org.springframework.boot</groupId>
52                 <artifactId>spring-boot-maven-plugin</artifactId>
53             </plugin>
54         </plugins>
55     </build>
56 </project>
pom.xml

3、dubbo-api  

  技術分享圖片

  定義接口IDubboDemoService

public interface IDubboDemoService {

    public String getString();
}

4、dubbo-provider

技術分享圖片

首先創建啟動類,和dubbo配置文件啟動類,mainConfig項目啟動類就不多說了

技術分享圖片
@Configuration
@PropertySource("classpath:dubbo-provider.properties")
@ImportResource({ "classpath:dubbo-provider.xml" })
public class DubboConfig {
}
DubboConfig.java

dubbo讀取的兩個配置文件

首先對dubbo進行配置

#應用名稱
dubbo.application.name=dubbo-provider
#註冊中心類型
dubbo.registry.protocol=zookeeper
#註冊中心地址
dubbo.registry.address=127.0.0.1:2181
#暴露服務方式
dubbo.protocol.name=dubbo
#暴露服務端口
dubbo.protocol.port=20880

之後對dubbo中的接口進行配置

<?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://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方應用信息,用於計算依賴關系 -->
    <dubbo:application name="${dubbo.application.name}" />

    <!-- 註冊中心暴露服務地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

    <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> -->
    <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />

    <!-- 暴露服務 -->
    <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />
  <!-- 暴露的接口 -->
    <dubbo:service interface="com.example.service.IDubboDemoService"
                   ref="dubboDemoServiceImpl" retries="0" timeout="6000" />

</beans>

註意:mainConfig.java和DubboConfg.java 兩個文件要放在接口的上一層,否則加載時會找不到接口

  接口實現:

@Service
public class DubboDemoServiceImpl implements IDubboDemoService {
    @Override
    public String   getString() {
        return "成功";
    }
}

5、dubbo-consume

技術分享圖片

只來看下配置文件

  dubbo.consume.properties:消費者只需要知道註冊中心的類型和地址

#應用名稱
dubbo.application.name=dubbo-consume
#註冊中心類型
dubbo.registry.protocol=zookeeper
#註冊中心地址
dubbo.registry.address=127.0.0.1:2181

  dubbo-consume.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://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方應用信息,用於計算依賴關系 -->
    <dubbo:application name="${dubbo.application.name}" />

    <!-- 註冊中心暴露服務地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

    <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> -->
    <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />

    <dubbo:reference id="consumeService" interface="com.example.service.IDubboDemoService" />

</beans>

測試代碼

  

技術分享圖片
 1 @RestController
 2 @RequestMapping("/dubbo")
 3 public class DubboDemoController {
 4 
 5         @Resource
 6          public IDubboDemoService service;
 7          
 8         @RequestMapping(value = "/getString", method = RequestMethod.GET)
 9         public String getString(){
10             return service.getString();
11         }
12 
13 }
DubboDemoController.java

到此簡單的配置已經ok了。

6、整個流程為 服務的提供者方面 dubbo將對外的服務註冊到zooKeeper上,同時在zooKeeper生成相應的node,對其監控。消費方從zooKeeper中去拿服務,所以服務的接口必須到時提供者和消費者約定好的。這樣就利用dubbo+zooKeeper實現的遠程RPC調用

ZooKeeper+Dubbo+SpringBoot 微服務Demo搭建