Spring Cloud Config实现集群配置中心

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>
View Code

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);
    }

}
View Code

可以使用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>
View Code

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添加配置

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");
    }
}
View Code

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。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章