Spring Cloud Finchley.SR1 的学习与应用 8

时间秒杀一切 提交于 2019-12-10 05:50:15

基于consul和git的配置中心

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

准备工作

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

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

关于配置文件,请看这里

添加配置中心模块

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

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

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

关于bootstrap.yml,请看这里

bootstrap.yml

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.yml

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
  • 测试

编写测试controller

@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来更新。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!