高可用HA(High Availability)是分布式系统架构设计中必须考虑的因素之一,它一般是指,经过设计减小系统不能提供服务的时间.前端
1)假设系统一直可以提供服务,咱们说系统的可用性是100%.java
2)若是系统每运行100个时间单位,会有1个时间单位没法提供服务,咱们说系统的可用性是99%.git
举个例子,百度的搜索首页是业界公认的高可用保障很是出色的系统
咱们一般会经过ping baidu.com来判断网络是否通畅,这也恰巧说明了百度首页的可用性很是之高,值得信赖.github
1.主从复制: 主服务挂掉后,从服务升级为主服务继续工做.web
2.双机热备: 一台工做,一台备用,工做服务器挂掉后,备用服务器继续工做.算法
3.分布式集群: 多台实例同时工做,当其中一台挂掉后,前端或者代理踢出这台服务器,负载均衡算法将再也不调度它.spring
Config Server 的高可用方案,是借助Eureka(注册中心)实现的,也就是上面提到的分布式集群方案.
多个Config Server同时工做,任何一台挂掉后,Eureka服务器都会通知客户端, 客户端后续将再也不从这里请求配置信息.bootstrap
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
# 将配置中心注册到eureka实现高可用 eureka: instance: hostname: fastjee-config.com # 更改eureka更新频率, 关闭eureka的自我保护机制. leaseRenewalIntervalInSeconds: 10 # 租期更新时间间隔(默认30秒) leaseExpirationDurationInSeconds: 30 # 租期到期时间(默认90秒) client: serviceUrl: defaultZone: https://fastjee:123456@fastjee-registration.com:5000/eureka/ healthcheck: enabled: true # 开启健康检查(须要spring-boot-starter-actuator依赖)
这里更改了eureka的自我保护机制, 为了方便后面的测试, 即时剔除无效服务器.
添加@EnableDiscoveryClient注解, 标识该服务为eureka客户端.服务器
@EnableConfigServer @EnableDiscoveryClient @SpringBootApplication(scanBasePackages = {"com.fastjee.common.web","com.fastjee.config"}) public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }
@EnableDiscoveryClient能够替换为@EnableEurekaClient,但后者使用场景比较单一,不兼容其它类型的注册中心.
在测试前, 创建测试用的Config Clinet项目;网络
将配置文件fastjee-config-server-test-dev.yml push到由configServer指定的github仓库.
config-test: sayHello: HelloWorld!
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
bootstrap.yml
spring: profiles: active: dev application: name: fastjee-config-server-test # 从配置中心拉取配置, 其余配置文件都在git仓库上 cloud: config: fail-fast: true discovery: service-id: fastjee-config enabled: true label: master profile: ${spring.profiles.active} name: ${spring.application.name} # 注册到注册中心 eureka: instance: hostname: fastjee-config-server-test.com client: serviceUrl: defaultZone: https://fastjee:123456@fastjee-registration.com:5000/eureka/
测试RESTFul接口.
@Value("${config-test.sayHello}") private String sayHello; @GetMapping("/sayHello") public @ResponseBody ResponseEntity<String> sayHello() { return ResponseEntity.ok(sayHello); }
启动后的效果图以下:
这里再也不写文字性的描述, 看图便可.