安装参考:
SpringCloud集成Apollo分布式配置中心 https://www.jianshu.com/p/5606483c7fbf
整合参考:
Spring Boot 2.0 整合携程Apollo配置中心 https://www.jianshu.com/p/23d695af7e80
pom.xml
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </dependency> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.1.2</version> <!--<version>1.1.0</version>--> <!--<version>1.2.0</version>--> </dependency> |
application.properties
1 2 3 4 5 |
server.port=8088 logging.level.root=DEBUG redis.cache.expireSeconds=1 redis.cache.commandTimeout=2 |
resources/META-INF/app.properties
1 2 3 4 5 6 7 8 |
#应用ID(在Apollo服务端新增项目添加的应用ID) app.id=demo #apollo-configservice地址 apollo.meta=http://localhost:8080 #运行时自动更新功能 apollo.autoUpdateInjectedSpringProperties=true |
JavaConfigBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.example.sc.apollo.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; /** * Java Config方式 */ @Configuration public class JavaConfigBean { @Value("${timeout:20}") private int timeout; public int getTimeout() { return timeout; } } |
SampleRedisConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.example.sc.apollo.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Configuration; /** * ConfigurationProperties使用方式 * TODO: 为什么不能更新到这里的配置 * 注: @ConfigurationProperties如果需要在Apollo配置变化时自动更新注入的值, * 需要配合使用EnvironmentChangeEvent或RefreshScope * 1. 配置类加入@RefreshScope * 2. 使用配置 @Autowired SampleRedisConfig sampleRedisConfig; * 3. 在 @ApolloConfigChangeListener 监听配置变化的地方刷新配置: refreshScope.refresh("sampleRedisConfig"); * 当配置中心改变的时候,这些配置就能实时更新了!!! * */ @Configuration @ConfigurationProperties(prefix = "redis.cache") @RefreshScope public class SampleRedisConfig { private int expireSeconds; private int commandTimeout; public void setExpireSeconds(int expireSeconds) { this.expireSeconds = expireSeconds; } public void setCommandTimeout(int commandTimeout) { this.commandTimeout = commandTimeout; } public int getExpireSeconds() { return expireSeconds; } public int getCommandTimeout() { return commandTimeout; } } |
ApolloConfigApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.example.sc.apollo; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import com.example.sc.apollo.config.JavaConfigBean; import com.example.sc.apollo.config.SampleRedisConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Set; @RestController @SpringBootApplication @EnableApolloConfig public class ApolloConfigApplication { @Autowired private RefreshScope refreshScope; //1.Java Config方式 @Autowired JavaConfigBean javaConfigBean; //2. ConfigurationProperties使用方式 @Autowired SampleRedisConfig sampleRedisConfig; // 3. @ApolloConfig使用 @ApolloConfig private Config config; public static void main(String[] args) { SpringApplication.run(ApolloConfigApplication.class, args); } @RequestMapping("/index") public String hello(){ return javaConfigBean.getTimeout()+""; } @RequestMapping("/index2") public String hello2(){ return sampleRedisConfig.getCommandTimeout()+"--"+sampleRedisConfig.getExpireSeconds(); } @RequestMapping("/index3") public String hello3(){ Set<String> propertyNames = config.getPropertyNames(); propertyNames.forEach(key -> { System.err.println(key+"="+config.getIntProperty(key,0)); }); return propertyNames.toString(); } @ApolloConfigChangeListener private void someOnChange(ConfigChangeEvent changeEvent) { //update injected value of batch if it is changed in Apollo System.out.println("======================>ApolloConfigChangeListener"); if (changeEvent.isChanged("timeout")) { System.out.println(config.getIntProperty("timeout", 0)); } refreshScope.refresh("sampleRedisConfig"); } } |
http://localhost:8088/index http://localhost:8088/index2 http://localhost:8088/index3