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

news/2024/7/5 3:39:51

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

本章使用的zookeeper版本是 3.6.0

项目架构图如下:

搭建服务提供者

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

结构如下:

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

1 <!-- spring-cloud 整合 zookeeper -->
2 <dependency>
3     <groupId>org.springframework.cloud</groupId>
4     <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
5 </dependency>

完整pom文件如下:

 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     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>test-springcloud-provider-payment8004</artifactId>
13 
14     <dependencies>
15 
16         <!-- spring-cloud 整合 zookeeper -->
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
20             <!-- 排除自带的zookeeper jar包 -->
21             <exclusions>
22                 <exclusion>
23                     <groupId>org.apache.zookeeper</groupId>
24                     <artifactId>zookeeper</artifactId>
25                 </exclusion>
26             </exclusions>
27         </dependency>
28 
29         <!-- zookeeper 引入对应版本的zookeeper -->
30         <dependency>
31             <groupId>org.apache.zookeeper</groupId>
32             <artifactId>zookeeper</artifactId>
33             <version>3.6.0</version>
34             <exclusions>
35                 <exclusion>
36                     <groupId>org.slf4j</groupId>
37                     <artifactId>slf4j-log4j12</artifactId>
38                 </exclusion>
39                 <exclusion>
40                     <groupId>log4j</groupId>
41                     <artifactId>log4j</artifactId>
42                 </exclusion>
43             </exclusions>
44         </dependency>
45 
46         <!-- spring boot -->
47         <dependency>
48             <groupId>org.springframework.boot</groupId>
49             <artifactId>spring-boot-starter-web</artifactId>
50         </dependency>
51         <dependency>
52             <groupId>org.springframework.boot</groupId>
53             <artifactId>spring-boot-starter-actuator</artifactId>
54         </dependency>
55 
56         <dependency>
57             <groupId>org.springframework.boot</groupId>
58             <artifactId>spring-boot-devtools</artifactId>
59             <scope>runtime</scope>
60             <optional>true</optional>
61         </dependency>
62         <dependency>
63             <groupId>org.projectlombok</groupId>
64             <artifactId>lombok</artifactId>
65             <optional>true</optional>
66         </dependency>
67 
68         <dependency>
69             <groupId>org.springframework.boot</groupId>
70             <artifactId>spring-boot-starter-test</artifactId>
71             <scope>test</scope>
72         </dependency>
73 
74     </dependencies>
75 
76     <build>
77         <finalName>test-springcloud-provider-payment8004</finalName>
78     </build>
79 
80 </project>

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 <!-- spring-cloud 整合 zookeeper -->
 2 <dependency>
 3     <groupId>org.springframework.cloud</groupId>
 4     <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
 5     <!-- 排除自带的zookeeper jar包 -->
 6     <exclusions>
 7         <exclusion>
 8             <groupId>org.apache.zookeeper</groupId>
 9             <artifactId>zookeeper</artifactId>
10         </exclusion>
11     </exclusions>
12 </dependency>
13 
14 <!-- zookeeper 引入对应版本的zookeeper -->
15 <dependency>
16     <groupId>org.apache.zookeeper</groupId>
17     <artifactId>zookeeper</artifactId>
18     <version>3.6.0</version>
19     <exclusions>
20         <exclusion>
21             <groupId>org.slf4j</groupId>
22             <artifactId>slf4j-log4j12</artifactId>
23         </exclusion>
24         <exclusion>
25             <groupId>log4j</groupId>
26             <artifactId>log4j</artifactId>
27         </exclusion>
28     </exclusions>
29 </dependency>

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

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

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是否相同

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

搭建服务消费者

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

项目结构如下:

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

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


http://lihuaxi.xjx100.cn/news/1475334.html

相关文章

STM32 进不了main 函数

1. 我用的是STM32L151C8T6 的芯片&#xff0c;在github 上找了个别人的例程&#xff0c;拿来当模板改&#xff0c;由于他用的是HSE 外部晶振&#xff0c;我用的是内部晶振HSI&#xff0c;所以需要改系统时钟&#xff0c;改完后debug&#xff0c; 一直进不了main 函数&#xff0…

Diffusion Models for Time Series Applications: A Survey

Diffusion Models for Time Series Applications: A Survey (Paper reading) Lequan Lin, The University of Sydney Business School, arXiv2023,Cited:5, Code, Paper 1. 引言 扩散模型是一类基于深度学习的生成模型&#xff0c;在前沿的机器学习研究中变得越来越突出。在…

Nebula基础的查询操作介绍

Nebula基础的查询操作介绍 这里只是对Nebula基础查询进行介绍&#xff0c;其目的是为了让未接触过Nebula的同学最短时间了解其语句。更详细更准确的内可以查看官方文档。 docs.nebula-graph 关于查询这里并没有使用官方例子数据&#xff0c;而是自己实际尝试了文档中的语句。 …

CTFshow 限时活动 红包挑战9 详细题解

CTFshow红包挑战9 题目源码开源了。源码如下&#xff1a; common.php <?phpclass user{public $id;public $username;private $password;public function __toString(){return $this->username;}}class cookie_helper{private $secret "*************"; /…

html5提供的FileReader是一种异步文件读取文件中的数据

前言&#xff1a;FileReader是一种异步文件读取机制&#xff0c;结合input:file可以很方便的读取本地文件。 input:file 在介绍FileReader之前&#xff0c;先简单介绍input的file类型。 <input type"file" id"file"> input的file类型会渲染为一个按…

C++ 网络编程项目fastDFS分布式文件系统(六)--qt(client)+login

目录 1. 登录和注册协议 1.1 注册协议 1.2 登录协议 2. 单例模式 1. 登录和注册协议 1.1 注册协议 # URL http://192.168.1.100:80/reg # post数据格式 { userName:xxxx, nickName:xxx, firstPwd:xxx, phone:xxx, email:xxx } 2. 服务器端 - Nginx 服务器端的配置。 loc…

【开发】tips:视频汇聚/视频云存储/视频监控管理平台EasyCVR如何提升网络稳定

安防视频监控/视频集中存储/云存储/磁盘阵列EasyCVR平台可拓展性强、视频能力灵活、部署轻快&#xff0c;可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等&#xff0c;以及支持厂家私有协议与SDK接入&#xff0c;包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安…

前端面试:【前端工程化】自动化测试工具 Jest、Mocha 和 Cypress

嗨&#xff0c;亲爱的前端开发者&#xff01;在构建高质量的前端应用程序时&#xff0c;自动化测试是不可或缺的一部分。本文将深入探讨三个流行的前端自动化测试工具&#xff1a;Jest、Mocha 和 Cypress&#xff0c;以帮助你了解它们的用途、特点以及如何选择适合你项目的测试…