apollo客户端的使用
apollo服务端安装请移至 记一次apollo服务端的安装过程
准备工作
-
创建springboot项目
-
pom.xml 添加apollo依赖
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.1.0</version> </dependency>
-
application.properties文件添加apollo配置
app.id=SampleApp apollo.meta=http://localhost:8080
-
启动类添加 @EnableApolloConfig 注解
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableApolloConfig public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
使用案例
-
直接使用@Value注解获取配置信息
-
使用bean方式获取配置信息
bean
import lombok.Getter; import lombok.Setter; import org.springframework.beans.factory.annotation.Value; @Getter @Setter public class TestData { @Value("${timeout:60}") private Integer timeout; }
config
import com.apollo.client.demo.config.data.TestData; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class BeansConfig { @Bean public TestData testData(){ return new TestData(); } }
controller
import com.apollo.client.demo.config.data.TestData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ApolloTestController { @Autowired private TestData testData; @RequestMapping("/getTimeoutTest") public Integer getTimeoutTest(){ return testData.getTimeout(); } }
-
使用apollo提供的ConfigService类获取配置信息
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigService; import lombok.Getter; import lombok.Setter; @Getter @Setter public class TestData { private Integer timeout; public void init(){ // config instance is singleton for each namespace and is never null Config config = ConfigService.getAppConfig(); String someKey = "timeout"; Integer someDefaultValue = 60; Integer value = config.getIntProperty(someKey, someDefaultValue); timeout = value; } }
-
监听配置变动
Config config = ConfigService.getAppConfig(); config.addChangeListener(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())); } });
-
可以使用注解方式添加监听
@Getter @Setter public class TestData { @ApolloConfig private Config config; @ApolloConfigChangeListener private void anotherOnChange(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())); } } }