apollo客户端的使用 冷不防 2024-04-18 13:44 14阅读 0赞 # apollo客户端的使用 # > apollo服务端安装请移至 [记一次apollo服务端的安装过程][apollo] ### 准备工作 ### 1. **创建springboot项目** ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI1MjE1ODIx_size_16_color_FFFFFF_t_70] 2. pom.xml 添加apollo依赖 <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.1.0</version> </dependency> 3. application.properties文件添加apollo配置 app.id=SampleApp apollo.meta=http://localhost:8080 4. 启动类添加 @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); } } ### 使用案例 ### 1. 直接使用@Value注解获取配置信息 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI1MjE1ODIx_size_16_color_FFFFFF_t_70 1] 2. 使用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(); } } 3. 使用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; } } 4. 监听配置变动 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())); } }); 5. 可以使用注解方式添加监听 @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())); } } } [apollo]: https://blog.csdn.net/qq_25215821/article/details/100597557 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI1MjE1ODIx_size_16_color_FFFFFF_t_70]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/18/29cf228134124b27b7d07646dc9c6ba7.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI1MjE1ODIx_size_16_color_FFFFFF_t_70 1]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/18/5e9d9db8ac0e4e7e93cda3b4392c6020.png
还没有评论,来说两句吧...