ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Apollo quick start SampleApp demo Java

2022-07-06 14:33:52  阅读:170  来源: 互联网

标签:Java start demo ctrip framework config import apollo com


<!--配置中心-->
<dependency>
 <groupId>com.ctrip.framework.apollo</groupId>
 <artifactId>apollo-client</artifactId>
 <version>1.7.0</version>
</dependency

pom文件引入jar包

 

src/main/resources/META-INF/app.properties

app.id=SampleApp
# value=800 默认值

apollo.meta=http://localhost:8080
# 启动日志
# 09:01:06.752 [Apollo-RemoteConfigLongPollService-1] WARN com.ctrip.framework.apollo.internals.RemoteConfigLongPollService - Long polling failed, will retry in 16 seconds. appId: SampleApp, cluster: default, namespaces: application, long polling url: null, reason: Get config services failed from http://localhost:80801/services/config?appId=SampleApp&ip=192.168.137.1 [Cause: Could not complete get operation [Cause: port out of range:80801]]
package com.redis.demo.apollo;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.context.annotation.Configuration;

//@Configuration
//@EnableApolloConfig
public class AplloTest {
//    @ApolloConfig
//    private Config config; //inject config for namespace application


    public static void main(String[] args) {
        Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
        String someKey = "timeout";
        String someDefaultValue = "800";
        String value = config.getProperty(someKey, someDefaultValue);
        System.out.println("value=" + value);


            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()));
                    }
                }
            });
        /**
         * Changes for namespace application
         * Found change - key: timeout, oldValue: 300, newValue: 3001, changeType: MODIFIED
         */
        try {
            //阻塞
            Thread.sleep(600000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



}

 

标签:Java,start,demo,ctrip,framework,config,import,apollo,com
来源: https://www.cnblogs.com/oktokeep/p/16450696.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有