`
白马探
  • 浏览: 14648 次
社区版块
存档分类
最新评论

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

 
阅读更多

1. 引言

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

所以这一篇我们聊聊配置中心的高可用,说到高可用,在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也是不现实的。

分享到:  

 

 
分享到:
评论

相关推荐

    SpringCloud——分布式配置中心(Spring Cloud Config)

    在微服务架构中,Spring Cloud Config 是一个强大的分布式配置中心,它允许开发人员将应用程序的配置存储在远程仓库中,并且可以在运行时动态地管理和更新这些配置,无需重启应用。这个特性对于大型分布式系统来说...

    springcloud配置中心个人demo

    SpringCloud Config 是一个分布式系统配置管理工具,它允许在分布式系统的不同组件之间集中管理和动态更新配置。本项目是一个个人实现的 SpringCloud Config 的演示示例,旨在帮助理解其工作原理和使用方法。以下是...

    springcloud+注册中心eureka+配置中心demo

    在SpringCloud框架中,Eureka是作为服务发现和注册中心的核心组件,而配置中心则用于集中管理和分发应用的配置,使得微服务架构中的各个服务能够动态地获取和更新配置。下面将详细介绍这两个核心概念以及如何在一个...

    springcloud微服务框架+服务模版

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

    Spring Cloud Config Demo

    Spring Cloud Config 是一款基于Spring Boot实现的配置管理工具,它允许开发者在远程服务器上集中管理和动态更新应用的配置,使得微服务架构中的配置管理变得更加便捷。这个"Spring Cloud Config Demo"是一个实例,...

    SpringCloud整合配置中心.zip

    SpringCloud整合配置中心是微服务架构中至关重要的一个环节,它允许我们集中管理和分发微服务的配置,使得在分布式环境中能够动态更新配置而无需重启服务。本项目以SpringCloud的配置中心组件——Spring Cloud ...

    微服务框架 springcloud config 自动刷新远程svn配置代码示例

    首先,`spring-cloud-config-server`是Spring Cloud Config的核心组件,它作为一个配置中心,存储并管理所有微服务的配置。你可以将配置文件托管在SVN仓库中,通过Config Server进行访问和管理。配置文件通常以YAML...

    spring cloud config微服务配置中心

    - **配置服务器(Config Server)**:Spring Cloud Config Server是配置中心的核心,它负责存储和提供所有微服务的配置信息。 - **配置客户端(Config Client)**:运行在每个微服务中的组件,用于与配置服务器...

    spring-cloud-examples

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

    cloudconfig:spring-cloud-config配置中心通过消息总线实现实时刷新demo

    1. 创建Config Server项目:基于`cloudconfig-master`这个项目,我们首先需要配置Spring Boot应用启动类,启用`@EnableConfigServer`注解,这样就创建了一个Config Server。 2. 配置仓库:在application.properties...

    spring-cloud使用的各种示例

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

    springcloud config server 服务配置中心 服务器端

    2. **监控**:集成Actuator,可以监控Config Server 的运行状态和配置变更。 3. **多环境支持**:利用Git的分支特性,为不同环境创建独立的配置分支,如`dev`、`test`、`prod`。 4. **灰度发布**:配合配置中心,...

    spring cloud 配置中心代码例子

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

    Spring Cloud之Config分布式配置应⽤

    Spring Cloud Config 是一个用于在分布式系统中管理外部配置的工具,它支持服务发现和集中式配置。这个框架允许你集中存储、管理和版本化你的应用配置,使得开发人员可以在不重启应用的情况下更新配置。本文将深入...

    spring-cloud-config

    - 配置中心:Config Server 是 Spring Cloud Config 的核心组件,它作为集中存储和管理所有微服务配置的中心节点。它可以连接到多种后端存储,如 Git 或 SVN,用于保存配置信息。 - 版本控制:由于配置信息存储在...

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

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

    26-Spring Cloud分布式配置中心Config1

    Spring Cloud分布式配置中心Config是微服务架构中解决多环境配置管理的一个重要工具。在传统的开发过程中,不同环境(如开发、测试、生产)的配置管理往往由开发人员手动处理,或者通过复杂的流程来确保配置的正确性...

    spring cloud文档pdf版(带书签)

    云原生应用旨在充分利用云平台提供的各种服务,具有高性能、高可用性和灵活性的特点。 2. Spring Cloud上下文服务(SpringCloudContext: ApplicationContextServices): 包括引导应用上下文(Bootstrap ...

    springcloud-config:springcloud-config的配置中心

    SpringCloud Config是Spring...综上所述,SpringCloud Config作为配置中心,提供了便捷的配置管理方式,增强了微服务架构的灵活性和可扩展性。理解和掌握其工作原理及使用方法,对于构建高效稳定的分布式系统至关重要。

Global site tag (gtag.js) - Google Analytics