SpringCloud学习笔记(四)_ZooKeeper注册中心
基于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: 80044 5 spring:6 application:7 name: cloud-payment-service8 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 @RestController2 @Slf4j3 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: 79994 5 spring:6 application:7 name: cloud-order8 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 @Configuration2 public class AppConfig {3 4 /**5 * 注入restTemplate,请用请求rest接口6 * @return7 */8 @Bean9 // 标注此注解后,RestTemplate就具有了客户端负载均衡能力
10 // 负载均衡技术依赖于的是Ribbon组件~
11 // RestTemplate都塞入一个loadBalancerInterceptor 让其具备有负载均衡的能力
12 @LoadBalanced
13 public RestTemplate restTemplate(){
14 return new RestTemplate();
15 }
16 }
6、编辑Controller
1 @RestController2 @Slf4j3 public class OrderController {4 5 public static final String PAYMENT_URL = "http://cloud-payment-service";6 7 @Autowired8 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服务器查看
相关文章:

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

【算法专题突破】双指针 - 移动零(1)
目录 写在前面 1. 题目解析 2. 算法原理 3. 代码编写 写在最后: 写在前面 在进行了剑指Offer和LeetCode hot100的毒打之后, 我决心系统地学习一些经典算法,增强我的综合算法能力。 1. 题目解析 题目链接:283. 移动零 - 力…...

Nginx高可用集群
目录 一.简介二.案例1.实现思路2.配置文件修改3.实现效果故障转移机制 一.简介 以提高应用系统的可靠性,尽可能地减少中断时间为目标,确保服务的连续性,达到高可用的容错效果。例如“故障切换”、“双机热备”、“多机热备”等都属于高可用集…...
Rust 基础入门 ——所有权 引言 :垃圾自动回收机制的缺陷。
在以往,内存安全几乎都是通过 GC 的方式实现,但是 GC 会引来性能、内存占用以及 Stop the world 等问题,在高性能场景和系统编程上是不可接受的, 我们先介绍一下这些概念都是什么: 内存安全是指程序在运行过程中不会访…...

Ubuntu20.04安装软件报错:The following packages have unmet dependencies
Ubuntu20.04更换阿里云源后安装软件都会报错:The following packages have unmet dependencies 查看资料,大概是ubuntu本身的源比较版本较老,而阿里云的源比较新,因此版本不匹配造成依赖的库不匹配,所以只要将阿里云的…...
Java 与设计模式(12):享元模式
一、定义 享元模式是一种结构型设计模式,旨在有效地共享对象以减少内存使用和提高性能。该模式的核心思想是通过共享尽可能多的相似对象来减少内存占用。它将对象分为可共享的内部状态和不可共享的外部状态。内部状态是对象的固有属性,可以在多个对象之…...
React配置代理(proxy)
使用axios进行请求,而配置代理过程。 第一种 在package.json中,添加proxy配置项,之后所有的请求都会指向该地址 但这种方法只能配置一次,也只有一个 示例: "proxy":"https://localhost:5000" 添加后&am…...

队列(Queue):先进先出的数据结构队列
栈与队列https://blog.csdn.net/qq_45467165/article/details/127958960?csdn_share_tail%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22127958960%22%2C%22source%22%3A%22qq_45467165%22%7D 队列(Queue)是一种常见的线…...

CentOS ens160 显示disconnected
使用nmcli device查看网卡状态,显示如图: 检查宿主机系统VMware DHCP Sevice和VMware NAT Sevice服务是否正常运行。 右键点击我的电脑管理按钮,打开计算机管理点击服务...

使用 ChatGPT 创建 PowerPoint 演示文稿
让 ChatGPT 成为您的助手来帮助您编写电子邮件很简单,因为众所周知,它非常能够生成文本。很明显,ChatGPT 无法帮助您做饭。但您可能想知道它是否可以生成文本以外的其他内容。在上一篇文章中,您了解到 ChatGPT 只能通过中间语言为您生成图形。在这篇文章中,您将了解使用中…...

matlab将数组值划分为两类
例如:大于0的处理为1,小于0的处理为-1. 当然,可以选择循环结构和选择结构,但是效率会很低。 这里直接使用逻辑语句完成。 % 不使用循环语句,将数组内值划分为两类 clc; clearvars; a[-0.1422 , -0.0433 , 0.1131 …...

【点击新增一个下拉框 与前一个内容一样 但不能选同一个值】
点击新增一个下拉框 与前一个内容一样 但不能选同一个值 主要是看下拉选择el-option的disabled,注意不要混淆 <el-form label-width"120px" :model"form" ref"form" style"color: #fff"><template v-for"(trapolicy, i…...

【Gitee提交pr】
Gitee提交pr 什么是pr怎样提交一个pr嘞? 什么是pr pr:指的是将自己的修改从自己的账号仓库dev下提交到官方账号仓库master下; 通俗来讲就是Gitee线上有属于自己的分支,然后本地在自己地分支修改完代码之后,提交到自己的线上分支&a…...

一款打工人必备的电脑端自律软件!!冲鸭打工人!!
你!有没有渴望进步!! 你!有没有渴望变强!!! 成为大佬!!!超越巨佬!!! 这就是一款为这样的你量身定做的程序:输入…...

【Vue框架】 router和route是什么关系
前言 之前没太注意,写着写着突然发现它们貌似不太一样,记录以下,回顾的看总结就好。 1、总结✨ route:当前激活路由的对象,用于访问和操作当前路由的信息 router:管理多个route的对象,整个应…...

整理mongodb文档:聚合管道
个人博客 整理mongodb文档:聚合管道 个人博客,求关注,电脑版看体验更加,如果不够清晰,请指出来,谢谢 文章概叙 文章主要通过几个常用的聚合表达式来介绍聚合管道的使用,以及从索引的角度来介绍聚合管道…...

Delphi 11.3 FMX 多设备平台中使用 TGrid 实现类似 TDBGrid 的效果
Delphi Firemonkey 中 TDBGrid 这个控件已经没有了。如何实现类似这个效果呢。其实可以用TGrid 来实现。以下用 11.3 来讲解。 查询里面用到的 connection 和 query 等控件那些一般的数据库用法,就不做过多描述了。请参考其他资料。 方法一.通过界面配置来实现 在…...
Qt-事件循环与QtConcurrent、QThread结合使用时注意的点
QEventLoop和QtConcurrent可以结合使用达到主线程ui不阻塞同步执行的效果,但是要小心避坑,查看如下代码: QEventLoop loop; QtConcurrent::run([&]() {doSomething();loop.quit(); }); loop.exec();上述写法存在两个问题: Q…...
基于MongoDB的空间数据存储与查询
一、概念说明 1.1 空间地理数据 MongoDB 中使用 GeoJSON对象 或 坐标对 描述空间地理数据。MongoDB使用 WGS84 参考系进行地理空间数据查询。 1、MongoDB支持空间数据的存储,数据类型需要限制为GeoJSON; 2、MongoDB可以为GeoJSON类型数据建立索引,提升空…...
jquery中pdf的上传、下载及excel导出
jquery中pdf的上传、下载及excel导出 1.PDF上传 pdfUpload2. pdf下载和excel导出用的一种方法,并且需要引入utils.js2.1PDF下载 pdfDownload2.2导出Excel excelExport 1.PDF上传 pdfUpload //PDF上传 pdfUpload window.pdfUploadfunction (obj){layer.open({type:…...

国防科技大学计算机基础课程笔记02信息编码
1.机内码和国标码 国标码就是我们非常熟悉的这个GB2312,但是因为都是16进制,因此这个了16进制的数据既可以翻译成为这个机器码,也可以翻译成为这个国标码,所以这个时候很容易会出现这个歧义的情况; 因此,我们的这个国…...

Chapter03-Authentication vulnerabilities
文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

376. Wiggle Subsequence
376. Wiggle Subsequence 代码 class Solution { public:int wiggleMaxLength(vector<int>& nums) {int n nums.size();int res 1;int prediff 0;int curdiff 0;for(int i 0;i < n-1;i){curdiff nums[i1] - nums[i];if( (prediff > 0 && curdif…...

Reasoning over Uncertain Text by Generative Large Language Models
https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829 1. 概述 文本中的不确定性在许多语境中传达,从日常对话到特定领域的文档(例如医学文档)(Heritage 2013;Landmark、Gulbrandsen 和 Svenevei…...

Docker 本地安装 mysql 数据库
Docker: Accelerated Container Application Development 下载对应操作系统版本的 docker ;并安装。 基础操作不再赘述。 打开 macOS 终端,开始 docker 安装mysql之旅 第一步 docker search mysql 》〉docker search mysql NAME DE…...

GruntJS-前端自动化任务运行器从入门到实战
Grunt 完全指南:从入门到实战 一、Grunt 是什么? Grunt是一个基于 Node.js 的前端自动化任务运行器,主要用于自动化执行项目开发中重复性高的任务,例如文件压缩、代码编译、语法检查、单元测试、文件合并等。通过配置简洁的任务…...

Linux基础开发工具——vim工具
文章目录 vim工具什么是vimvim的多模式和使用vim的基础模式vim的三种基础模式三种模式的初步了解 常用模式的详细讲解插入模式命令模式模式转化光标的移动文本的编辑 底行模式替换模式视图模式总结 使用vim的小技巧vim的配置(了解) vim工具 本文章仍然是继续讲解Linux系统下的…...
shell脚本质数判断
shell脚本质数判断 shell输入一个正整数,判断是否为质数(素数)shell求1-100内的质数shell求给定数组输出其中的质数 shell输入一个正整数,判断是否为质数(素数) 思路: 1:1 2:1 2 3:1 2 3 4:1 2 3 4 5:1 2 3 4 5-------> 3:2 4:2 3 5:2 3…...

【记录坑点问题】IDEA运行:maven-resources-production:XX: OOM: Java heap space
问题:IDEA出现maven-resources-production:operation-service: java.lang.OutOfMemoryError: Java heap space 解决方案:将编译的堆内存增加一点 位置:设置setting-》构建菜单build-》编译器Complier...
AWS vs 阿里云:功能、服务与性能对比指南
在云计算领域,Amazon Web Services (AWS) 和阿里云 (Alibaba Cloud) 是全球领先的提供商,各自在功能范围、服务生态系统、性能表现和适用场景上具有独特优势。基于提供的引用[1]-[5],我将从功能、服务和性能三个方面进行结构化对比分析&#…...