SpringCloud-config

Spring Cloud Config

简介

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production, you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git, so it easily supports labelled versions of configuration environments as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持。有了Config Server,您就有了一个中心位置来管理跨所有环境的应用程序的外部属性。客户机和服务器上的概念与Spring环境和PropertySource抽象完全相同,因此它们非常适合Spring应用程序,但可以用于以任何语言运行的任何应用程序.当应用程序通过部署管道从dev转移到测试并进入生产环境时,您可以在两者之间管理配置.服务器存储后端默认的实现使用git,因此它很容易支持配置环境的标记版本,并且可以访问各种各样的工具来管理内容。很容易添加替代实现并将它们插入Spring配置中

config-server

创建一个配置仓库

具体样式,可以看https://github.com/huskyui/config-repo

spring-cloud下面有三个文件,config-dev.ymlconfig-prod.ymlconfig-test.yml

导入pom

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

<!--spring-cloud.version Hoxton.SR3 -->

application.yml

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/huskyui/config-repo # uri
          search-paths: spring-cloud # 层级
server:
  port: 12000
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/
# 今天写defalutZone写错了,发现应用在请求loclahost:8761/eureka/,原来默认值是这个
      

@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
// 开启config-server
public class CloudConfigServerApplication {

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

}

测试

curl http://localhost:12000/config-dev.yml
currency:
  name: huskyui-update-2020-03-24-----2
config-client

pom

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
<!--spring-cloud-version Hoxton.SR3-->

bootstrap.yml

bootstrap.yml优先于application.yml配置

spring:
  cloud:
    config:
      label: master # 用于拉取远程配置属性的标签名称,在基于git的服务器,通常是master
      name: config # Name of application used to fetch remote properties. 对应config-dev.yml中的config
      profile: dev # The default profile to use when fetching remote configuration
      discovery:
        enabled: true #Flag to indicate that config server discovery is enabled
        service-id: config-server #Service id to locate config server.也就是config-server注册在eureka中的application-name
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/

application.yml

spring:
  application:
    name: config-git 
server:
  port: 13000
management:
  endpoints:
    web:
      exposure:
        include: refresh # 放开/actuator/refresh路径,具体是什么样式的请求,还得看文档

HelloController

@RestController
/**
 * Convenience annotation to put a <code>@Bean</code> definition in
 * {@link org.springframework.cloud.context.scope.refresh.RefreshScope refresh scope}.
 * Beans annotated this way can be refreshed at runtime and any components that are using
 * them will get a new instance on the next method call, fully initialized and injected
 * with all dependencies.
   在调用/actuator/refresh时,运行期间,这个bean会被刷新
*/
@RefreshScope
public class HelloController {
    @Value("${currency.name:error}")
    private String name;

    @RequestMapping("/hello")
    public String name(){
        return name;
    }

}

测试

1.测试单个应用刷新
打包运行
测试当前数据问题
curl localhost:13000/hello
huskyui-update-2020-03-24-----2
更新数据
更新数据config-dev里面的数据
currency:
  name: huskyui-update-2020-03-27
 并提交到远程仓库
调用刷新接口
curl -X POST localhost:13000/actuator/refresh
["currency.name","config.client.version"]
curl localhost:13000/hello
huskyui-update-2020-03-27
可以看到数据刷新成功
2.测试多个应用刷新
java -jar xxx.jar --server.port=13000
java -jar xxx.jar --server.port=13001
curl localhost:13000/hello
huskyui-update-2020-03-27
curl localhost:13001/hello
huskyui-update-2020-03-27
更新数据config-dev.yml
currency:
  name: huskyui-update-2020-03-27-for-two-client-test2
并提交到远程仓库
curl localhost:13000/hello
huskyui-update-2020-03-27
curl localhost:13001/hello
huskyui-update-2020-03-27
下面执行刷新操作
curl -X POST localhost:13000/actuator/refresh
["currency.name","config.client.version"]
curl localhost:12000/config-dev.yml
currency:
  name: huskyui-update-2020-03-27-for-two-client-test2
curl localhost:13000/hello
huskyui-update-2020-03-27-for-two-client-test2
curl localhost:13001/hello
huskyui-update-2020-03-27
curl -X POST localhost:13001/actuator/refresh
["currency.name","config.client.version"]
curl -X POST localhost:13001/hello
huskyui-update-2020-03-27-for-two-client-test2




可以看到,如果配置文件更新时,/actuator/refresh只是针对单个应用的,如果需要更新所有应用需要分别是去刷新对应的应用
使用spring-cloud-bus实现群体刷新

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jeIW5krD-1585555116625)(https://s1.ax1x.com/2020/03/30/GmFm2n.jpg)]

从上面的架构图,我们config-serverconfig-client都订阅一下RabbitMQ

我们需要修改原来的config-server

POM

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

application.yml添加

  spring:
    rabbitmq:
      host: localhost
      port: 5672
      username: guest
      password: guest  ## 上述是RabbitMQ相关连接参数,漏了一个vhost
  management:
    endpoints:
      web:
        exposure:
          include: "*" ## 放开/actuator/bus-refresh

config-client修改部分也和上述一样

修改后,启动config-server,并启动两台config-client,端口不同(–server.port=different_port)

我们访问http://localhost:15672/访问RabbitMQ客户端,点击exchange,可以看到是

springCloudBus,是topic类型,三个绑定该exchange的queue的routing key都是#,也就是不处理fanout

测试

首先修改config-repo,并提交
首先查看数据
D:\gitclonepackage\cloud>curl localhost:13000/hello
huskyui-update-2020-03-30
D:\gitclonepackage\cloud>curl localhost:13001/hello
huskyui-update-2020-03-30

调用刷新操作
curl -X POST localhost:12000/actuator/bus-refresh
这个请求的时候,是请求config-server;当然也可以请求config-client的bus-refresh

D:\gitclonepackage\cloud>curl localhost:13001/hello
huskyui-update-2020-03-30-last-commit
D:\gitclonepackage\cloud>curl localhost:13000/hello
huskyui-update-2020-03-30-last-commit

本篇文章参考:

Spring Cloud(七):配置中心(Git 版与动态刷新)
Spring Cloud(八):配置中心(服务化与高可用)

Spring Cloud(九):配置中心(消息总线)【Finchley 版】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值