Spring Cloud Config集中式配置管理使用

  • Spring Cloud 集中式配置管理功能,是基于SVN或者Git版本控制系统来管理的(其实,本地的也可以)
  • 使用Spring Cloud集中配置管理,主要有Config Server 以及 Config Client两个部分
  • 本人实验的github地址:https://github.com/hfq-shimanqiang/spring-cloud-jedi

    创建Config Server服务

    依赖配置

    compile("org.springframework.cloud:spring-cloud-config-server")
    

    application.yml配置

    说明:spring.cloud.config.server.git.uri是Git仓库地址

    server:
      port: 18080
    spring:
      profiles:
        active: test
      cloud:
        config:
          server:
            git:
              uri: https://github.com/hfq-shimanqiang/hello-hello.git

    Application启动类配置

    @EnableConfigServer 是启动配置服务的关键注解

    @SpringBootApplication
    @EnableConfigServer
    @EnableDiscoveryClient
    public class CloudConfigServerApplication {
    
        /**
         * 参考
         * http://blog.csdn.net/liaokailin/article/details/51307215
         * <p>
         * 访问
         * http://localhost:18080/foo/test
         * http://localhost:18080/foo/test/master
         * 查看生效可用的配置@see bootstrap.yml
         *
         *
         * @param args
         */
        public static void main(String[] args) {
            SpringApplication.run(CloudConfigServerApplication.class, args);
        }
    }

    很简单的几个步骤就能启动一个配置管理服务了

    访问: http://localhost:18080/foo/test

    补充:在hello-hello项目中有一个文件:foo-test.yml

    地址: https://github.com/hfq-shimanqiang/hello-hello.git

    创建Config Client使用配置(不需要application.yml喽)

    bootstrap.yml配置

    spring:
      cloud:
        config:
          uri: http://localhost:18080
          label: master
          #http://localhost:18080/foo/test 的结果。对应下面属性:name 、profile
          name: foo
          profile: test
    

    Application启动类配置

    @SpringBootApplication
    public class CloudConfigClinetApplication {
    
        /**
         * 参考
         * http://nobodyiam.com/2016/04/02/dive-into-spring-cloud-config/
         * <p>
         * 访问:http://localhost:18081/env
         * 查看环境配置
         * <p>
         * PSOT方式访问:http://localhost:18081/refresh
         * 刷新git上的配置文件
         *
         * @param args
         */
        public static void main(String[] args) {
            SpringApplication.run(CloudConfigClinetApplication.class, args);
        }
    }
    

    很简单就不说了

    遇到的几个问题

  • 客户端暴露/env 以及 /refresh 访问,默认情况是不能访问的

    增加依赖配置

    compile ("org.springframework.boot:spring-boot-starter-actuator")

    配置application.yml

    management:
      security:
        enabled: false
  • 如果执行了refresh不能更新配置后的内容,需要在配置类上增加注解@RefreshScope

    @Component
    @ConfigurationProperties(prefix = "demo")
    @RefreshScope
    public class DemoConf {
        private String say;
    
        public String getSay() {
            return say;
        }
    
        public void setSay(String say) {
            this.say = say;
        }
    }
    

    实验情况,需要手动发post调用 refresh,最终可以通过Spring Bus解决处理,还需要具体的调研使用方法


    可能表述的不是很详细明白,具体的使用很容易,直接参考github上的地址即可
    不过在配置的过程中,因为资料不是很详,磕磕绊绊的,不过都解决了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值