1. 程式人生 > >Java微服務(二):服務消費者與提供者搭建

Java微服務(二):服務消費者與提供者搭建

  本文接著上一篇寫的《Java微服務(一):dubbo-admin控制檯的使用》,上篇文章介紹了docker,zookeeper環境的安裝,並參考dubbo官網演示了dubbo-admin控制檯的使用。上篇文章已經搭建好zookeeper服務註冊中心,本片文章主要搭建服務消費者和服務提供者。按照微服務的原則,本文將demo分為3部分:服務介面、服務消費者、服務消費者。

  服務介面:定義了系統所需要的全部介面。

  服務提供者:主要是對介面的實現。

  服務消費者:對介面的使用

1.Dubbo介紹

 

 

 

節點角色說明
Provider 暴露服務的服務提供方
Consumer 呼叫遠端服務的服務消費方
Registry 服務註冊與發現的註冊中心
Monitor 統計服務的呼叫次數和呼叫時間的監控中心
Container 服務執行容器
 

  Dubbo 架構具有以下幾個特點,分別是連通性、健壯性、伸縮性、以及向未來架構的升級性。

呼叫關係說明
  1. 服務容器負責啟動,載入,執行服務提供者。
  2. 服務提供者在啟動時,向註冊中心註冊自己提供的服務。
  3. 服務消費者在啟動時,向註冊中心訂閱自己所需的服務。
  4. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連線推送變更資料給消費者。
  5. 服務消費者,從提供者地址列表中,基於軟負載均衡演算法,選一臺提供者進行呼叫,如果呼叫失敗,再選另一臺呼叫。
  6. 服務消費者和提供者,在記憶體中累計呼叫次數和呼叫時間,定時每分鐘傳送一次統計資料到監控中心。

Dubbo 架構具有以下幾個特點,分別是連通性、健壯性、伸縮性、以及向未來架構的升級性。

更詳細的介紹,請參考官網:http://dubbo.apache.org/en-us/

2.服務介面

  通過idea建立一個jar工程,建立工程的過程可以參考《Spring boot 入門(一):快速搭建Spring boot專案》,此工程的目的只是簡單的定義介面,所以這裡直接建立jar包,不是maven工程。建立好了後,新建一個介面。以下是我建立的介面:

 

  其中UserService程式碼如下:

1 package com.edu.hello.dubbo.service.user.api;
2 
3 public interface UserService {
4     String sayHi();
5 }

  建立完介面後,需要把介面install到本地倉庫,供服務消費者和服務提供者使用

  在Terminal直接指向mvn clean install或者直接在lifecycle目錄下點選install進行安裝,出現如下頁面,表示安裝成功

 

 

3.服務提供者

  服務提供者主要是對介面的實現,用相同的方法建立一個maven工程,建立好後的maven工程目錄如下:

 

其中UserServicelmpl是多介面的實現,程式碼如下:

 1 package com.edu.hello.dubbo.service.user.provider.api.impl;
 2 
 3 import com.alibaba.dubbo.config.annotation.Service;
 4 import com.edu.hello.dubbo.service.user.api.UserService;
 5 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
 6 import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
 7 import org.springframework.beans.factory.annotation.Value;
 8 
 9 /**
10  * @ClassName UserServiceImpl
11  * @Deccription TODO
12  * @Author DZ
13  * @Date 2019/8/31 11:20
14  **/
15 @Service(version = "${user.service.version}")
16 public class UserServiceImpl implements UserService {
17 
18     @Value("${dubbo.protocol.port}")
19     private String port;
20 
21     /*@HystrixCommand(commandProperties = {
22             @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
23             @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
24     })*/
25     @Override
26     public String sayHi() {
27         return "Say Hello, i am from " + port;
28     }
29 }

其中@HystrixCommand註解在後面熔斷器中會講到,這裡先註釋。

yml配置如下:

 1 spring:
 2   application:
 3     name: hello-dubbo-service-user-provider
 4 
 5 user:
 6   service:
 7     version: 1.0.0
 8 
 9 dubbo:
10   scan:
11     basePackages: com.edu.hello.dubbo.service.user.provider.api
12   application:
13     id: hello-dubbo-service-user-provider
14     name: hello-dubbo-service-user-provider
15     qos-port: 22222
16     qos-enable: true
17   protocol:
18     id: dubbo
19     name: dubbo
20     port: 12346
21     status: server
22     serialization: kryo #高速序列化
23     # optimizer:
24 
25   registry:
26     id: zookeeper
27     address: zookeeper://192.168.1.12:2181?back=192.168.1.12:2182,192.168.1.12:2183
28   provider:
29     loadbalance: roundrobin #負載均衡
30 
31 
32 
33 management:
34   endpoint:
35     dubbo:
36       enable: true
37     dubbo-shutdown:
38       enabled: true
39     dubbo-configs:
40       enabled: true
41     dubbo-services:
42       enabled: true
43     dubbo-references:
44       enabled: true
45     dubbo-peoperties:
46       enabled: true
47   health:
48     dubbo:
49       status:
50         defaults: memory
51         extras: load,threadpool
View Code

pom檔案如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.7.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.edu</groupId>
12     <artifactId>hello-dubbo-service-user-provider</artifactId>
13     <version>1.0.0-SNAPSHOT</version>
14     <name>hello-dubbo-service-user-provider</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter</artifactId>
25         </dependency>
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-actuator</artifactId>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34         </dependency>
35         <dependency>
36             <groupId>com.alibaba.boot</groupId>
37             <artifactId>dubbo-spring-boot-starter</artifactId>
38             <version>0.2.0</version>
39         </dependency>
40         <dependency>
41             <groupId>com.edu</groupId>
42             <artifactId>hello-dubbo-service-user-api</artifactId>
43             <version>${project.version}</version>
44         </dependency>
45         <dependency>
46             <groupId>de.javakaffee</groupId>
47             <artifactId>kryo-serializers</artifactId>
48             <version>0.42</version>
49         </dependency>
50         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
51         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
52         <dependency>
53             <groupId>org.springframework.cloud</groupId>
54             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
55             <version>2.0.1.RELEASE</version>
56         </dependency>
57         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard -->
58         <dependency>
59             <groupId>org.springframework.cloud</groupId>
60             <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
61             <version>2.0.1.RELEASE</version>
62         </dependency>
63 
64 
65 
66     </dependencies>
67 
68     <build>
69         <plugins>
70             <plugin>
71                 <groupId>org.springframework.boot</groupId>
72                 <artifactId>spring-boot-maven-plugin</artifactId>
73             </plugin>
74         </plugins>
75     </build>
76 
77 </project>
View Code

本文pom檔案和yml檔案主要是根據dubbo官網中服務提供者的pom檔案中的依賴而來,具體參考:https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples/dubbo-registry-zookeeper-samples。

 

 

注意basePackages註解

4.服務提供者

按照相同的方式建立服務提供者,配置檔案和服務提供者也類似,直接貼程式碼了

 

 

 1 package com.edu.hello.dubbo.service.user.consumer.controller;
 2 
 3 
 4 import com.alibaba.dubbo.config.annotation.Reference;
 5 import com.edu.hello.dubbo.service.user.api.UserService;
 6 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 /**
12  * @ClassName UserController
13  * @Deccription TODO
14  * @Author DZ
15  * @Date 2019/8/31 18:37
16  **/
17 @RestController
18 public class UserController {
19 
20     @Reference(version = "${user.service.version}")
21     private UserService userService;
22 
23     @HystrixCommand(fallbackMethod = "sayHiError")
24     @RequestMapping(value = "hi", method = RequestMethod.GET)
25     public String sayHi() {
26         return userService.sayHi();
27     }
28 
29     public String sayHiError() {
30         return "Hystrix fallback";
31     }
32 }

 

 

yml配置檔案和pom配置檔案和提供者基本類似;

 1 spring:
 2   application:
 3     name: hello-dubbo-service-user-consumer
 4 
 5 user:
 6   service:
 7     version: 1.0.0
 8 
 9 dubbo:
10   scan:
11     basePackages: com.edu.hello.dubbo.service.user.consumer.controller
12   application:
13     id: hello-dubbo-service-user-consumer
14     name: hello-dubbo-service-user-consumer
15     qos-port: 22223
16     qos-enable: true
17   protocol:
18     id: dubbo
19     name: dubbo
20     port: 12345
21     #status: server
22     serialization: kryo
23   registry:
24     id: zookeeper
25     address: zookeeper://192.168.1.12:2181?back=192.168.1.12:2182,192.168.1.12:2183
26 
27 
28 management:
29   endpoint:
30     dubbo:
31       enable: true
32     dubbo-shutdown:
33       enabled: true
34     dubbo-configs:
35       enabled: true
36     dubbo-services:
37       enabled: true
38     dubbo-references:
39       enabled: true
40     dubbo-peoperties:
41       enabled: true
42   health:
43     dubbo:
44       status:
45         defaults: memory
46         extras: load,threadpool
47 server:
48   port: 9090
View Code
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.7.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.edu</groupId>
12     <artifactId>hello-dubbo-service-user-consumer</artifactId>
13     <version>1.0.0-SNAPSHOT</version>
14     <name>hello-dubbo-service-user-consumer</name>
15     <description>Demo project for Spring Boot</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-web</artifactId>
25         </dependency>
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-actuator</artifactId>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34         </dependency>
35         <dependency>
36             <groupId>com.alibaba.boot</groupId>
37             <artifactId>dubbo-spring-boot-starter</artifactId>
38             <version>0.2.0</version>
39         </dependency>
40         <dependency>
41             <groupId>com.edu</groupId>
42             <artifactId>hello-dubbo-service-user-api</artifactId>
43             <version>${project.version}</version>
44         </dependency>
45         <dependency>
46             <groupId>de.javakaffee</groupId>
47             <artifactId>kryo-serializers</artifactId>
48             <version>0.42</version>
49         </dependency>
50         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
51         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
52         <dependency>
53             <groupId>org.springframework.cloud</groupId>
54             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
55             <version>2.0.1.RELEASE</version>
56         </dependency>
57         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard -->
58         <dependency>
59             <groupId>org.springframework.cloud</groupId>
60             <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
61             <version>2.0.1.RELEASE</version>
62         </dependency>
63     </dependencies>
64 
65     <build>
66         <plugins>
67             <plugin>
68                 <groupId>org.springframework.boot</groupId>
69                 <artifactId>spring-boot-maven-plugin</artifactId>
70                 <configuration>
71                     <mainClass>com.edu.hello.dubbo.service.user.consumer.HelloDubboServiceUserConsumerApplication</mainClass>
72                 </configuration>
73             </plugin>
74         </plugins>
75     </build>
76 
77 </project>
View Code

這裡面關於服務熔斷和負載均衡的的程式碼可以暫時不關注,後面會專門對熔斷進行討論。

 

5.結果

分別啟動服務消費者和服務提供者,啟動成功後,如下:

 

 訪問http://localhost:9090/hi

 

同時我們可以啟動dubbo-admin控制檯檢視服務,注意埠的衝突: