0


【Springcloud篇】学习笔记二(四至六章):Eureka、Zookeeper、Consul

第四章_Eureka服务注册与发现

1.Eureka基础知识

1.1Eureka工作流程-服务注册

image-20230907225740321

1.2Eureka两大组件

image-20230907230812361

2.单机Eureka构建步骤

  • IDEA生成EurekaServer端服务注册中心,类似于物业公司
  • EurekaClient端cloud-provider-payment8081将注册进EurekaServer成为服务提供者provider,类似尚硅谷对外提供授课服务
  • EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer,类似来上课的各位同学

2.1IDEA生成EurekaServer端服务注册中心

(1)pom文件引入

image-20230908212248764

(2)yml配置文件

image-20230908212339646

(3)主启动

image-20230908212358935

(4)测试

运行localhost:7001

2.2EurekaClient端cloud-provider-payment8081将注册进EurekaServer

(1)pom

image-20230908213212902

(2)改yml和主启动

image-20230908213420503

image-20230908214435053

(3)测试

运行localhost:7001,观察到支付模块已经注册到Eureka服务注册中心

image-20230908214101436

2.3EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer

(1)pom

image-20230908231331980

(2)改yml和主启动

image-20230908231356303

(3)测试

image-20230908224807687

2.4工作流程

image-20230908225038280

3.集群Eureka构建步骤

3.1Eureka集群原理说明

(1)问题引入

image-20230908224926945

image-20230908225107902

3.2EurekaServer集群环境构建步骤

(1)修改映射配置添加进hosts文件

image-20230908225413313

image-20230908225655965

(2)修改服务7001和7002的yml配置文件
  • 7001

image-20230908230111621

  • 7002

image-20230908231226006

(3)测试

image-20230908231151885

3.3将支付8081和订单80服务发布到2台Eureka集群配置中——改yml

image-20230908232134560

3.4测试01

启动顺序

  • 启动EurekaServer,7001/7002服务
  • 再启动服务提供者,8001
  • 再启动消费者,80

测试之前的网站

3.5支付8001集群环境构建

(1)思路

image-20230909102352656

(2)新建支付服务8002

并将支付服务8001代码复制粘贴到支付服务8002中,再修改相关yml中端口名称和主启动类名称即可

(3)修改支付服务8001/8002的Controller

image-20230909101800555

(4)bug
①修改订单80服务远程调用的地址

image-20230909102039146

②使用@LoadBalanced注解赋予RestTemplate负载均衡能力

image-20230909102327154

(5)测试
  • 先启动EurekaServer,即7001/7002服务
  • 再启动服务提供者,即支付8001/8002服务、最后启动订单80服务
  • 测试:localhost/consumer/payment/get/3
  • 结果:8001/8002端口交替出现、负载均衡效果达到

image-20230909100043584

image-20230909100001608

4.Actuator微服务信息完善

  • 涉及的jar包

image-20230909104731604

4.1主机名称:服务名称修改

  • 存在的问题:Eureka显示主机名称(不安全)image-20230909103905524
  • 修改:image-20230909104342019
  • 效果:image-20230909104235974

4.2访问信息需配置有IP显示

image-20230909104508410

image-20230909104617070

5.服务发现Discovery

服务发现:对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息

5.1服务发现步骤

(1)修改支付服务8001的controller

引入discoverClient,即服务发现客户端

image-20230909110307597

(2)主启动添加注解@EnableDiscoveryClient

image-20230909110349338

  • 说明

image-20230909110636308

6.Eureka自我保护

6.1概述

image-20230909110753223

即:某时刻某一个微服务不可用了的时候,Eureka不会立刻清理,依旧会对微服务的信息进行保存

属于CAP里面的AP分支

image-20230909111258872

6.2怎么禁止自我保护

(1)注册中心EurekaServer70001

image-20230909113107238

  • 效果

image-20230909112111458

(2)生产者客户端EurekaClient端8001

image-20230909112612065

效果:

image-20230909112926832

  • 关闭8001服务后

image-20230909113018158

第五章_Zookeeper服务注册与发现

1.Zookeeper概述

Zookeeper是一个分布式协调工具,可以实现注册中心功能

关闭Linux服务器防火墙后启动zookeeper服务器

zookeeper服务器取代Eureka服务器,zk作为服务注册中心

2.Linux新增JDK1.8环境并安装Zookeeper服务

  • 首先,在主机下载JDK1.8和Zookeeper相关jar包的压缩包
  • 第二,通过Xftp5软件将jar包的压缩包传到Linux上(路径为/usr/local),再解压image-20230918091414367
  • 第三,配置Java和zookeeper的环境变量(修改文件/etc/profile,新增以下内容)

image-20230910195530154

  • 第四,配置zookeeper服务注册中心的配置文件(进入zookeeper安装路径)image-20230918091617235该文件新增以下内容:image-20230918091739057
  • 第五,启动zookeeper并查看状态

image-20230918091929726

4.主机连接zookeeper

(1)pom文件—注意删掉Eureka相关依赖,否则会报两个服务注册中心错误
<dependencies><!-- 引入自己定义的api通用包,可以使用payment对象--><dependency><groupId>com.peng.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Springboot整合zookeeper客户端--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-discovery</artifactId><!-- 先排除自带的zookeeper3.5.3 ,否则会和linux安装的3.8.2版本产生冲突--><exclusions><exclusion><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId></exclusion></exclusions></dependency><!-- linux安装的zookeeper3.8.2版本--><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.8.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
(2)application.yml

image-20230918092243336

(3)主启动

image-20230918092307072

(4)测试

image-20230918092414362

  • zookeeper发现注册到服务中心的服务

使用命令:./zkCli.sh 进入zookeeper客户端

image-20230918092638001

  • 通过service的id流水号查看注册进zookeeper的服务详细信息image-20230918093948911image-20230918093850106

5.注册进Zookeeper的节点为临时节点

主动停掉主机的java程序后,过段时间注册进zookeeper的服务消失

image-20230918094520609

6.新建消费者模块—cloud-consumerzk-order-80

(1)pom
<dependencies><!-- 引入自己定义的api通用包,可以使用payment对象--><dependency><groupId>com.peng.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Springboot整合zookeeper客户端--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-discovery</artifactId><!-- 先排除自带的zookeeper3.5.3 ,否则会和linux安装的3.8.2版本产生冲突--><exclusions><exclusion><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId></exclusion></exclusions></dependency><!-- linux安装的zookeeper3.8.2版本--><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.8.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
(2)yml

image-20230918113648023

(3)配置类—引入restTemplate

image-20230918113659700

(4)主启动

image-20230918113800047

(5)业务类(通过80端口调用8004端口服务)

image-20230918113830626

(6)测试
  • 测试一:cloud-consumer-order服务注册进zookeeper服务注册中心(成功)image-20230918113901513
  • 测试二:通过80端口调用8004端口(失败)image-20230918114031309

第六章_Confsul

1.Consul简介

(1)Consul是什么

image-20230918101447982

(2)Consul能做什么

image-20230918101557151

(3)Consul中文文档

Spring Cloud Consul 中文文档 参考手册 中文版

2.安装并运行Consul

(1)下载地址

Install | Consul | HashiCorp Developer

选中window的amd64版本进行安装

image-20230918193812756

(2)解压缩包,并打开cmd

image-20230918193901744

  • 验证consul是否安装成功image-20230918193938987
  • 启动consulimage-20230918194044100
  • 输入localhost:8500,即可看到consul注册中心页面

3.服务提供者

新建module工程:cloud-providerconsul-payment8006

(1)pom文件
<dependencies><!-- 引入自己定义的api通用包,可以使用payment对象--><dependency><groupId>com.peng.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><!--springcloud consul server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><!-- springboot整合Web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 日常通用jar包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
(2)yml文件

image-20230918200903058

(3)主启动

image-20230918200917415

(4)业务类

image-20230918200926412

(5)测试一

image-20230918200957813

(6)测试二

image-20230918195727306

4.服务消费者

(1)pom文件
<dependencies><!-- 引入自己定义的api通用包,可以使用payment对象--><dependency><groupId>com.peng.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><!--springcloud consul server--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><!-- springboot整合Web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 日常通用jar包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
(2)yml文件

image-20230918202850509

(3)主启动
(4)业务类

image-20230918202949006

(5)测试一:成功

image-20230918202215114

(6)测试二:失败

image-20230918203031857

5.三个注册中心异同点

image-20230918211433116

(1)CAP
  • C:Consistency(强一致性)
  • A:Availability(可用性)
  • P:Partition(分区容错性)

CAP理论关注粒度是数据,而不是

(2)经典CAP图
  • AP(Eureka)
  • CP(Zookeeper/Consul)
(3)AP

image-20230918211526686

(4)CP

image-20230918211230699


本文转载自: https://blog.csdn.net/weixin_61440595/article/details/135932205
版权归原作者 光久li 所有, 如有侵权,请联系我们删除。

“【Springcloud篇】学习笔记二(四至六章):Eureka、Zookeeper、Consul”的评论:

还没有评论