目录

 

一.统一配置中心

1.创建github配置中心

2.将配置中心文件引入项目中去

3.springcloud Bus自动刷新配置


一.统一配置中心

  • 集中管理配置
  • 不同环境不同配置
  • 运行期间可动态调整
  • 配置修改之后可自动更新

1.创建github配置中心

新建项目config,加入spring-cloud-config和eureka-client依赖。
在启动类上加入
@EnableConfigServer
在配置文件中加入

#github配置中心地址
spring.cloud.config.server.git.uri=https://github.com/yiyuan-wangyu/spring-cloud-config.git
#github账号
spring.cloud.config.server.git.username=yiyuan-wangyu
#github密码
spring.cloud.config.server.git.password=xxxxxxxxxx

在github的config项目中添加之前的customer配置信息,文件名为customer.properties

server.port=8764
spring.application.name=feign-customer
#断路器
feign.hystrix.enabled=true
#management.endpoint.web.expose="*"
service-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

#配置hystrix的超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

这时,基本配置就已经完成了。直接访问http://192.168.1.140:8901/customer.yml是无效的,必须按照配置访问规则来查看
/{name}-{profiles}.yml
/{label}/{name}-{profiles}.yml
其中.yml也可以替换为.properties。会自动进行转换
name:服务名
peofiles:环境
label:分支(默认master分支)

访问http://192.168.1.140:8901/customer-12dev123.yml可以查看customer.properties文件

SpringCloud(七)统一配置中心

这时候在git中创建新配置文件customer-dev.properties
SpringCloud(七)统一配置中心

添加配置代码

logging.level.com.netflix=DEBUG 

访问http://192.168.1.140:8901/customer-dev.yml可以查看customer-dev.properties文件,并且可以发现,其配置会将customer.properties的内容也包含进来。那是因为{name}.yml文件在配置中心是作为公共配置存在的。
SpringCloud(七)统一配置中心

这时候查看控制台输出,会发现其实config组件会在本地生成配置文件。
SpringCloud(七)统一配置中心

如果需要手动配置文件生成路径,只需要增加

#github配置中心地址
spring.cloud.config.server.git.uri=https://github.com/yiyuan-wangyu/spring-cloud-config.git
#github账号
spring.cloud.config.server.git.username=yiyuan-wangyu
#github密码
spring.cloud.config.server.git.password=xxxxxxxxxx
#自定义配置文件生成路径
spring.cloud.config.server.git.basedir=/Users/wangyu/IdeaProjects/wangyu/springcloud/config/baseUrl

重启项目,访问后查看控制台,会发现文件生成路径已经改变
SpringCloud(七)统一配置中心


2.将配置中心文件引入项目中去

将之前上传到git的配置文件引入到customer微服务中去。首先引入依赖

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

删除application配置文件,增加bootstrap.properties配置文件

eureka.client.service-url.defaultZone= http://localhost:8761/eureka/
#对应配置{name}
spring.application.name=customer
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config
#{对应配置profile}
spring.cloud.config.profile=dev

启动即可。这时候访问customer微服务的接口http://localhost:8764//hi?name=测试SpringCloud(七)统一配置中心

一切OK。
PS:config的高可用,布置多个config服务,其他微服务从config中获取配置信息的时候,会负载均衡处理。
配置文件加载顺序:首先会通过eureka找到config实例(注册eureka需要在本地进行),然后通过config配置去github上获取配置信息,再根据application.name进行配置文件分配。

3.springcloud Bus自动刷新配置

导入springcloud-bus(config服务端可客户端都需要导入)依赖,安装rabbit

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

开放bus-refresh接口

#spring-cloud-bus
management.endpoints.web.exposure.include=*

使用post方法json类型访问接口http://localhost:8901/actuator/bus-refresh即可刷新配置。
另外,如果在代码中引用来配置数据,还需要增加@RefreshScope注解来辅助刷新。


通过github自动刷新
SpringCloud(七)统一配置中心
SpringCloud(七)统一配置中心

确定就OK了。

相关文章:

  • 2022-12-23
  • 2021-08-08
  • 2021-05-21
  • 2022-01-17
  • 2021-10-25
  • 2021-12-24
猜你喜欢
  • 2022-12-23
  • 2021-12-20
  • 2021-11-15
  • 2021-10-19
  • 2021-11-19
  • 2021-05-24
  • 2022-01-19
相关资源
相似解决方案