前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >配置中心 - Spring Cloud Config

配置中心 - Spring Cloud Config

作者头像
十毛
发布2019-03-27 15:21:46
4640
发布2019-03-27 15:21:46
举报

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

特性

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

搭建配置中心服务器


  • pom.xml
代码语言:javascript
复制
<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>
  • 通过注解启动配置中心
代码语言:javascript
复制
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)
代码语言:javascript
复制
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
  • 创建仓库
代码语言:javascript
复制
# 创建仓库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后访问配置信息(http://localhost:7001/blog-default.properties)

体验配置文件

代码语言:javascript
复制
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties

客户端配置


  • pom.xml
代码语言:javascript
复制
<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
代码语言:javascript
复制
@RestController
public class HomeController {
    @Value("${threshold}")
    private String threshold;

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

注意:不是__application.properties

代码语言:javascript
复制
spring.application.name=blog
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001
  • 体验配置 http://localhost:8080/threshold

测试配置参数

配置实时生效


借助Spring Boot的Actuator的接口/refresh

  • 客户端添加依赖(pom.xml)
代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>
  • 配置注解@RefreshScope
代码语言:javascript
复制
@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)
代码语言:javascript
复制
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

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.03.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 搭建配置中心服务器
  • 客户端配置
  • 配置实时生效
  • 常见问题
  • 参考
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档