Config配置中心搭建

简介:SpringCloud Config 为微服务架构中心的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供了一个中心化的外部配置;
怎么玩:
SpringCloud Config分为服务端和客户端两部分。
服务端也成为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口;

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务默认采用git来存储配置信息,这样就有助于对环境进行版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容;


服务端:
pom:

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

yml:

server:
   port:8080
spring:
   application:
     name:cloud-config-center
   cloud:
     server:
           git:
             uri:git@github.com:aaa/springcloud-config.git 
             search-paths:
               - springcloud-config
    label:master     #读取分支
eureka:
  client:
    service-url:
      defaultZone:http://localhost:7001/eureka   

启动类:
@EnableConfigServer
windows修改hosts文件,增加映射:127.0.0.1:config-8080.com
测试:http://config-8080.com:8080/master/config-dev.yml


客户端配置:
pom:

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

yml:

server:
   port:8081
spring:
   application:
      name:config-client
   cloud:
   #客户端配置
     config:
       label:master #分支名称
       name:config #配置文件名称
       profile:dev #读取环境名称
       uri:http://localhost:8080 #配置中心地址
  #服务注册到eureka地址
  eureka:
    client:
      service-url:
        defaultZone:http://localhost:7001/eureka    
   management:
  endpoints:
    web:
      exposure:
        include: "*" 

Config动态刷新
pom:

  <dependency>
    <groupId>org.springframework.boot<groupId>
    <artifactId>spring-boot-starter-actuator<artifactId>
  <dependency>

yml:
#暴露监控端点

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

@RefreshScope 业务类Controller的修改

@RestController
@RefreshScope
public class ConfigClientController{
   @Value(“${config.info})
   private String configInfo;
   @GeetMapping("/configInfo")
   public String getConfigInfo()
   {
      return configInfo;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值