- 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
创建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上的地址即可
不过在配置的过程中,因为资料不是很详,磕磕绊绊的,不过都解决了
Spring Cloud Config集中式配置管理使用
最新推荐文章于 2024-07-15 02:21:12 发布