springcloud(8)配置中心Config

分布式配置中心


什么是配置中心及使用前后的好处 (画图)

       什么是配置中心:

              一句话:统一管理配置, 快速切换各个环境的配置

       相关产品:

              百度的disconf

                     地址:https://github.com/knightliao/disconf                    

              阿里的diamand

                     地址:https://github.com/takeseem/diamond

                     springcloud的configs-server:

                     地址:http://cloud.spring.io/spring-cloud-config/

推荐干货文章:http://jm.taobao.org/2016/09/28/an-article-about-config-center/


SpringCloud配置中心config-server

       1、新建项目,创建config-server

       2、启动类增加注解

              @EnableConfigServer


使用git服务器结合Config搭建分布式配置中心


1、默认使用git存储配置中心

       使用git服务器,可以自己搭建gitlab服务器

              或者使用github、开源中国git、阿里云git

                     794666918@qq.com

                     xdclass.net123

                     https://gitee.com/waitforxy/config_cloud.git

       2、配置文件添加配置

                     spring:

                       application:

                         name: config-server

                     #git配置

                       cloud:

                         config:

                           server:

                             git:

                               uri: https://gitee.com/waitforxy/config_cloud   ()

                               username: 794666918@qq.com

                               password: xdclass.net123

                               #超时时间

                               timeout: 5

                              #分支

                              default-label: master

      


3、访问方式(一定要注意语法,如果有问题,会出错

                     多种访问路径,可以通过启动日志去查看

                     例子 http://localhost:9100/product-service.yml

                     /{name}-{profiles}.properties

                     /{name}-{profiles}.yml

                     /{name}-{profiles}.json

                     /{label}/{name}-{profiles}.yml                               

                     name 服务器名称

                     profile 环境名称,开发、测试、生产

                     lable 仓库分支、默认master分支                                             

也能访问

加一个test分支


分布式配置中心客户端使用


官方文档:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html#_spring_cloud_config_client

             

       1、加入依赖
            

 <!--配置中心-->
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-client</artifactId>
</dependency>

(客户端启动不知道从哪拿数据,是先到注册中心拿数据还是先到配置中心拿数据)

          2、修改对应服务的配置文件,把application.yml 改为 bootstrap.yml(优先读取里面的配置选项)

                     #指定注册中心地址

                     eureka:

                       client:

                         serviceUrl:

                           defaultZone: http://localhost:8761/eureka/

                     #服务的名称

                     spring:

                       application:

                         name: product-service

                       #指定从哪个配置中心读取

                       cloud:

                         config:

                           discovery:

                             service-id: CONFIG-SERVER         (eureka)

                             enabled: true

                           profile: test

                           #建议用lable去区分环境,默认是lable是master分支

                           #label: test

              注意点:

                     1.配置文件要用bootstrap.yml

                     2.默认读取文件名是 服务名称

这里要去掉


服务启动时读取bootstrap,yml中的配置,读取配置之后,到注册中心读取配置中心的名称,读取配置,git中读取到配置()


webhook动态刷新

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智达教育‍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值