springcloud学习 第九章:配置中心config

下载源码:https://gitee.com/ctocloud/spring-cloud-poetry.git

1、定义

       在分布式系统中,由于有很多系统组成,那么管理这些系统的配置文件就显得比较麻烦。

spring cloud config 为了解决这个问题提供了文件管理中心用来集中管理外部文件。

也同时支持从开发环境到测试环境到生成环境不同配置的管理及切换。

文件管理中心默认集成git,能很容易支持不同版本的配置文件,也可以实现在线配置的更改。

 

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

Http服务有以上格式的资源,application 注入的是 config.name,label是git标签,默认是master。

 

2、例子

例子源码请参见

 

3、源码及测试

3.1、首先新建项目 cloud-config,pom文件需要增加依赖spring-cloud-config-server

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

3.2、然后 application.properties

server:
  port: 8889

spring:
  application:
    name: cloud-config
  cloud:
      config:
        server:
          git:
            uri: https://github.com/ctocloud/sping-cloud-poetry.git
            search-paths: config
            #username: username
            #password: password
        label: master

# 访问git仓库的用户名
#spring.cloud.config.server.git.username=xxxxoooo
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
#spring.cloud.config.server.git.password=xxxxoooo

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/

git.uri:配置文件所在git 仓库地址

git.searchPaths:配置文件所在项目目录路径

config.label:git项目所在分支

git.username:如果是私有仓库,git账号

git.password:如果是私有仓库,git密码

java 代码 ConfigApplication

 

3.3、@EnableConfigServer:启动配置服务的注解

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class  ConfigApplication {


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

3.4、启动项目,访问项目路径http://localhost:8888//foo/dev,说明配置服务启动成功。

 

 

3.5、新建客户端 cloud-config -client,pom文件需要引入依赖 spring-cloud-starter-config。.

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

3.6、配置文件 bootstrap.properties

spring.application.name=cloud-config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.name=config-client

eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=cloud-config
server.port=8889

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh

 

3.7、java 代码 ConfigClientApplication

启动 cloud-config-client,访问http://localhost:8882/hi,显示参数值,说明请求成功。

 

 

4、实时刷新配置文件

服务中心是利用 spring cloud bus 来实现节点文件变更的通知。

首先我们需要启动 rabbitmq 客户端

 

然后在 cloud-config-client 的pom文件增加依赖 spring-cloud-starter-bus-amqp。

 

修改 bootstrap.properties 文件。

spring.rabbitmq.host:消息服务的服务名

spring.rabbitmq.port:端口

spring.rabbitmq.username:账号

spring.rabbitmq.password:密码

 

测试用例:

启动客户端两个实利,8882,8883,这时候可先去git修改变量值,

再执行localhost:8882/actuator/bus-refresh,来刷新配置中心的值。

这时候再访问localhost:8882/hi,发现读取的变量值以改变。

再访问locahost:8883/hi,变量值也已经修改,

说明消息已经通知节点8883更新了配置。

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值