# 从浅入深 学习 SpringCloud 微服务架构(三)注册中心 Eureka(3)

news/2024/10/20 7:16:45/

Eureka3_2">从浅入深 学习 SpringCloud 微服务架构(三)注册中心 Eureka(3)

段子手168

1、eureka:高可用的引入

Eureka Server 可以通过运行多个实例并相互注册的方式实现高可用部署,
Eureka Server 实例会彼此增量地同步信息,从而确保所有节点数据一致。
事实上,节点之间相互注册是 Eureka Server 的默认行为。

2、eurekaServer 高可用:server 间的相互注册

在这里插入图片描述

1)修改 eureka_server 子工程(子模块)中的 application.yml 文件

模拟两个 EurekaServer, 一个端口 9000,一个端口 8000,两个需要相互注册。

## C:\java-test\idea2019\spring_cloud_demo\eureka_service\src\main\resources\application.yml# 模拟两个 EurekaServer, 一个端口 9000,一个端口 8000,两个需要相互注册。server:port: 9000  # 启动端口 命令行注入。spring:application:name: service-eureka  #spring应用名, # 注意 FeignClient 不支持名字带下划线eureka: # 配置 eureka_server
#  instance:
#    hostname: localhostclient:
#    register-with-eureka: false  # 是否将自己注册到注册中心,不配置时,默认 true
#    fetch-registry: false  # 是否从 Eureka 中获取注册信息,不配置时,默认 trueservice-url: # 配置暴露给 EurekaClient 的请求地址
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://127.0.0.1:8000/eureka/
## C:\java-test\idea2019\spring_cloud_demo\eureka_service\src\main\resources\application.yml# 模拟两个 EurekaServer, 一个端口 9000,一个端口 8000,两个需要相互注册。server:port: 8000  # 启动端口 命令行注入。spring:application:name: service-eureka  #spring应用名, # 注意 FeignClient 不支持名字带下划线eureka: # 配置 eureka_server
#  instance:
#    hostname: localhostclient:
#    register-with-eureka: false  # 是否将自己注册到注册中心,不配置时,默认 true
#    fetch-registry: false  # 是否从 Eureka 中获取注册信息,不配置时,默认 trueservice-url: # 配置暴露给 EurekaClient 的请求地址
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://127.0.0.1:9000/eureka/

2)打开 idea 的 【Run Dashboard】 运行仪表面板。

复制一个 EurekaServerApplication.java 启动类,命名为:EurekaServerApplication(1)

注意:
如果在 idea 中找不到 【Run Dashboard】 运行仪表面板,可以看如下文章:

# IDEA2019 如何打开 Run Dashboard 运行仪表面板

在这里插入图片描述

3)运行 2个 启动类(urekaServerApplication,urekaServerApplication(1) ),进行测试

浏览器地址栏输入:http://localhost:9000 输出界面如下:

在这里插入图片描述

浏览器地址栏输入:http://localhost:8000 输出界面如下:

在这里插入图片描述

3、eurekaServer 高可用:服务注册到多个 eurekaserver

1)运行 order_service, product_service 子工程的 启动类,

浏览器地址栏输入:http://localhost:9000 输出界面如下:

在这里插入图片描述

浏览器地址栏输入:http://localhost:8000 输出界面如下:

在这里插入图片描述

2)修改 product_service 子工程的 application.yml 文件,

添加 注册到多个 eurekaserver 服务 配置。


## C:\java-test\idea2019\spring_cloud_demo\product_service\src\main\resources\application.ymlserver:port: 9001  # 启动端口 命令行注入。
#  port: ${port:56010}  # 启动端口设置为动态传参,如果未传参数,默认端口为 56010
#  servlet:
#    context-path: /application1spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册

3)修改 order_service 子工程的 application.yml 文件,

添加 注册到多个 eurekaserver 服务 配置。


## C:\java-test\idea2019\spring_cloud_demo\order_service\src\main\resources\application.ymlserver:port: 9002  # 启动端口 命令行注入。
#  port: ${port:9002}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9002
#  servlet:
#    context-path: /application1spring:application:name: service-order  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册

EurekaServerApplicationEurekaServerApplication1_200">4)再次运行 order_service, product_service, EurekaServerApplication,EurekaServerApplication(1)

可以宕机 EurekaServerApplication,测试 其他运行是否正常,有无影响。

浏览器地址栏输入:http://localhost:9000 输出界面如下:

在这里插入图片描述

浏览器地址栏输入:http://localhost:8000 输出界面如下:

在这里插入图片描述

浏览器地址栏输入:http://localhost:9001/product/1 输出界面如下:

在这里插入图片描述

浏览器地址栏输入:http://localhost:9002/order/buy/1 输出界面如下:

在这里插入图片描述

4、eurekaServer 高可用:显示 IP 与服务续约时间设置

1)修改 eureka_service 子工程的 application.yml 文件,

取消配置高可用 EurekaServer 服务。


## C:\java-test\idea2019\spring_cloud_demo\eureka_service\src\main\resources\application.yml# 模拟两个 EurekaServer, 一个端口 9000,一个端口 8000,两个需要相互注册。server:port: 9000  # 启动端口 命令行注入。spring:application:name: service-eureka  #spring应用名, # 注意 FeignClient 不支持名字带下划线eureka: # 配置 eureka_server
#  instance:
#    hostname: localhostclient:register-with-eureka: false  # 是否将自己注册到注册中心,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 truefetch-registry: false  # 是否从 Eureka 中获取注册信息,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 trueservice-url: # 配置暴露给 EurekaClient 的请求地址
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://127.0.0.1:9000/eureka/  # 配置高可用时,须配置为另一个 EurekaServerApplication 的端口号,如:8000

2)修改 product_service 子工程的 application.yml 文件,

添加 在服务提供者通过 eureka.instance.instance-id 配置,在控制台显示服务 IP 地址。


## C:\java-test\idea2019\spring_cloud_demo\product_service\src\main\resources\application.ymlserver:port: 9001  # 启动端口 命令行注入。
#  port: ${port:56010}  # 启动端口设置为动态传参,如果未传参数,默认端口为 56010
#  servlet:
#    context-path: /application1spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/
#      defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 高可用,注册多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。

EurekaServerApplication1__296">3)再次运行 product_service, EurekaServerApplication(1) 测试

浏览器地址栏输入:http://localhost:9000 输出界面如下:

在这里插入图片描述

4)修改 product_service 子工程的 application.yml 文件,

添加 心跳间隔和续约时间 配置。


## C:\java-test\idea2019\spring_cloud_demo\product_service\src\main\resources\application.ymlserver:port: 9001  # 启动端口 命令行注入。
#  port: ${port:56010}  # 启动端口设置为动态传参,如果未传参数,默认端口为 56010
#  servlet:
#    context-path: /application1spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/
#      defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/  # 高可用,注册多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}  # 向注册中心注册服务的id(IP 地址)。lease-renewal-interval-in-seconds: 10  # 设置向注册中心每10秒发送一次心跳,告诉注册中心此服务没有岩机,此参数默认是30秒。lease-expiration-duration-in-seconds: 20  # 设置每20秒如果注册中心没收到此服务的心跳,就认为此服务岩机了,此参数默认是90秒。

EurekaServerApplication1__349">3)再次运行 product_service, EurekaServerApplication(1) 测试

浏览器地址栏输入:http://localhost:9000 输出界面如下:

在这里插入图片描述

如果把 product_service 子工程的 启动类停掉,再次刷新网页查看,
只有几十秒,Eureka 上就没有了 product_service 服务。

在这里插入图片描述

5、eurekaServer高可用:自我保护机制

EurekaServer___366">1)EurekaServer 默认是开启 自我保护机制的,这样会在服务的控制台界面显示红色字体,

如下界面:

在这里插入图片描述

2)修改 eureka_service 子工程的 application.yml 文件,

配置 关闭自我保护机制功能 和 剔除服务间隔。


## C:\java-test\idea2019\spring_cloud_demo\eureka_service\src\main\resources\application.yml# 模拟两个 EurekaServer, 一个端口 9000,一个端口 8000,两个需要相互注册。server:port: 9000  # 启动端口 命令行注入。spring:application:name: service-eureka  #spring应用名, # 注意 FeignClient 不支持名字带下划线eureka: # 配置 eureka_server
#  instance:
#    hostname: localhostclient:register-with-eureka: false  # 是否将自己注册到注册中心,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 truefetch-registry: false  # 是否从 Eureka 中获取注册信息,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 trueservice-url: # 配置暴露给 EurekaClient 的请求地址
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://127.0.0.1:9000/eureka/  # 配置高可用时,须配置为另一个 EurekaServerApplication 的端口号,如:8000server:enable-self-preservation: false  # 关闭自我保护机制eviction-interval-timer-in-ms: 4000  # 设置剔除服务间隔时间为 4000 毫秒(4秒)。此参数默认为 true。

3)浏览器地址栏输入:http://localhost:9000 输出界面如下:

在这里插入图片描述

上一节链接如下:
# 从浅入深 学习 SpringCloud 微服务架构(三)注册中心 Eureka(2)


http://www.ppmy.cn/news/1435867.html

相关文章

Vue.js之MVVM设计模式

前言 看到招聘信息网站上有对MVVM框架经验的需求,刚好曾有过这方面的笔记,在复习的同时总结核心知识点分析给大家。 MVVM是可以实现View和Model的完全分离,通过ViewModel这个桥梁进行交互,然后ViewModel通过双向数据绑定把View层和…

vsCode 配置 用户代码

适当配置自己的代码片段 , 减少coding 时间 , 释放copy双手 小白值得学习 配置方法 左下脚设置 --》 用户代码片段 -- 》 输入你需要配置的文件(比如输入jsx/vue) ,回车 编辑 -- 》 编辑示例参数 以及坑点 {"Print to console": {// &q…

OllyDbg 快捷键及常用法

keywords: debug, ollydbg 快捷键 Ctrl --> C Shift --> S Alt --> M 功能快捷键设置/取消断点F2执行到光标所在行F4步过F8步进F7运行F9暂停F12回到应用层M-F9打开文件F3重新调试C-F2打开应用程序输入表C-n寻找表达式C-g打开断点窗口M-b切换断点状态空格添加备注…

【IDEA】在IntelliJ IDEA中导入Eclipse项目:详细指南

IntelliJ IDEA和Eclipse是两款常用的集成开发环境(IDE),在软件开发中经常会遇到需要在它们之间迁移项目的情况。本文将重点介绍如何在IntelliJ IDEA中导入Eclipse项目,以帮助开发者顺利地迁移他们的项目,并在IntelliJ …

BBED 安装

–11G-23C 安装使用相同 1、解压 [root11g-fs oraback]$ unzip linux_bbed.zip [root11g-fs oraback]# cd linux_bbed/ [root11g-fs linux_bbed]# ll total 32 -rw-rw-r--. 1 root root 8704 Apr 20 2010 bbedus.msb -rw-rw-r--. 1 root root 10270 Jul 26 2000 bbedus.ms…

基于STM32的蓝牙小车(虚拟串口模拟)的Proteus仿真

文章目录 一、前言二、仿真图1.要求2.思路3.画图3.1 电源部分3.2 超声波测距部分3.3 电机驱动部分3.4 按键部分3.5 蓝牙部分3.6 显示屏部分3.7 整体 4.仿真5.软件 三、总结 一、前言 proteus本身并不支持蓝牙仿真,这里我采用虚拟串口的方式来模拟蓝牙控制。 这里给…

Nginx代理MinIO时出现“Access Denied“错误

MinIO与Nginx集成时&#xff0c;如果出现"Access Denied"错误&#xff0c;通常意味着Nginx代理的请求没有被正确地转发到MinIO服务。 出现错误&#xff1a; <Error><Code>AccessDenied</Code><Message>Access Denied.</Message><…

【第3节】“茴香豆“:搭建你的 RAG 智能助理

目录 1 基础知识1.1.RAG技术的概述1.2 RAG的基本结构有哪些呢&#xff1f;1.3 RAG 工作原理&#xff1a;1.4 向量数据库(Vector-DB )&#xff1a;1.5 RAG常见优化方法1.6RAG技术vs微调技术 2、茴香豆介绍2.1应用场景2.2 场景难点2.3 茴香豆的构建&#xff1a; 3 论文快读 1 基础…