Spring Cloud Config配置中心

转自:https://www.jianshu.com/p/1706831ee897

Spring Cloud Config配置中心

Spring Cloud Config是Spring-Cloud用于分布式配置管理的组件,分成Server和Client两种角色。Config-Server负责配置文件的分布式存储和管理,并通过HTTP接口对外提供Config-Client访问;Config-Client通过接口获得配置文件,加载到启动上下文并在应用中使用。

Config-Server端

1. Config-Server依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

2. 准备被管理的配置文件

Spring-Cloud-Config提供了对多种环境配置文件的支持,比如:开发环境,测试环境,生产环境等;为了更加全面的模拟,准备三个配置分别如下:

config-dev.properties
config-test.properties
config-pro.properties

3. 准备启动配置文件

被管理的配置文件可以来自多个地方,包括:本地文件,远程Git仓库以及远程Svn仓库,下面分别在resources/application.properties中做配置;

  • 本地文件
spring.application.name=config-server
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=file:E:/github/spring-cloud-config-repo
  • 远程Git仓库
spring.application.name=config-server
server.port=8888
spring.profiles.active=git
spring.cloud.config.server.git.uri=https://github.com/ksfzhaohui/spring-cloud-config-repo
spring.cloud.config.server.git.default-label=master
  • 远程svn仓库
spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=https://NJD9YZGJ2-PC.99bill.com:8443/svn/spring-cloud-config-repo
spring.cloud.config.server.svn.username=root
spring.cloud.config.server.svn.password=root
spring.cloud.config.server.svn.default-label=

4. 配置启动入口

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer.class, args);
    }
}

@EnableConfigServer 注解启动配置中心。

5. 请求统一配置文件

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

例如:http://localhost:8888/config-dev.yml

Config-Client端

1. Config-Client依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

2. 配置Config配置中心

在配置文件resources/bootstrap.properties中做如下配置:注意:bootstrap.properties的加载优先与application.properties

spring.application.name=config
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri= http://localhost:8888/

spring.application.name:对应{application},本实例中是config;
spring.cloud.config.label:对应{label},指定server端配置的分支,此处填master即可;
spring.cloud.config.profile:对应{profile},指定client当前的环境,可选值:dev,test,pro;
spring.cloud.config.uri:server端地址;

3. 非bootstrap.properties配置文件中使用配置中心

在application.properties 或者其他properties中可以使用占位符的形式使用统一配置中心中配置的属性。
例如:application.properties 使用配置项foo

app.foo=${foo}

4. 在代码中直接引用配置属性

允许在所有Spring Bean中通过使用统一配置的属性。
例如:在HelloController中使用foo配置属性

@RestController
@RequestMapping(value = "/hello")
public class HelloController {

    @Value("${foo}")
    private String foo;
}

安全配置

1. 引入安全依赖

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

2. 配置安全用户名和密码

修改application.properties添加安全用户名和密码配置

management:
  security:
    enabled: false
security: # 配置中心的用户名密码
  basic:
    enabled: true
  user:
    name: admin
    password: admin

3. Config-Client端修改Server.uri地址

修改bootstrap.properties中的config配置如下:

spring.cloud.config.uri= http://admin:admin@127.0.0.1:8080/

Spring-Cloud-Config配置的更新

手动调用刷新接口

    1. 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>
    1. 暴露全部endpoints
      在bootstrap.properties中添加
management.endpoints.web.exposure.include=*
    1. 修改HelloController
@RefreshScope
@RestController
public class HelloController {
 
    @Value("${foo}")
    String foo;
 
    @RequestMapping(value = "/hello")
    public String hello() {
        return foo;
    }
}

@RefreshScope 注解在手动执行刷新时,会更新改注解下的变量。

    1. 手动调用刷新接口
http://localhost:8889/actuator/refresh

消息总线自动更新



作者:自负的鱼
链接:https://www.jianshu.com/p/1706831ee897
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/sharpest/p/13706691.html