Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,可以管理集群中的配置文件。
使用Git、SVN等版本管理系统存放配置文件,配置服务器会到版本管理系统获取配置,集群中的配置客户端再到配置服务器中获取配置。

开发工具:IntelliJ IDEA 2019.2.2

一、创建配置服务器

1、SVN服务器添加项目和配置文件

config-client-dev.yml内容:

server:
port: 8092
test:
user:
name: aa

config-client-test.yml

server:
port: 8093
test:
user:
name: bb

2、创建项目

IDEA中创建一个新的SpringBoot项目,名称为“spring-config-server”,SpringBoot版本选择2.1.10,在选择Dependencies(依赖)的界面勾选Spring Cloud Config -> Config Server。
pom.xml会引入spring-cloud-config-server依赖项,再在pom.xml中加入org.tmatesoft.svnkit依赖项,pom.xml完整内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-server</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.10.1</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

3、修改配置application.yml

spring-cloud-config-server提供了4种配置,可以通过不同名字来激活:
(1)git:默认值,表示去Git仓库读取配置文件;
(2)subversion:表示去SVN仓库读取配置文件;
(3)native:表示去本地文件系统读取配置文件;
(4)vault:表示去Vault(一种资源控制工具)中读取配置文件;

server:
port: 8091
spring:
application:
name: config-server
profiles:
active: subversion
cloud:
config:
server:
svn:
uri: https://localhost/svn/test-project
username: abc
password: 123456
default-label: default-config

4、修改启动类代码

增加注解@EnableConfigServer

package com.example.springconfigserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class SpringConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(SpringConfigServerApplication.class, args);
} }

可以使用Config Server的端点获取配置文件的内容,端点与配置文件的映射规则如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{application} 是应用名称,对应配置文件的名称部分,本例是config-client。
{profile} 是配置文件的版本,本例是dev和test。
{label} 表示分支,如果是git则默认是master分支。

启动服务,浏览器访问(把下面test换为dev,结果类似)下面地址,分别输出如下:
http://localhost:8091/config-client/test

{"name":"config-client","profiles":["test"],"label":null,"version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}

http://localhost:8091/config-client/test/default-config

{"name":"config-client","profiles":["test"],"label":"default-config","version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}

http://localhost:8091/config-client-test.yml

server:
port: 8093
test:
user:
name: bb

http://localhost:8091/default-config/config-client-test.yml

server:
port: 8093
test:
user:
name: bb

二、配置客户端读取SVN配置

1、创建项目

IDEA中创建一个新的SpringBoot项目,名称为“spring-config-client”,SpringBoot版本选择2.1.10,在选择Dependencies(依赖)的界面勾选Web -> Spring Web,Spring Cloud Config -> Config Client。
pom.xml会引入spring-boot-starter-web和spring-cloud-starter-config依赖项,pom.xml完整内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-client</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2、修改启动类代码

增加测试方法

package com.example.springconfigclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
public class SpringConfigClientApplication { @Autowired
private Environment env; public static void main(String[] args) {
SpringApplication.run(SpringConfigClientApplication.class, args);
} @RequestMapping("/")
public String home(){
return env.getProperty("test.user.name");
}
}

3、添加配置bootstrap.yml

spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8091
profile: dev

设置了应用名称config-client,使用spring.cloud.config.uri来设置配置服务器的地址,使用spring.cloud.config.profile来读取指定的配置。最终,配置客户端会到SVN服务器的test-project/default-config目录下读取config-client-dev.yml(.properties)。

启动服务,浏览器访问:http://localhost:8092/(这里端口8092在config-client-dev.yml中已指定),页面输出:aa

也可以使用spring.cloud.config.name代替spring.application.name,结果一样。

spring:
cloud:
config:
uri: http://localhost:8091
profile: dev
name: config-client

如果spring.cloud.config.name和spring.application.name都不提供,则默认读取application-dev.yml。
在SVN的test-project/default-config目录下新增文件application-dev.yml,内容

server:
port: 8092
test:
user:
name: cc

启动服务,浏览器访问:http://localhost:8092/,页面输出:cc

可以设置spring.client.config.label来覆盖服务器的default-lable属性,另外上面profile也可改为下面写法。

spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8091
lable: default-config
name: config-client
profiles:
active: dev

三、使用/refresh端点手动刷新配置

1、在上面配置客户端的pom.xml中添加依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、application.yml(或bootstrap.yml)添加配置

management:
endpoints:
web:
exposure:
include: "*"

3、在Controller上添加注解@RefreshScope

package com.example.springconfigclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
@RefreshScope
public class SpringConfigClientApplication { @Autowired
private Environment env; public static void main(String[] args) {
SpringApplication.run(SpringConfigClientApplication.class, args);
} @RequestMapping("/")
public String home(){
return env.getProperty("test.user.name");
}
}

4、修改SVN服务器上config-client-dev.yml内容

把name的值由aa修改为aa11,提交SVN修改。

5、/refresh只支持POST请求,发送POST请求到http://localhost:8092/actuator/refresh

使用Postman发送POST请求,如果SVN没有修改,返回[],如果有修改,返回结果如下:

刷新浏览器地址:http://localhost:8092/,结果已由aa,变成了aa11。

四、使用Spring Cloud Bus自动刷新配置

上面使用/refresh端点手动刷新配置只是刷新一个客户端的配置,如果有很多微服务的客户端,则需要一个个需要手动刷新。
使用Spring Cloud Bus可以实现刷新一个客户端配置后,自动刷新其余的客户端配置。
Spring Cloud Bus使用消息代理(RabbitMQ、Kafka等)连接分布式系统的客户端,广播传播状态的更改或其他的管理指令。

Spring Cloud Bus的结构图如下,所有客户端通过消息总线连接到了一起,每个客户端会订阅配置更新事件,当其中一个客户端的actuator/bus-refresh被请求时,会向消息总线发送一个配置更新事件,其他客户端获得该事件后也会更新配置。

使用实例:

1、SVN服务器只保留config-client-dev.yml,内容如下:

test:
user:
name: aa

2、上面客户端项目“spring-config-client”的pom.xml加入依赖项

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3、application.yml添加配置(如果rabbitmq服务器在本机,端口和账号没改的话,下面也可不添加)

Spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

4、修改启动类代码,支持启动时输入端口号

IDEA 配置可以启动多次:IDEA -> Edit Configurations -> Run/Debug  Configuration -> 勾选Allow parallel run

package com.example.springconfigclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Scanner; @SpringBootApplication
@RestController
@RefreshScope
public class SpringConfigClientApplication { @Autowired
private Environment env; public static void main(String[] args) {
//SpringApplication.run(SpringConfigClientApplication.class, args);
Scanner scan = new Scanner(System.in);
String port = scan.nextLine();
new SpringApplicationBuilder(SpringConfigClientApplication.class).properties("server.port=" + port).run(args);
} @RequestMapping("/")
public String home(){
return env.getProperty("test.user.name");
}
}

5、测试

(1)客户端项目配置可以启动多次:IDEA -> Edit Configurations -> Run/Debug  Configuration -> 勾选Allow parallel run

(2)启动启动端spring-config-client,输入端口号9001,浏览器访问http://localhost:9001/,页面显示aa

(3)再次启动启动端spring-config-client,输入端口号9002,浏览器访问http://localhost:9002/,页面显示aa

(4)修改SVN的config-client-dev.yml的name值为aa123

(5)使用Postman发送POST请求到http://localhost:9001/actuator/bus-refresh

(6)浏览器访问http://localhost:9001/和http://localhost:9002/,两个页面都已显示aa123。

Spring Cloud Config实现集群配置中心的更多相关文章

  1. Spring Cloud config之一:分布式配置中心入门介绍

    Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...

  2. Spring Cloud Config 1 (分布式配置中心)

    spring cloud config是spring cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端和客户端两部分. 服务端也被称为 ...

  3. Spring Cloud Eureka的集群配置(六)

    1.再次创建2个Eureka工程 工程名:microservicecloud-eureka-7002 工程名:microservicecloud-eureka-7003 2.pom.xml文件 < ...

  4. Spring Cloud(Dalston.SR5)--Config 集群配置中心

    Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...

  5. Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】

    Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-26 |  本文接之前的<Spring Clou ...

  6. Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】

    Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-24 |  Spring Cloud Confi ...

  7. Spring Cloud Alibaba | Nacos集群部署

    目录 Spring Cloud Alibaba | Nacos集群部署 1. Nacos支持三种部署模式 2. 集群模式下部署Nacos 2.1 架构图 2.2 下载源码或者安装包 2.3 配置集群配 ...

  8. Spring Cloud第十一篇 | 分布式配置中心高可用

    ​ 本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  9. Spring Cloud(九):配置中心(消息总线)【Finchley 版】

    Spring Cloud(九):配置中心(消息总线)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-05-07 |  我们在 Spring Cloud(七):配置中心 ...

随机推荐

  1. Glibc编译报错:*** These critical programs are missing or too old: as ld gcc

    Binutils版本升级 这里是binutils版本过低导致, 查看已部署版本 上传离线升级包 [root@sdw1 glibc]# tar -zxvf binutils-2.32.tar.gz [r ...

  2. 删除节点(removeChild())

    remoceChild():方法将从一个给定元素里删除一个子节点: reference = element.removeChild(node); 这个方法的返回值是一个指向已经被删除的子节点的引用指针 ...

  3. 【Android - 控件】之V - DrawerLayout的使用

    DrawerLayout是Android V4包中的一个布局控件,用来实现一个抽屉样式的布局. DrawerLayout通过设置子视图的layout_gravity来决定子视图停靠在屏幕的哪个边缘外侧 ...

  4. jQuery.hasClass() 函数详解

    jQuery.hasClass() 函数详解 hasClass()函数用于指示当前jQuery对象所匹配的元素是否含有指定的css类名. 该函数属于jQuery对象(实例). 语法 JavaScrip ...

  5. 第一次c语言作业。

    第一次c语言作业 作业1 2.1 你对软件工程专业或者计算机科学与技术专业了解是怎样? 我认为计算机科学与技术是研究信息过程.用以表达此过程的信息结构和规则及其在信息处理系统中实现的学科.这门学科是为 ...

  6. 转:使用JSR-303进行校验 @Valid

    一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...

  7. Selenium 4 Java的最佳测试框架

    几十年来,Java一直是开发应用程序服务器端的首选编程语言.尽管JUnit一直在与开发人员一起帮助他们进行自动化的单元测试,但随着时间的推移和测试行业的发展,特别是伴随着自动化测试的兴起,已经开发了许 ...

  8. Unknown class XXViewController in Interface Builder file.”问题处理

    “Unknown class XXViewController in Interface Builder file.”问题处理   在静态库中写了一个XXViewController类,然后在主工程的 ...

  9. Node.js 中 __dirname 和 ./ 的区别

    概要 __dirname 总是指向被执行 js 文件的绝对路径 在 /d1/d2/myscript.js 文件中写了 __dirname, 它的值就是 /d1/d2 . ./ 会返回你执行 node ...

  10. Ubuntu 18.04 环境下 kubernetes v1.16.2 单机部署说明

    一.安装环境 本次部署使用阿里云ECS 操作系统: Ubuntu  18.04 64位 实例规格: ecs.c6.large 2U4G 二.kubernetes 版本 k8s.gcr.io/kube- ...