Apollo quick start SampleApp demo Java

2024-10-09 09:51:03 浏览数 (2)

代码语言:javascript复制
<!--配置中心-->
<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

代码语言:javascript复制
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]]
代码语言:javascript复制
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();
        }
    }



}

0 人点赞