`
公园美丽
  • 浏览: 11878 次
社区版块
存档分类
最新评论

SpringCloud:Spring Cloud Config 配置中心高可用和refresh

 
阅读更多
 

1. 引言

任何单机版的服务都只能使用与测试环境或者自己做Demo测试,生产环境严禁使用单机服务,配置中心在整个微服务体系中都是及其重要的一个节点,尤其是在DevOps中自动扩容,如果配置中心宕机,那么所有的自动扩容都会失败。

(了解源码可+求求: 1791743380)

所以这一篇我们聊聊配置中心的高可用,说到高可用,在springcloud体系中,是有注册中心的,那么,我们的配置中心也是一个服务,可不可以使用Eureka做服务的注册与发现呢?

答案是肯定的。

2. Serve端

我们将上一篇的Serve端Copy到新的目录中,增加Eureka-client的依赖,使得Config-Serve可以注册到Eureka上。

2.1 pom.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <parent>  
  6.         <groupId>org.springframework.boot</groupId>  
  7.         <artifactId>spring-boot-starter-parent</artifactId>  
  8.         <version>2.1.6.RELEASE</version>  
  9.         <relativePath/> <!-- lookup parent from repository -->  
  10.     </parent>  
  11.     <groupId>com.springcloud</groupId>  
  12.     <artifactId>config-server</artifactId>  
  13.     <version>0.0.1-SNAPSHOT</version>  
  14.     <name>config-server</name>  
  15.     <description>Demo project for Spring Boot</description>  
  16.   
  17.     <properties>  
  18.         <java.version>1.8</java.version>  
  19.         <spring-cloud.version>Greenwich.SR1</spring-cloud.version>  
  20.     </properties>  
  21.   
  22.     <dependencies>  
  23.         <dependency>  
  24.             <groupId>org.springframework.cloud</groupId>  
  25.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework.cloud</groupId>  
  29.             <artifactId>spring-cloud-config-server</artifactId>  
  30.         </dependency>  
  31.   
  32.         <dependency>  
  33.             <groupId>org.springframework.boot</groupId>  
  34.             <artifactId>spring-boot-starter-test</artifactId>  
  35.             <scope>test</scope>  
  36.         </dependency>  
  37.     </dependencies>  
  38.   
  39.     <dependencyManagement>  
  40.         <dependencies>  
  41.             <dependency>  
  42.                 <groupId>org.springframework.cloud</groupId>  
  43.                 <artifactId>spring-cloud-dependencies</artifactId>  
  44.                 <version>${spring-cloud.version}</version>  
  45.                 <type>pom</type>  
  46.                 <scope>import</scope>  
  47.             </dependency>  
  48.         </dependencies>  
  49.     </dependencyManagement>  
  50.   
  51.     <build>  
  52.         <plugins>  
  53.             <plugin>  
  54.                 <groupId>org.springframework.boot</groupId>  
  55.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  56.             </plugin>  
  57.         </plugins>  
  58.     </build>  
  59.   
  60. </project>  

 

2.2 配置文件application.yml

Java代码  收藏代码
  1. server:  
  2.   port: 8080  
  3. spring:  
  4.   application:  
  5.     name: spring-cloud-config-server  
  6.   cloud:  
  7.     config:  
  8.       server:  
  9.         git:  
  10.           uri: https://github.com/meteor1993/SpringCloudLearning  
  11.           search-paths: chapter6/springcloud-config  
  12.           username: username  
  13.           password: password  
  14. eureka:  
  15.   client:  
  16.     service-url:  
  17.       defaultZone: http://localhost:8761/eureka/  

 

增加eureka的地址配置

2.3 启动类

启动类增加@EnableEurekaClient激活对注册中心的支持

Java代码  收藏代码
  1. package com.springcloud.configserver;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.config.server.EnableConfigServer;  
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  7.   
  8. @SpringBootApplication  
  9. @EnableConfigServer  
  10. @EnableEurekaClient  
  11. public class ConfigServerApplication {  
  12.   
  13.     public static void main(String[] args) {  
  14.         SpringApplication.run(ConfigServerApplication.class, args);  
  15.     }  
  16.   
  17. }  

 

这样Server注册端我们就修改完成了。先启动Eureka,再启动Serve,在浏览器中访问http://localhost:8761/,就可以看到我们的Serve端已经注册到注册中心了,接下来我们开始改造Client端。

3. Client端

首先还是将上一篇的Client端Copy过来,和Server端一样,增加Eureka-client的依赖,使得Config-Client可以从注册中心上发现服务。

3.1 pom.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <parent>  
  6.         <groupId>org.springframework.boot</groupId>  
  7.         <artifactId>spring-boot-starter-parent</artifactId>  
  8.         <version>2.1.6.RELEASE</version>  
  9.         <relativePath/> <!-- lookup parent from repository -->  
  10.     </parent>  
  11.     <groupId>com.springcloud</groupId>  
  12.     <artifactId>config-client</artifactId>  
  13.     <version>0.0.1-SNAPSHOT</version>  
  14.     <name>config-client</name>  
  15.     <description>Demo project for Spring Boot</description>  
  16.   
  17.     <properties>  
  18.         <java.version>1.8</java.version>  
  19.         <spring-cloud.version>Greenwich.SR1</spring-cloud.version>  
  20.     </properties>  
  21.   
  22.     <dependencies>  
  23.         <dependency>  
  24.             <groupId>org.springframework.boot</groupId>  
  25.             <artifactId>spring-boot-starter-web</artifactId>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework.cloud</groupId>  
  29.             <artifactId>spring-cloud-starter-config</artifactId>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframework.cloud</groupId>  
  33.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
  34.         </dependency>  
  35.   
  36.         <dependency>  
  37.             <groupId>org.springframework.boot</groupId>  
  38.             <artifactId>spring-boot-starter-test</artifactId>  
  39.             <scope>test</scope>  
  40.         </dependency>  
  41.     </dependencies>  
  42.   
  43.     <dependencyManagement>  
  44.         <dependencies>  
  45.             <dependency>  
  46.                 <groupId>org.springframework.cloud</groupId>  
  47.                 <artifactId>spring-cloud-dependencies</artifactId>  
  48.                 <version>${spring-cloud.version}</version>  
  49.                 <type>pom</type>  
  50.                 <scope>import</scope>  
  51.             </dependency>  
  52.         </dependencies>  
  53.     </dependencyManagement>  
  54.   
  55.     <build>  
  56.         <plugins>  
  57.             <plugin>  
  58.                 <groupId>org.springframework.boot</groupId>  
  59.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  60.             </plugin>  
  61.         </plugins>  
  62.     </build>  
  63.   
  64. </project>  

 

3.2 bootstrap.properties

这里我们需要先清空application.yml,所有的配置全都转移到bootstrap.properties中。

Java代码  收藏代码
  1. spring.application.name=spring-cloud-config-client  
  2. server.port=8081  
  3.   
  4. spring.cloud.config.name=springcloud-config  
  5. spring.cloud.config.profile=dev  
  6. spring.cloud.config.label=master  
  7. spring.cloud.config.discovery.enabled=true  
  8. spring.cloud.config.discovery.serviceId=spring-cloud-config-server  
  9.   
  10. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/  

 

主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三个配置:

  • spring.cloud.config.discovery.enabled :开启Config服务发现支持
  • spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值
  • eureka.client.serviceUrl.defaultZone :指向注册中心的地址

3.3 启动类

启动类增加@EnableEurekaClient激活对注册中心的支持

Java代码  收藏代码
  1. package com.springcloud.configclient;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  6.   
  7. @SpringBootApplication  
  8. @EnableEurekaClient  
  9. public class ConfigClientApplication {  
  10.   
  11.     public static void main(String[] args) {  
  12.         SpringApplication.run(ConfigClientApplication.class, args);  
  13.     }  
  14.   
  15. }  

 

4. 高可用

现在我们来模拟生产环境。

首先,顺次启动Eureka,Server,Client。

在idea启动两个config-serve,我们修改idea配置启动配置,换到8000端口启动config-serve。

先访问http://localhost:8761/,可以看到两个config-serve都正常注册到注册中心。

 

SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh
 

 

如上图就可发现会有两个server端同时提供配置中心的服务,防止某一台down掉之后影响整个系统的使用。

我们访问client端的链接:http://localhost:8081/hello, 可以看到页面正常显示:hello dev update1,刷新几次,显示都没问题,现在我们随机停掉一个端口的config-serve服务,再去刷新页面,可以发现,页面依然可以正常显示:hello dev update1。

至此,我们高可用的目的已经达到,但是,不知道各位有没有映像,我们上一篇留了一个坑,服务启动后,我们修改远端github上的配置时,这个配置并不会实时被客户端端所获取到,下面我们来聊一聊有关Spring Cloud Config 刷新的问题。

5. refresh

我们的客户端并不能主动去感知Git或者Svn的配置变化,从而主动获取最新的配置。那么,客户端如何去主动获取新的配置信息呢?springcloud已经给我们提供了解决方案,每个客户端通过POST方法触发各自的/refresh。

修改config-client项目已到达可以refresh的功能。

5.1 添加依赖pom.xml

在我们原有的config-client项目的pom.xml的基础增加新的依赖

Java代码  收藏代码
  1. <dependency>  
  2.   <groupId>org.springframework.boot</groupId>  
  3.   <artifactId>spring-boot-starter-actuator</artifactId>  
  4. </dependency>  

 

增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。

5.2 开启更新机制

需要给加载变量的类上面加载@RefreshScope,在客户端执行/refresh的时候就会更新此类下面的变量值。

Java代码  收藏代码
  1. package com.springcloud.configclient.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.cloud.context.config.annotation.RefreshScope;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7.   
  8. /** 
  9.  * @Author: shiyao.wei 
  10.  * @Date: 2019/7/4 16:19 
  11.  * @Version: 1.0 
  12.  * @Desc: 
  13.  */  
  14. @RestController  
  15. @RefreshScope // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。  
  16. public class HelloController {  
  17.   
  18.     @Value("${springcloud.hello}")  
  19.     private String hello;  
  20.   
  21.     @RequestMapping("/hello")  
  22.     public String from() {  
  23.         return this.hello;  
  24.     }  
  25. }  

 

5.3 配置文件bootstrap.properties

Java代码  收藏代码
  1. <!-- swagger工具包 -->  
  2. <dependency>  
  3.     <groupId>io.springfox</groupId>  
  4.     <artifactId>springfox-swagger2</artifactId>  
  5.     <version>${swagger.version}</version>  
  6. </dependency>  
  7. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->  
  8. <dependency>  
  9.     <groupId>io.springfox</groupId>  
  10.     <artifactId>springfox-swagger-ui</artifactId>  
  11.     <version>${swagger.version}</version>  
  12. </dependency>  

 

  • management.security.enabled: springboot 1.5.X 以上默认开通了安全认证,所以需要添加这个配置
  • management.endpoints.web.exposure.include: springboot 2.x 默认只开启了info、health的访问,*代表开启所有访问

5.4 测试

我们先访问客户端的测试连接:http://localhost:8081/hello, 这时,页面的显示是:hello dev update1,我们修改github上的信息,修改为:hello dev update,现在访问http://localhost:8081/hello,得到的信息还是:hello dev update1,现在我们刷新一下客户端,通过cmd命令行执行:curl -X POST http://localhost:8081/actuator/refresh,可以看到命令行上有显示:[“springcloud.hello”,”config.client.version”],意味着springcloud.hello这个配置已经刷新,这时,我们再去刷新一下页面,可以看到,页面上得到的信息已经变为了:hello dev update,这时我们refresh成功。

每次手动刷新客户端还是很麻烦,有没有什么办法只要提交代码就自动调用客户端来更新呢,github的webhook是一个好的办法。

6. webhook

WebHook是当某个事件发生时,通过发送http post请求的方式来通知信息接收方。Webhook来监测你在Github.com上的各种事件,最常见的莫过于push事件。如果你设置了一个监测push事件的Webhook,那么每当你的这个项目有了任何提交,这个Webhook都会被触发,这时Github就会发送一个HTTP POST请求到你配置好的地址。

如此一来,你就可以通过这种方式去自动完成一些重复性工作,比如,你可以用Webhook来自动触发一些持续集成(CI)工具的运作,比如Travis CI;又或者是通过 Webhook 去部署你的线上服务器。下图就是github上面的webhook配置。

 

SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh
 

 

  • Payload URL: 触发后回调的URL
  • Content type: 数据格式,两种一般使用json
  • Secret: 用作给POST的body加密的字符串。采用HMAC算法
  • events: 触发的事件列表
events事件类型 描述
push 仓库有push时触发。默认事件
create 当有分支或标签被创建时触发
delete 当有分支或标签被删除时触发

这样我们就可以利用hook的机制去触发客户端的更新,但是当客户端越来越多的时候hook支持的已经不够优雅,另外每次增加客户端都需要改动hook也是不现实的。

分享到:
评论

相关推荐

    Spring Cloud.docx

    Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh spring-cloud-config-bus-rabbitMQ Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)

    springcloud微服务框架+服务模版

    spring-cloud-config-eureka:配置中心服务化和高可用代码示例 spring-cloud-config-eureka-bus:配置中心和消息总线示例(配置中心终结版) gateway-service-zuul:Spring Cloud Zuul使用初级篇 网关 均衡负载 ...

    spring-cloud-config-refresh:Spring Cloud Config刷新

    特点从Spring Cloud Config Server添加可配置的“自动”配置功能,并具有可配置的延迟。2.设定为了设置刷新,您必须将休闲依存关系添加到Cloud Config Client。 &lt; dependency&gt; &lt; groupId&gt;...

    spring-cloud-examples

    spring-cloud-config-eureka:配置中心服务化和高可用代码示例 spring-cloud-config-eureka-bus:配置中心和消息总线示例(配置中心终结版) gateway-service-zuul:Spring Cloud Zuul使用初级篇 网关 均衡负载 ...

    spring-cloud使用的各种示例

    - [springcloud(八):配置中心服务化和高可用](http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html) - [springcloud(九):配置中心和消息总线(配置中心终结版)]...

    spring cloud 配置中心代码例子

    http://localhost:8080/actuator/refresh 必须是post方式访问 查看配置 :http://localhost:8081/cloud-config/simple2 查看配置效果:http://localhost:8080/index 注意: 测试了下配置文件好像要放到spring cloud ...

    SpringCloud消息总线RabbitMQ+Bus-Refresh接口触发所有config-client自动重新读取配置文件

    实践方志鹏博客搭建Springcloud+RabbitMQ+Config-client+config-server Eureka-server的微服务架构,通过/bus/refresh接口触发所有config-client自动从config-server重新读取配置文件。SpringCloud和SpringBoot版本...

    springCloud

    断路器示意图 SpringCloud Netflix实现了断路器库的名字叫Hystrix. 在微服务架构下,通常会有多个层次的服务调用. 下面是微服架构下, 浏览器端通过API访问后台微服务的一个示意图: hystrix 1 一个微服务的超时...

    spring-cloud-example:基于 spring-cloud 的配置的简单客户端和服务器示例

    介绍基于 spring-cloud 的配置系统的工作示例。 使用ServerMain启动服务器。 然后,使用ClientMain启动客户端。 使用 curl 访问演示端点,例如 curl http://localhost:8080/更改 spring-cloud-server/config-...

    java8源码-spring-cloud-demo:演示

    java8 源码 spring cloud demo 这代码就当作自己的demo库吧 ...分布式配置中心(支持refresh) 服务网关中心 oauth2授权 统一swagger文档中心结合oauth2认证 简单服务容错(Hystrix) 统一异常处理(gateway特殊处理)

    springcloud1

    春天的云 尤里卡服务器(Spring ...config-server(Spring Cloud Bus)[cloudb] 端口:9050 使用Rabbitmq(端口:5672)在config-server和eureka-client1之间发送消息 url:localhost:9050/actuator/bus-refresh m

    hxconfig.zip

    配置了spring cloud svn方式获得配置信息,集成了spring bus,并配置到了config server 上,在config server上 用/bus/refresh可以将所有config client的配置进行同步修改,只做到这里,如果想自动刷新可以在svn 的...

Global site tag (gtag.js) - Google Analytics