从浅入深 学习 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: localhost

client:

# register-with-eureka: false # 是否将自己注册到注册中心,不配置时,默认 true

# fetch-registry: false # 是否从 Eureka 中获取注册信息,不配置时,默认 true

service-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: localhost

client:

# register-with-eureka: false # 是否将自己注册到注册中心,不配置时,默认 true

# fetch-registry: false # 是否从 Eureka 中获取注册信息,不配置时,默认 true

service-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.yml

server:

port: 9001 # 启动端口 命令行注入。

# port: ${port:56010} # 启动端口设置为动态传参,如果未传参数,默认端口为 56010

# servlet:

# context-path: /application1

spring:

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=utf8

url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai

username: root

password: 12311

jpa:

database: MySQL

show-sql: true

open-in-view: true

eureka: # 配置 Eureka

client:

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.yml

server:

port: 9002 # 启动端口 命令行注入。

# port: ${port:9002} # 启动端口设置为动态传参,如果未传参数,默认端口为 9002

# servlet:

# context-path: /application1

spring:

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=utf8

url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai

username: root

password: 12311

jpa:

database: MySQL

show-sql: true

open-in-view: true

eureka: # 配置 Eureka

client:

service-url:

defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/ # 多个 eurekaserver 用 , 隔开。

instance:

prefer-ip-address: true # 使用 ip 地址注册

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: localhost

client:

register-with-eureka: false # 是否将自己注册到注册中心,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 true

fetch-registry: false # 是否从 Eureka 中获取注册信息,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 true

service-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.yml

server:

port: 9001 # 启动端口 命令行注入。

# port: ${port:56010} # 启动端口设置为动态传参,如果未传参数,默认端口为 56010

# servlet:

# context-path: /application1

spring:

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=utf8

url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai

username: root

password: 12311

jpa:

database: MySQL

show-sql: true

open-in-view: true

eureka: # 配置 Eureka

client:

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 地址)。

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.yml

server:

port: 9001 # 启动端口 命令行注入。

# port: ${port:56010} # 启动端口设置为动态传参,如果未传参数,默认端口为 56010

# servlet:

# context-path: /application1

spring:

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=utf8

url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai

username: root

password: 12311

jpa:

database: MySQL

show-sql: true

open-in-view: true

eureka: # 配置 Eureka

client:

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秒。

5)再次运行 product_service, EurekaServerApplication(1) 测试

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

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

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

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: localhost

client:

register-with-eureka: false # 是否将自己注册到注册中心,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 true

fetch-registry: false # 是否从 Eureka 中获取注册信息,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 true

service-url: # 配置暴露给 EurekaClient 的请求地址

# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

defaultZone: http://127.0.0.1:9000/eureka/ # 配置高可用时,须配置为另一个 EurekaServerApplication 的端口号,如:8000

server:

enable-self-preservation: false # 关闭自我保护机制

eviction-interval-timer-in-ms: 4000 # 设置剔除服务间隔时间为 4000 毫秒(4秒)。此参数默认为 true。

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

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

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: