美文网首页Spring Cloud
配置中心 - Spring Cloud Config

配置中心 - Spring Cloud Config

作者: 十毛tenmao | 来源:发表于2019-03-08 12:23 被阅读16次

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

配置中心系列

特性

  • 多环境、多应用支持
  • 使用git作为存储,并天然支持回滚、对比
  • 通过Http接口,也支持其他语言和应用

搭建配置中心服务器


  • pom.xml
<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);
    }
}
  • 修改配置(application.properties)
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
体验配置文件
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties

客户端配置


  • pom.xml
<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>
  • HomeController.java
@RestController
public class HomeController {
    @Value("${threshold}")
    private String threshold;

    @GetMapping("threshold")
    public String threshold() {
        return threshold;
    }
}
  • 增加配置文件bootstrap.properties

注意:不是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

  • 客户端添加依赖(pom.xml)
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>
  • 配置注解@RefreshScope
@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;
    }
}
  • 暴露refresh接口(application.properties)
management.endpoints.web.exposure.include=refresh
  • 通过git修改配置文件

  • 刷新配置

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

常见问题

  • 配置文件不存在:可能是没有配置spring.cloud.config.server.git.search-paths=config
  • refresh接口不存在:可能是没有引入spring-boot-actuator,也或者没有修改配置文件management.endpoints.web.exposure.include=refresh

参考

相关文章

网友评论

    本文标题:配置中心 - Spring Cloud Config

    本文链接:https://www.haomeiwen.com/subject/hskkpqtx.html