springcloud config总结

1.config 服务端(即配置中心)配置(application.yml)

  • 【需要spring-cloud-config-server依赖】

1.1application.yml配置文件中加入

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: git@github.com:DearMrJ/springcloud-config.git
          search-paths:
            - springcloud-config
      label: master
#      username: # git 仓库用户名(公开库可以不用填写)
#      password: # git 仓库密码(公开库可以不用填写)
  • 如果报错Cannot clone or checkout repository:
    git@github.com:xxxx,则可能是未配置ssh连接,配置流程如下:
  		I.首先在本地创建 SSH Keys
  			$ ssh-keygen -t rsa -C "xx@yy.com"
  			成功的话会在 ~/(即C:/users/[username/]) 下生成 .ssh文件夹,进去,
  			打开 id_rsa.pub(可以在git bash中使用cat输出),复制里面的key.
  			
  		II.到GitHub上添加ssh key(右上角个人头像->settings->SSH and GPG keys->new SSH keys->粘入id_rsa.pub内容)
  		
  		III.验证 SSH Keys 是否添加成功
  			$ ssh -T git@github.com

1.2主程序类@EnableConfigServer启用配置服务

1.3访问测试

http://config-3344.com:3344/master/config-prod.yml(hosts文件配置域名解析)

2.config 客户端配置(bootstrap.yml)

  • 【需要spring-cloud-starter-config依赖】,区别于服务端(配置中心)

2.1bootstrap.yml配置文件中加入

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # 分支名称
      name: config #配置文件名称
      profile: dev # 读取的后缀,上述三个综合,为master分支上的config-dev.yml的配置文件被读取,http://config-3344.com:3344/master/config-dev.yml
      uri: http://config-3344.com:3344 #配置中心的地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

2.2【Config客户端之动态刷新】

2.2.1半自动动态刷新
  1. POM引入actuator监控

  2. 修改bootstrap.yml,暴露监控端口

    management:  
     endpoints:
        web:
          exposure:
            include: "*" 
    
  3. 【业务类Controller@RefreshScope】

  4. 【需要运维人员发送POST请求刷新3355/3366…】
    curl -X POST "http://localhost:3355/actuator/refresh"

2.2.2【全自动动态刷新】
  • 需要【Springcloud-bus消息总线】,springcloud-bus目前只支持rabbitmq和kafka;

  • 在配置中心和各个客户端加入【spring-cloud-starter-bus-amqp】,伴随着【spring-boot-starter-actuator】,因为在暴露bus刷新配置(见yml)的端点时候,需要actuator模块的依赖。

  1. 配置中心application.yml如下:
server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: git@github.com:DearMrJ/springcloud-config.git
          search-paths:
            - springcloud-config
      label: master
#      username: # git 仓库用户名(公开库可以不用填写)
#      password: # git 仓库密码(公开库可以不用填写)
  rabbitmq:
    host: 192.168.66.66
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
# rabbitmq相关配置,暴露bus刷新配置的端点,因此需要actuator模块的依赖
management:
  endpoints:  # 暴露bus刷新配置的断电
    web:
      exposure:
        include: "bus-refresh"
  1. 客户端bootstrap.yml配置如下:
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # 分支名称
      name: config #配置文件名称
      profile: dev # 读取的后缀,上述三个综合,为master分支上的config-dev.yml的配置文件被读取,http://config-3344.com:3344/master/config-dev.yml
      uri: http://config-3344.com:3344 #配置中心的地址
  rabbitmq: #rabbitmq相关配置,15672是web管理端口,5672是mq访问端口
    port: 5672
    host: 192.168.66.66
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. 全局广播
curl -X POST "http://localhost:3344/actuator/bus-refresh

即通知配置中心,进行广播。

  1. 定点通知
curl -X POST "http://localhost:3344/actuator/bus-refresh/{applicationName}:{port}"
如:
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值