配置中心 - Spring Cloud Config

/ Java / 没有评论 / 1820浏览

Spring Cloud Config作为配置中心服务于分布式系统,而且其Spring Environment和PropertySource特性与Spring程序非常契合,特别适合Spring项目中。

配置中心系列

特性

搭建配置中心服务器


<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
package com.tenmao.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
spring.application.name=config-server
server.port=7001
# Git 仓库位置
spring.cloud.config.server.git.uri=/data/workspace/config-repo.git
# 仓库路径下相对搜索位置,可配置多个
spring.cloud.config.server.git.search-paths=config
# 创建仓库config-repo
mkdir -p /data/workspace/config-repo.git
cd /data/workspace/config-repo.git
git init --bare
cd /data/workspace
git clone file:///data/workspace/config-repo.git

# 创建配置文件config/blog.properties和config/blog-dev.properties
mkdir config
cd config
touch blog.properties
cat threshold=default-0.1 > blog.properties
touch blog-dev.properties
cat threshold=dev-0.1 > blog-dev.properties
git add .

# 提交到远程仓库
git commit -m "initial config"
git push

# 创建新的版本(label)
git checkout -b 2.0
# 修改配置文件
git add .

# 提交到远程仓库
git commit -m "set threshold to 0.2"
git push --set-upstream origin 2.0

img

体验配置文件

- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.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>
        <version>2.1.1.RELEASE</version>
    </dependency>
</dependencies>
@RestController
public class HomeController {
    @Value("${threshold}")
    private String threshold;

    @GetMapping("threshold")
    public String threshold() {
        return threshold;
    }
}

注意:不是application.properties

spring.application.name=blog
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001

配置实时生效


借助Spring Boot的Actuator的接口/refresh

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>
@RefreshScope
@RestController
public class HomeController {
    private String threshold;

    @Value("${threshold}")
    public void setThreshold(String threshold) {
        this.threshold = threshold;
    }

    @GetMapping("threshold")
    public String threshold() {
        return threshold;
    }
}
management.endpoints.web.exposure.include=refresh

使用postman通过POST方法访问接口http://localhost:8080/actuator/refresh 注意:Spring Boot 2.x,是/actuator/refresh,而不是/refresh

常见问题

参考