SpringCloud学习笔记(四)_ZooKeeper注册中心

基于Spring Cloud实现服务的发布与调用。而在18年7月份,Eureka2.0宣布停更了,将不再进行开发,所以对于公司技术选型来说,可能会换用其他方案做注册中心。本章学习便是使用ZooKeeper作为注册中心。

本章使用的zookeeper版本是 3.6.0

项目架构图如下:

SpringCloud学习笔记(四)_ZooKeeper注册中心_第1张图片

搭建服务提供者

1、新建一个maven项目(test-springcloud-provider-payment8004)

结构如下:

SpringCloud学习笔记(四)_ZooKeeper注册中心_第2张图片

2、引入依赖,编辑pom文件

1 
2 
3     org.springframework.cloud
4     spring-cloud-starter-zookeeper-discovery
5 

完整pom文件如下:

 1 
 2 
 5     
 6         test-springcloud
 7         com.test
 8         1.0-SNAPSHOT
 9     
10     4.0.0
11 
12     test-springcloud-provider-payment8004
13 
14     
15 
16         
17         
18             org.springframework.cloud
19             spring-cloud-starter-zookeeper-discovery
20             
21             
22                 
23                     org.apache.zookeeper
24                     zookeeper
25                 
26             
27         
28 
29         
30         
31             org.apache.zookeeper
32             zookeeper
33             3.6.0
34             
35                 
36                     org.slf4j
37                     slf4j-log4j12
38                 
39                 
40                     log4j
41                     log4j
42                 
43             
44         
45 
46         
47         
48             org.springframework.boot
49             spring-boot-starter-web
50         
51         
52             org.springframework.boot
53             spring-boot-starter-actuator
54         
55 
56         
57             org.springframework.boot
58             spring-boot-devtools
59             runtime
60             true
61         
62         
63             org.projectlombok
64             lombok
65             true
66         
67 
68         
69             org.springframework.boot
70             spring-boot-starter-test
71             test
72         
73 
74     
75 
76     
77         test-springcloud-provider-payment8004
78     
79 
80 

pom.xml

需要注意,由于通过spring-cloud-starter-zookeeper-discovery依赖引入的zookeeper jar包,于zookeeper服务器版本不一致导致的,导致项目启动失败

报错:Caused by: org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /services/xx/xxx

解决:引入对于版本的 zookeeper jar包,本章使用的zookeeper版本是3.6.0,所以引入zookeeper-3.6.0.jar,如下:

 1 
 2 
 3     org.springframework.cloud
 4     spring-cloud-starter-zookeeper-discovery
 5     
 6     
 7         
 8             org.apache.zookeeper
 9             zookeeper
10         
11     
12 
13 
14 
15 
16     org.apache.zookeeper
17     zookeeper
18     3.6.0
19     
20         
21             org.slf4j
22             slf4j-log4j12
23         
24         
25             log4j
26             log4j
27         
28     
29 

pom.xml

3、编辑配置文件application.yml

 1 # 端口
 2 server:
 3   port: 8004
 4 
 5 spring:
 6   application:
 7     name: cloud-payment-service
 8   cloud:
 9     zookeeper:
10       # 集群模式用逗号隔开
11       connect-string: 127.0.0.1:2181

4、编写主启动类

1 // 启用服务发现
2 @EnableDiscoveryClient
3 @SpringBootApplication
4 public class PaymentMain8004 {
5     public static void main(String[] args) {
6         SpringApplication.run(PaymentMain8004.class, args);
7     }
8 }

5、编写Controller

 1 @RestController
 2 @Slf4j
 3 public class PaymentController {
 4 
 5     @Value("${server.port}")
 6     private String serverPort;
 7 
 8     @RequestMapping(value = "payment/zk")
 9     public String paymentzk(){
10         return "springcloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID();
11     }
12 }

6、启动项目,测试

1)使用地址:http://localhost:8004/payment/zk

SpringCloud学习笔记(四)_ZooKeeper注册中心_第3张图片

2)使用zookeeper客户端连接到zookeeper服务中,查看节点信息

SpringCloud学习笔记(四)_ZooKeeper注册中心_第4张图片

json格式如下:

 1 {
 2     "name": "cloud-payment-service",
 3     "id": "4f3db6b1-7d3a-4b3e-ac7a-159289573440",
 4     "address": "192.168.1.4",
 5     "port": 8004,
 6     "sslPort": null,
 7     "payload": {
 8         "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
 9         "id": "application-1",
10         "name": "cloud-payment-service",
11         "metadata": {}
12     },
13     "registrationTimeUTC": 1586166066913,
14     "serviceType": "DYNAMIC",
15     "uriSpec": {
16         "parts": [{
17             "value": "scheme",
18             "variable": true
19         }, {
20             "value": "://",
21             "variable": false
22         }, {
23             "value": "address",
24             "variable": true
25         }, {
26             "value": ":",
27             "variable": false
28         }, {
29             "value": "port",
30             "variable": true
31         }]
32     }
33 }

View Code

7、测试zookeeper的服务节点是临时节点还是永久节点?

通过关闭应用服务,在zookeeper客户端中是用命令:ls /services/cloud-payment-service,

查看服务之后存在,然后启动服务,查看节点ID是否相同

SpringCloud学习笔记(四)_ZooKeeper注册中心_第5张图片

通过测试验证:zookeeper的服务节点是临时节点

搭建服务消费者

1、新建一个maven项目(test-springcloud-order7999)

项目结构如下:

SpringCloud学习笔记(四)_ZooKeeper注册中心_第6张图片

2、引入pom依赖,同上(与服务提供者依赖相同)

3、编辑application.yml文件

 1 # 端口
 2 server:
 3   port: 7999
 4 
 5 spring:
 6   application:
 7     name: cloud-order
 8   cloud:
 9     zookeeper:
10       connect-string: 127.0.0.1

4、编写主启动类

1 @SpringBootApplication
2 public class OrderMain7999 {
3     public static void main(String[] args) {
4         SpringApplication.run(OrderMain7999.class, args);
5     }
6 }

5、编辑配置类,注入RestTemplate对象

 1 @Configuration
 2 public class AppConfig {
 3 
 4     /**
 5      * 注入restTemplate,请用请求rest接口
 6      * @return
 7      */
 8     @Bean
 9     // 标注此注解后,RestTemplate就具有了客户端负载均衡能力
10     // 负载均衡技术依赖于的是Ribbon组件~
11     // RestTemplate都塞入一个loadBalancerInterceptor 让其具备有负载均衡的能力
12     @LoadBalanced
13     public RestTemplate restTemplate(){
14         return new RestTemplate();
15     }
16 }

6、编辑Controller

 1 @RestController
 2 @Slf4j
 3 public class OrderController {
 4 
 5     public static final String PAYMENT_URL = "http://cloud-payment-service";
 6 
 7     @Autowired
 8     private RestTemplate restTemplate;
 9 
10     @GetMapping("/consumer/payment/zk")
11     public String paymentzk(){
12         return restTemplate.getForObject(PAYMENT_URL + "/payment/zk", String.class);
13     }
14 
15 }

7、启动项目测试

1)访问地址:http://localhost:7999/consumer/payment/zk

SpringCloud学习笔记(四)_ZooKeeper注册中心_第7张图片

2)使用zookeeper客户端登录zookeeper服务器查看

你可能感兴趣的:(SpringCloud,spring,cloud,学习,笔记)