springcloud~搭建高可用注册中心

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/han_xiaoxue/article/details/79443227

用途

在实际生产环境下单点的注册中心是存在缺陷的,当该节点宕机后,依赖他服务的微服务将出现故障,所以就需要一个高可用的注册中心来弥补这种缺陷。Eureka通过“伙伴”机制实现高可用。每一台Eureka都需要在配置中指定另一个Eureka的地址作为伙伴,Eureka启动时会向自己的伙伴节点获取当前已经存在的注册列表, 这样在向Eureka集群中新加机器时就不需要担心注册列表不完整的问题。

项目架构

这里写图片描述

配置文件

总配置application.properties

spring.profiles.active=slave

##使用ip地址的形式定义注册中心的地址
eureka.instance.prefer-ip-address=true
#禁用自我保护模式
eureka.server.enable-self-preservation=false

###eureka server 启用安全验证
security.user.name=admin
security.user.password=123456
security.basic.enabled=true

#不向注册中心注册自己
#eureka.client.register-with-eureka=false
#注册中心的职责就是去维护服务实例,不需要去检索服务
#eureka.client.fetch-registry=false

##服务续约任务的调用间隔时间默认为30s
#eureka.instance.lease-renewal-interval-in-seconds=3
###定义服务失效的时间默认是90s
#eureka.instance.lease-expiration-duration-in-seconds=540

注册中心1的配置application-slave.properties

spring.application.name=springcloud-eureka-server
server.port=8766

eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.hostname=${spring.cloud.client.ipAddress}
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8765/eureka/
eureka.instance.status-page-url-path=/

注册中心2的配置application-master.properties

spring.application.name=springcloud-eureka-server
server.port=8765

eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.hostname=${spring.cloud.client.ipAddress}
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@localhost:8766/eureka/
eureka.instance.status-page-url-path=/

启动

首先先启动第一个服务,此时把总配置application.properties中的active设置成为slave。然后启动启动类,此时会报错,不过没关系。再启动第二个。

spring.profiles.active=slave
#spring.profiles.active=master

启动第二个服务的时候,此时把总配置application.properties中的active设置成为master。然后启动启动类。

#spring.profiles.active=slave
spring.profiles.active=master

此时就实现了高可用的注册中心了。
这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/han_xiaoxue/article/details/79443227