本文共 1591字,阅读大约须要 6分钟 !java
业务微服务化之后,咱们要求服务高可用,因而咱们能够部署多个相同的服务实例,并引入负载均衡机制。而微服务注册中心做为微服务化系统的重要单元,其高可用也是很是必要的,所以在生产中咱们可能须要多个微服务注册中心实例来保证服务注册中心的稳定性。本文就以 Eureka微服务注册中心为例,来实践一下如何 在线扩充 Eureka Server实例来保证 微服务注册中心的高可用性!git
注: 本文首发于 My Personal Blog,欢迎光临 小站github
本文内容脑图以下:spring
咱们欲模拟以下过程:bootstrap
首先启动一个 eureka-server实例(eureka-server-1)浏览器
再启动一个 eureka-client实例(eureka-client-1)并注册到 eureka-server-1中app
接下来咱们启动第二个 eureka-server实例:eureka-server-2,而且让已启动的 eureka-server-1 和 eureka-client-1在不重启的状况下来感知到 eureka-server-2的加入负载均衡
同理咱们能够在启动第三个 eureka-server实例:eureka-server-3,而且让已启动的 eureka-server-1 、eureka-server-2 和 eureka-client-1 在不重启的状况下来感知到 eureka-server-3的加入spring-boot
更多 eureka-server实例的加入原理彻底相同,再也不赘述微服务
这样一来咱们便实现了微服务注册中心的动态在线扩容!
而如何才能让已启动的服务可以在不重启的状况下来感知到新的 eureka-server 的加入呢?
为此咱们引入 Spring Cloud Config 配置中心 config-server,并将 eureka-server和 eureka-client服务的配置文件由 config-server进行统一管理,这样一来对配置文件的修改若是能够经过某种机制来通知已启动的服务,那么问题便迎刃而解了!
咱们给出整个过程的原理图以下:
接下来咱们来实践这整个过程!
pom中关键依赖以下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
主类添加相应注解:
@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { ... }
bootstrap.yml配置文件以下:
spring: application: name: config-server cloud: config: server: git: uri: https://github.com/hansonwang99/xxxx username: xxxx password: xxxx server: port: 1115
该 yml配置很重要,其链接了预先准备好的 git仓库,后续 eureka-server 和 eureka-client 的配置文件都是存于该git仓库中的!
pom中关键依赖以下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
主类添加相应注解:
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { ... }
bootstrap.yml重要配置以下:
spring: application: name: eureka-server cloud: config: uri: http://localhost:1115
注意该 yml中关于 spring cloud config 的配置一样很是重要,由于此 eureka-server须要从 config-server中拉取配置!
最后咱们还须要添加一个 controller便于测试:
@RestController @RequestMapping("/test") public class TestController { @Autowired EurekaClientConfigBean eurekaClientConfigBean; @GetMapping("/eureka-service-info") public Object getEurekaServerUrl(){ return eurekaClientConfigBean.getServiceUrl(); } }
这个 Rest Controller 接口的意图很简单:打印出当前到底有多少个 eureka-server实例在提供服务(从而能够直观的判断出 eureka-server的扩容是否已经成功)
pom中关键依赖以下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
主类添加相应注解:
@SpringBootApplication @EnableDiscoveryClient public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
bootstrap.yml 重要配置以下:
spring: application: name: eureka-client cloud: config: uri: http://localhost:1115
这里基本同上面 eureka-server的配置,再也不赘述
一样,咱们也在 eureka-client中添加一个 controller便于测试:
@RestController @RequestMapping("/test") public class TestController { @Autowired EurekaClientConfigBean eurekaClientConfigBean; @GetMapping("/eureka-service-info") public Object getEurekaServerUrl(){ return eurekaClientConfigBean.getServiceUrl(); } }
三个工程的建立到此完毕!下来咱们来依次启动各个工程
首先启动 config-server工程
而后用 peer1配置文件来启动 第一个eureka server,指令以下:
mvn spring-boot:run -Dspring.profiles.active=peer1
启动过程的开始打印出以下信息,一目了然:
固然这个 peer1配置文件实际物理存在于git仓库之上:
mvn spring-boot:run -Dspring.profiles.active=peer1
其依然须要经过 spring cloud config 去git仓库拉取 eureka-client的 peer1配置文件,这一点从 eureka-client的启动过程当中也能看到:
既然 config-server / eureka-server / eureka-client 三个服务已经启动了,那么接下来咱们来测试一下:
浏览器访问 eureka-client的 Rest接口:localhost:1114/test/eureka-service-info
:
同理,浏览器访问 eureka-server的 Rest接口:localhost:1111/test/eureka-service-info
:
OK,一切正常,但目前微服务注册中心仅一个 eureka-server实例,下来咱们来 在线动态扩容微服务注册中心实例。
接下来咱们再用 peer2配置文件来 启动第二个 eureka server,指令以下:
mvn spring-boot:run -Dspring.profiles.active=peer2
启动完毕后,浏览器访问微服务注册中心第一个实例 eureka-server-1:localhost:1111/
发现第二个微服务注册中心实例 eureka-server-2已经成功启动并加入
为了让已启动的 eureka-client和 eureka-server-1能在线感知到 eureka-server-2的加入,此时咱们须要修改两个地方:
server: port: 1114 spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/,http://localhost:1112/eureka/ # 此处改成包含两个eureka-server
server: port: 1111 spring: application: name: eureka-server eureka: instance: hostname: localhost preferIpAddress: true client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:1112/eureka/ # 此处改成第二个eureka-server地址 server: waitTimeInMsWhenSyncEmpty: 0 enableSelfPreservation: false
Git仓库里配置文件的修改完毕并不能触发已启动的 eureka-client和 eureka-server-1 在线更新,咱们还须要向 eureka-client服务和 eureka-server-1服务来 POST两个请求来激活刚才所修改的配置:
POST localhost:1111/actuator/refresh // 激活 eureka-client服务的配置 POST localhost:1114/actuator/refresh // 激活 eureka-server-1 服务的配置
POST请求一旦发出,咱们从控制台里能直观看到更新配置文件的详情:
配置文件更新完毕,浏览器再次访问 eureka-client的 Rest接口:localhost:1114/test/eureka-service-info
:
接下来还能够再用 peer3配置文件来启动第三个 eureka server加入高可用微服务注册中心集群,过程和上面相似,此处再也不赘述!固然更多 eureka-server实例的扩充原理也相同。
因为能力有限,如有错误或者不当之处,还请你们批评指正,一块儿学习交流!