java客户端线上Apollo服务端的实现

时间:2021-05-19

1.指定环境

1.1 在C:\opt\settings\下有server.properties


env=DEV是对应服务器上的如下图

apollo.meta=http://192.168.1.143:8070是你服务端的端口号

1.2 在C:\opt\data\babel023\config-cache有这四个文件

2.在META-INF下创建app.properties


app.properties下的内容app.id对应Apollo服务端的id

3.创建个SimpleApolloConfig

package com.fqgj.rest;import com.google.common.base.Charsets;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 org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * @author Jason Song(song_s@ctrip.com) */public class SimpleApolloConfigDemo { private static final Logger logger = LoggerFactory.getLogger(SimpleApolloConfigDemo.class); private String DEFAULT_VALUE = "undefined"; private Config config; public SimpleApolloConfigDemo() { ConfigChangeListener changeListener = new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { logger.info("Changes for namespace {}", changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()); } } }; config = ConfigService.getAppConfig(); config.addChangeListener(changeListener); } private String getConfig(String key) { String result = config.getProperty(key, DEFAULT_VALUE); logger.info(String.format("Loading key : %s with value: %s", key, result)); return result; } public static void main(String[] args) throws IOException { SimpleApolloConfigDemo apolloConfigDemo = new SimpleApolloConfigDemo(); System.out.println( "Apollo Config Demo. Please input key to get the value. Input quit to exit."); while (true) { System.out.print("> "); String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine(); if (input == null || input.length() == 0) { continue; } input = input.trim(); if (input.equalsIgnoreCase("quit")) { System.exit(0); } String ccc= apolloConfigDemo.getConfig(input); System.out.println(ccc); } }}

运行结果如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章