Spring Cloud Finchley.SR1 的学习与应用 8 - 基于consul和git的配置中心

基于consul和git的配置中心

在《Spring Cloud Finchley.SR1 的学习与应用 2 - Consul》中讲述来consul和git2consul的安装。这边文章讲述如下基于基于consul和git的配置中心。html

准备工做

启动consul server 和 git2consul,在git仓库中新建common-server-config-respo模块,用来存储配置文件。将common-server-config-respo/business-a-woqu/application.yml文件推送至git仓库。application.yml内容以下:java

extend:
  info:
    desc: i am business a dev modify

关于配置文件,请看这里git

添加配置中心模块

以server-businessa-woqu为例,添加配置中心模块web

  • POM
    在pom.xml中加入如下依赖:
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
  • 配置文件

分离配置文件,创建application.yml、bootstrap.yml两个配置文件spring

关于bootstrap.yml,请看这里json

bootstrap.ymlbootstrap

spring:
  application:
    name: business-a-woqu
  cloud:
    consul:
      host: woqu.consul
      port: 8500
      discovery:
        #instance-id: ${spring.application.name}:${server.port}
        instance-group: ${spring.application.name}
        register: true

      config:
        enabled: true   #默认是true
        format: YAML  # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
        fail-fast: true
        watch:
          enabled: true
        default-context: ${spring.application.name} #指定consul配置的配置文件父路径
        # 指定consul配置的文件夹前缀为config
        prefix: woqu_configuration/master/common-server-config-respo
        data-key: application.yml


#consul config 路径:prefix defaultContext data-key

server:
  port: 9001

application.ymlapp

feign:
  hystrix:
    enabled: true


management:
  endpoints:
    web:
      exposure:
        include: "*"
        exclude: dev

logging:
  level:
    root: info
    com.woqu: debug

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 10000
  ReadTimeout: 60000
  • 测试

编写测试controllerspring-boot

@RestController
public class DescController {

    @Value("${extend.info.desc:error}")
    private String desc;

    @GetMapping("/desc")
    public String desc() {
        return desc;
    }
}

启动consul server,server-businessa-woqu,发送请求:学习

GET http://127.0.0.1:9001/desc

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Tue, 20 Nov 2018 07:51:26 GMT

i am business a dev modify

Response code: 200; Time: 176ms; Content length: 26 bytes

开启更新机制

须要给加载变量的类上面加载@RefreshScope,在客户端执行/actuator/refresh的时候就会更新此类下面的变量值。

@RestController
@RefreshScope
public class DescController {

    @Value("${extend.info.desc:error}")
    private String desc;

    @GetMapping("/desc")
    public String desc() {
        return desc;
    }
}

启动consul server,server-businessa-woqu,发送请求:

GET http://127.0.0.1:9001/desc

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Tue, 20 Nov 2018 07:51:26 GMT

i am business a dev modify

Response code: 200; Time: 176ms; Content length: 26 bytes

更改common-server-config-respo/business-a-woqu/application.yml中信息:

extend:
  info:
    desc: i am business a dev modify after commit

请求刷新接口

POST http://127.0.0.1:9001/actuator/refresh

HTTP/1.1 200 
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 20 Nov 2018 07:54:01 GMT

[]

Response code: 200; Time: 710ms; Content length: 2 bytes

发送请求:

GET http://127.0.0.1:9001/desc

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 39
Date: Tue, 20 Nov 2018 08:07:44 GMT

i am business a dev modify after commit

Response code: 200; Time: 11ms; Content length: 39 bytes

以上是经过git commit来实现更新的,也能够在consul控制台直接更改value来更新。 Hystrix Dashboard

相关文章
相关标签/搜索