编程语言
首页 > 编程语言> > Apollo quick start SampleApp demo Java

Apollo quick start SampleApp demo Java

作者:互联网

<!--配置中心-->
<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