SpringCloud学习之服务高可用
在上一篇SpringCloud学习之服务注册与发现文章中使用的是单节点的服务注册中心,但在实际应用中应该考虑发生故障的情况,一台服务出现了故障应该还有其他服务存在,这时候就需要服务的高可用了,在Eureka的服务治理设计中,所有的节点即是服务提供方,也是服务消费方,服务注册中心也不例外。
Eureka Server的高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就形成了一组互相注册的服务注册中心了,以实现服务清单的互相同步,达到高可用的效果,如下图所示
![](https://img.haomeiwen.com/i7892353/dc93e958705234a9.png)
在上一章节SpringCloud学习之服务注册与发现所写的server中复制一份,分别设为ServerApplication1与ServerApplication2,通过端口区分,ServerApplication1中在VM options中通过输入-Dserver.port=8761设置端口为为8761,ServerApplication2中设为8762,点击下方Apply保存。
也可以自己新建一个server2,在application.yml中设置端口
![](https://img.haomeiwen.com/i7892353/eab453819da83475.png)
将application.yml配置改为如下:
eureka:
client:
register-with-eureka: false #只作为服务端
service-url:
defaultZone : http://localhost:8762/eureka #注册服务器地址
server:
enable-self-preservation: false #关掉红字警告 开发环境关闭
spring:
application:
name: eureka #应用名
启动ServerApplication1,让ServerApplication1向8762端口注册,访问http://localhost:8761/发现是没问题的。
将application.yml配置改为如下:
eureka:
client:
register-with-eureka: false #只作为服务端
service-url:
defaultZone : http://localhost:8761/eureka #注册服务器地址
server:
enable-self-preservation: false #关掉红字警告 开发环境关闭
spring:
application:
name: eureka #应用名
启动ServerApplication2,让ServerApplication2向8761端口注册,访问http://localhost:8762/发现是client也已经注册上去了,如下图所示
![](https://img.haomeiwen.com/i7892353/701c6f053003ba1d.png)
这时当我们停止ServerApplication1之后,再次访问http://localhost:8762/发现client还在,说明高可用实现了。但这时当我们重启client时是会报错的,因为我们client的配置还是如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注册服务器地址
instance:
hostname: client
spring:
application:
name: client #应用名
这时我们需要修改下我们的client的配置改为向两个注册中心都注册,修改如下:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka #注册服务器地址
instance:
hostname: client
spring:
application:
name: client #应用名
这时再启动client就没问题了。
当我们有三个服务注册中心的时候就需要两两注册了,如下图所示
![](https://img.haomeiwen.com/i7892353/346276e473864d7d.png)
将ServerApplication1的配置改为如下,同时想8762和8763注册,然后启动
eureka:
client:
register-with-eureka: false #只作为服务端
service-url:
defaultZone : http://localhost:8762/eureka,http://localhost:8763/eureka #注册服务器地址
server:
enable-self-preservation: false #关掉红字警告 开发环境关闭
spring:
application:
name: eureka #应用名
将ServerApplication2的配置改为如下,同时想8761和8763注册,然后启动
eureka:
client:
register-with-eureka: false #只作为服务端
service-url:
defaultZone : http://localhost:8761/eureka,http://localhost:8763/eureka #注册服务器地址
server:
enable-self-preservation: false #关掉红字警告 开发环境关闭
spring:
application:
name: eureka #应用名
将ServerApplication3的配置改为如下,同时想8761和8762注册,然后启动
eureka:
client:
register-with-eureka: false #只作为服务端
service-url:
defaultZone : http://localhost:8761/eureka,http://localhost:8762/eureka #注册服务器地址
server:
enable-self-preservation: false #关掉红字警告 开发环境关闭
spring:
application:
name: eureka #应用名
将client的配置改为如下,然后重新启动
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka #注册服务器地址
instance:
hostname: client
spring:
application:
name: client #应用名
这时访问http://localhost:8761/,http://localhost:8762/,http://localhost:8763/ 发现都没问题。
更多技术文章可关注个人公众号: 码农Fly
![](https://img.haomeiwen.com/i7892353/8289a855c9b3452f.jpg)