apollo客户端的使用

apollo客户端的使用

apollo服务端安装请移至 记一次apollo服务端的安装过程

准备工作

  1. 创建springboot项目
    在这里插入图片描述

  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注解获取配置信息
    在这里插入图片描述

  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()));
            }
        }
    }
    
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值