Spring Cloud 实战(三)- Spring Cloud Config配置

Spring Cloud Config配置

线上环境,修改配置,动态加载是刚需。本节我将记录下如何运用Spring Cloud Config动态加载服务所需的配置,实现热加载的功能。

前期准备

Config 服务前期准备

Spring Cloud Config需要 git或者svn 作为配置的存储之处。我在 git 上建立了一个Config-Repo的工程,如下图:
在这里插入图片描述

这里需要了解一下,Spring boot启动时加载文件的顺序问题
在这里插入图片描述
在这里插入图片描述

具体可参考:翟永超的《Spring Cloud 微服务实战》一书,第25页。

其中 newproduct.yml 配置为:

spring:
  profiles:
    active: dev

newproduct-dev.yml 为:

#server
server:
  port: 9005
#spring
spring:
  application:
    name: newproduct
  datasource:
    name: SpringCloudSell
    type: com.alibaba.druid.pool.DruidDataSource
    #druid
    druid:
      filters: stat
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/SpringCloudSell?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
      username: zyx
      password: zyx
      initial-size: 1
      min-idle: 1
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20

#mybatis
mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.taeyeon

#logging
logging:
  level:
    com.taeyeon.category.dao: debug
    com.taeyeon.info.dao: debug
    
#test
girl:
  name: zwy
  age: 23
  
#boy
boy:
  name: zyx
  age: 18

Config服务配置

  1. pom 文件需要引入依赖:
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
  1. 启动类需要注解 @EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        System.out.println("++++++++++++++++++++++ConfigApplication Start+++++++++++++++++++++++++");
        SpringApplication.run(ConfigApplication.class, args);
        System.out.println("======================ConfigApplication Start is Done=========================");
    }
}
  1. application.yml 配置
server:
  port: 9001
#spring
spring:
  application:
    name: config

#uri 填写Config-Repo的 github 地址;如果Config-Repo是私有项目,则username和 Password 则填写你的
#github的用户名和密码; 否则,username和 Password不用配置
  cloud:
    config:
      server:
        git:
          uri: xxxx
          username: xxxx
          password: xxxx
#eureka
eureka:
  client:
    service-url:
      defaultZone: http://peers1:9010/eureka/,http://peers0:9009/eureka/
    fetch-registry: true
    register-with-eureka: true
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

#这里需要消息队列,实现将配置的更改信息推送到消息对列,然后消费者从消息队列中获取配置信息。
rabbitmq:
  addresses: localhost
  port: 15672
  username: guest
  password: guest

#主要用于开启refresh 功能
management:
  endpoints:
    web:
      exposure:
        include: "*"

至此,config 服务配置完毕,接下来配置config-client:newproduct服务。

newproduct服务配置

  1. pom 依赖
<!-- RabbitMQ-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
  1. bootstrap.yml 配置
    由于 newproduct需要从 config服务中拉去配置信息,因此 newproduct本地配置 bootstrap.yml,且它的加载优先级高于application.yml。
#spring
spring:
  application:
    name: newproduct
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG
      profile: dev

rabbitmq:
  addresses: localhost
  port: 15672
  username: guest
  password: guest

  #Eureka Server
eureka:
  client:
    service-url:
      defaultZone: http://peers0:9009/eureka/,http://peers1:9010/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  1. RefreshScope
    两种方式指定需要动态刷新的 bean:
  1. 调用类
    比如,我们现在需要在controller 层调用一个可以动态变化的参数girl的 age 字段(如 Config-Repo 中配置的 newproduct-dev.yml所示),调用的 controller 类如下所示:
@RestController
@RefreshScope
public class ConfigController {
    @Autowired
    private Environment env;

    @GetMapping("/girl")
    public Map<String,String> getGirl(){
        Map<String,String> map = new HashMap<>();
        map.put("Name",env.getProperty("girl.name"));
        map.put("Age",env.getProperty("girl.age"));
        return map;
    }
  1. 配置类(推荐)
@Data
@Component
@ConfigurationProperties("boy")
public class BoyConfig {
    private String name;
    private String age;
}

controller 调用:

@RestController
@RefreshScope
public class ConfigController {
@Autowired
private BoyConfig boyConfig;
  
@GetMapping("/boy")
public Map<String,String> getBoy(){
    Map<String,String> map = new HashMap<>();
    map.put("Name",boyConfig.getName());
    map.put("Age",boyConfig.getAge());
    return map;
}

刷新配置

前面的配置已经完成了大部分,但是若想要完成热加载,还需要调用 bus-refresh,两种方式:

  1. 手动
    github 上手动更改了 girl 或者 boy 的信息后,本地调用config服务的 bus-refresh 请求:
    curl -v -X POST “http://localhost:9001/actuator/bus-refresh

  2. webhooks
    github、gitee 等都有 webhooks 相关功能,更多功能可百度、谷歌。。。。。。
    我用的是 github,若想实现修改配置保存成功后就发送一个 post 请求到本地,那就必须要有一个公网的地址和本地地址映射,大家可以了解下 natapp.cn,可以免费申请一个临时的地址。
    在这里插入图片描述

PayLoad URL 填写 http://xxxx/monitor ,其中 xxxx 即为你申请到的地址。

End…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值