如何快速推广网站,wordpress子站点打不开,公众号与网站,青岛网站建设与推广从浅入深 学习 SpringCloud 微服务架构#xff08;三#xff09;注册中心 Eureka#xff08;3#xff09;
段子手168
1、eureka#xff1a;高可用的引入
Eureka Server 可以通过运行多个实例并相互注册的方式实现高可用部署#xff0c; Eureka Server 实例会彼此增量地…从浅入深 学习 SpringCloud 微服务架构三注册中心 Eureka3
段子手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个 启动类urekaServerApplicationurekaServerApplication(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?useUnicodetruecharacterEncodingutf8url: jdbc:mysql://localhost:3306/shop?useUnicodetruecharacterEncodingutf8useSSLfalseserverTimezoneAsia/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?useUnicodetruecharacterEncodingutf8url: jdbc:mysql://localhost:3306/shop?useUnicodetruecharacterEncodingutf8useSSLfalseserverTimezoneAsia/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 地址注册
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?useUnicodetruecharacterEncodingutf8url: jdbc:mysql://localhost:3306/shop?useUnicodetruecharacterEncodingutf8useSSLfalseserverTimezoneAsia/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 地址)。
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?useUnicodetruecharacterEncodingutf8url: jdbc:mysql://localhost:3306/shop?useUnicodetruecharacterEncodingutf8useSSLfalseserverTimezoneAsia/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秒。
3再次运行 product_service, EurekaServerApplication(1) 测试
浏览器地址栏输入http://localhost:9000 输出界面如下 如果把 product_service 子工程的 启动类停掉再次刷新网页查看 只有几十秒Eureka 上就没有了 product_service 服务。 5、eurekaServer高可用自我保护机制
1EurekaServer 默认是开启 自我保护机制的这样会在服务的控制台界面显示红色字体
如下界面 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 微服务架构三注册中心 Eureka2