SpringCloud Config的使用

1 是什么

Spring Cloud Config:服务配置中心,为服务端和客户端提供了分布式系统的外部化配置支持。

2 怎么用

2.1 创建Config Server工程

本工程也整合了Eureka作为服务注册中心,不熟悉的可参考之前文章SpringCloud Eureka的使用
1 在git或gitee上创建配置仓库,复制地址,我是在gitee上
创建配置文件:
根据不同环境配置了三个文件名称:
order-service.properties
order-service-dev.properties
order-service-test.properties
其中设置了一个hello属性,为每个配置文件分别设置了不同的值,如:
hello:xiaohou
hello:dev
hello:test

2 引入依赖:config-server,因为我的工程有父控,所以没有version

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

3 application.yml配置文件中填写git地址,用户名,密码

server:
  port: 9091
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
        # 配置仓库地址
          uri: https://gitee.com/niupangpang/spring-cloud-config-server2.git
          username: username
          password: password

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9090/eureka

4 程序启动类加入@EnableConfigServer

@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServer9091Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigServer9091Application.class, args);
    }

}

2.2 Config Client获取配置

1 引入依赖:config client

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

2 创建bootstrap.properties或bootstrap.yml配置,来指定config server

spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: spring-cloud-config-server
       #获取的配置文件名称,不写,默认查找服务应用名称
      name: order-service
      profile: dev
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9090/eureka

这里需要格外注意:上面这些属性必须配置在bootstrap.yml中,config部分内容才能被正确加载。因为config的相关配置会先于application.yml,而bootstrap.yml的加载也是先于application.yml。
说明:
URL与配置文件的映射关系如下:
{application}/{profile}[/{label}]
{application}-{profile}.yml
{label}/{application}-{profile}.yml
{application}-{profile}.properties
{label}/{application}-{profile}.properties

如果 profile不配置,默认查找application.properties

3 创建一个Contrroller 来返回配置中心的属性

@RestController
public class OrderController {
    @Value("${hello}")
    private String txt;
    @GetMapping("/config")
    public String config(){
        return txt;
    }
}

通过@Value("${hello}") 就可以查看配置服务中的hello属性值了

4 启动Eureka-Server、Config-Server、Config-Client
浏览器输入地址:
http://localhost:8082/config
在这里插入图片描述

3 动态配置刷新

如果gitee上配置文件内容更新,config Client端并不会实时刷新,如果需要实时动态刷新需要结合其他组件

3.1 使用actuator

1 在Config Client工程增加依赖:

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

2 在application.yml中启动actuator刷新端点

#启用端点refresh
management:
  endpoints:
    web:
      exposure:
        include: refresh

3 Controller类 增加注解@RefreshScope

@RestController
@RefreshScope
public class OrderController {
    @Value("${hello}")
    private String txt;
    @GetMapping("/config")
    public String config(){
        return txt;
    }
}

借助Postman等工具,使用post请求访问:http://localhost:8082/actuator/refresh
弊端: 只会刷新当前客户端的配置文件,其他客户端的并不会刷新,微服务架构体系下如果模块过多,操作麻烦

3.2 使用Spring Cloud Bus

利用消息中间件kafka或rabbitMQ,我使用的是kafka,安装过程可参考Kafka-安装教程
1 在Config Server工程增加依赖:

<dependency>
       <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2 application.yml配置文件增加bus和kafka的相关配置:

server:
  port: 9091
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/niupangpang/spring-cloud-config-server2.git
          username: 123
          password: 123
    bus:
      enabled: true
      trace:
        enabled: true
      refresh:
        enabled: true
  kafka:
    bootstrap-servers: 192.168.222.222:9092
    consumer:
      group-id: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9090/eureka
#开启bus-refresh端点
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

1 在Config Client工程增加依赖:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency

2 配置文件增加bus及kafka相关配置:

server:
  port: 8082
spring:
  application:
    name: spring-cloud-order-service
  cloud:
    bus:
      enabled: true
  kafka:
    bootstrap-servers: 192.168.222.222:9092
    consumer:
      group-id: order-service
#启用端点refresh
management:
  endpoints:
    web:
      exposure:
        include: refresh

借助Postman等工具,使用post请求访问:http://localhost:9091/actuator/bus-refresh。其中9091是config-server工程的端口,会刷新所有客户端的配置文件
弊端:虽然刷新了所有客户端配置文件,但是需要手动触发请求,还是不能自动刷新,可以借助gitee上的WebHooks进行自动触发请求,在这不演示了。

4 源码地址

https://gitee.com/niupangpang/spring-cloud-netflix.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值