【Spring Boot】Spring Boot之整合Apollo配置中心
一、Apollo配置中心简单介绍
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
二、整合步骤
1)添加Maven依赖
<!-- apollo --> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.6.0</version> </dependency>
2)添加apollo配置在application.properties或bootstrap.properties
非特殊场景下只需要这两个配置
# 注入默认application namespace的配置示例 apollo.bootstrap.enabled=true # Apollo application appid app.id=001 # 指定Apollo Meta Server (默认http://apollo.meta)(默认Meta server与config server地址一样) apollo.meta=http://localhost:8080
其他配置:
server.port=9090 #1、 注入默认application namespace的配置示例 apollo.bootstrap.enabled=true # Apollo application appid app.id=001 # 指定Apollo Meta Server (默认http://apollo.meta)(默认Meta server与config server地址一样) apollo.meta=http://localhost:8080 #2、注入默认application namespace或多个非默认namespace的配置示例 #apollo.bootstrap.namespaces = application,TEST1.public.yml,TEST2.app002_public #3、将Apollo配置加载提到初始化日志系统之前(1.2.0+) (默认false) # 使Apollo的加载顺序放到日志系统加载之前,不过这会导致Apollo的启动过程无法通过日志的方式输出 #apollo.bootstrap.eagerLoad.enabled = true # 指定apollo cluster (默认default) #apollo.cluster=SomeCluster # 指定环境 # 如果是运行jar文件,需要注意格式是java -Denv=YOUR-ENVIRONMENT -jar xxx.jar # 设置内存中的配置项是否保持和页面上的顺序一致(默认false) #apollo.property.order.enable=true # 配置访问秘钥 #apollo.accesskey.secret=1cf998c4e2ad4704b45a98a509d15719 # Spring应用通常会使用Placeholder来注入配置,使用的格式形如${someKey:someDefaultValue},如${timeout:100}。冒号前面的是key,冒号后面的是默认值。 # 建议在实际使用时尽量给出默认值,以免由于key没有定义导致运行时错误。 # 从v0.10.0开始的版本支持placeholder在运行时自动更新,而@ConfigurationProperties如果需要在Apollo配置变化时自动更新注入的值,需要配合使用EnvironmentChangeEvent或RefreshScope # 通过设置false来关闭placeholder在运行时自动更新功能(默认true) #apollo.autoUpdateInjectedSpringProperties=false
注意:对于Meta Server的是内网地址,本地开发环境无法直接连接的情况
直接指定Config Service地址的方式来跳过Meta Server服务发现
如果是运行jar文件,需要注意格式是java -Dapollo.configService=http://localhost:8080 -jar xxx.jar
3)从Apollo中获取配置
1、Spring Placeholder方式即@Value(${key})方式,该方式支持属性自动更新(即Apollo属性更新后会马上通知应用更新)
@Value("${v1:defaultValue}") public String v1;*
2、Spring Boot提供的@ConfigurationProperties使用方式,该方式默认不会自动更新,如果需要在Apollo配置变化时自动更新注入的值,需要配合使用EnvironmentChangeEvent或RefreshScope。
@Configuration @ConfigurationProperties(prefix = "demo") @Data public class ApplicationProperties { public String demo1; public String demo2; public String demo3; public String demo4; }
3、Apollo API使用方式
* 获取默认namespace的配置
* 获取非默认的配置
* 监听配置变化事件
@GetMapping("test2") public String test2() { // 获取默认namespace的配置(application),key为v1的值 Config config = ConfigService.getAppConfig(); String someKey = "v1"; String someDefaultValue = "defaultValue"; String value = config.getProperty(someKey, someDefaultValue); System.out.println(value); Config publicConfig = ConfigService.getConfig("TEST1.public.yml"); String someKey2 = "public_key"; String someDefaultValue2 = "defaultValue"; String value2 = publicConfig.getProperty(someKey2, someDefaultValue2); System.out.println(value2); // 非yaml/yml/properties格式的namespace,获取方式 ConfigFile configFile = ConfigService.getConfigFile("TEST1.public", ConfigFileFormat.YML); String content = configFile.getContent(); System.out.println(content); // 监听配置变化事件 config.addChangeListener(new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { System.out.println("Changes for namespace " + changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType())); } } }); return v1; }
4、Apollo Annotation使用方式
* @ApolloConfig
用来自动注入Config对象
* @ApolloConfigChangeListener
用来自动注册ConfigChangeListener
* @ApolloJsonValue
用来把配置的json字符串自动注入为对象
@RestController public class ApolloAnnotionUse { // 注入默认的名称空间配置 @ApolloConfig private Config config; @ApolloConfig("application") private Config anotherConfig; // 注入指定的名称空间配置 @ApolloConfig("TEST1.public.yml") private Config yetAnotherConfig; // 用来把配置的json字符串自动注入为对象 @ApolloJsonValue("${jsonBeanProperty:[]}") private List<String> anotherJsonBeans; @Value("${v1:defaultValue}") public String v1; // 监控默认空间的 @ApolloConfigChangeListener private void someOnChange(ConfigChangeEvent changeEvent) { //update injected value of batch if it is changed in Apollo if (changeEvent.isChanged("v1")) { v1 = config.getProperty("v1", "100"); } } // 监控指定空间的 @ApolloConfigChangeListener("application") private void anotherOnChange(ConfigChangeEvent changeEvent) { //do something } // 监控指定空间的 @ApolloConfigChangeListener({"application","TEST1.public.yml"}) private void anotherOnChangeMuch(ConfigChangeEvent changeEvent) { //do something } @GetMapping("annoTest") public void test() { System.out.println( config.getProperty("v1", "0")); System.out.println( anotherConfig.getProperty("v1", "0")); System.out.println( yetAnotherConfig.getProperty("publicKey", "0")); System.out.println(v1); System.out.println(anotherJsonBeans); } }
相关文章
- Spring boot项目分环境Maven打包,动态配置文件,动态配置项目
- 利用 Spring Boot 中的 @ConfigurationProperties,优雅绑定配置参数
- 【Spring Boot】Spring Boot之两种引入spring boot maven依赖的方式
- Spring Boot 2.0 配置图文教程
- spring boot:actuator的安全配置:使用spring security做ip地址限制(spring boot 2.3.2)
- spring boot: 设计接口站api的版本号,支持次版本号(spring boot 2.3.2)
- Spring Boot下配置MyBatis多数据源
- intellij idea 无法启动或调试 spring-boot
- Spring Boot使用Druid和监控配置
- Spring自定义标签解析
- spring boot:spring security给用户登录增加自动登录及图形验证码功能(spring boot 2.3.1)
- spring boot配置dubbo(XML)
- Spring Boot连接多个Redis库配置方案及代码示例
- spring boot使用yaml替代properties
- Spring Boot:定制拦截器
- 通过@Enable*注解触发Spring Boot配置
- Spring-Boot + Mybatis 多数据源配置
- 探索Spring和Spring Boot的异同:从入门到精准,快速掌握双方的区域和应用场景
- 【超详细demo】Spring Boot 多数据源配置
- Spring Boot整合Scheduled定时任务器、整合Quartz定时任务框架
- 如何使用Spring Cloud搭建高可用的Elasticsearch集群?详解Elasticsearch的安装与配置及Spring Boot集成的实现