SpringBoot集成Apollo笔记

1、Maven的pom.xml里面增加apollo依赖

1.1.1

    com.ctrip.framework.apollo
    apollo-client
    ${apollo.version}

2、项目增加配置文件:

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

内容添加:app.id=Test00001(AppId)
在这里插入图片描述
3、增加配置监控

添加代码监听(核心代码)

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
public class ConfigBean {
	
	@ApolloConfigChangeListener
	private void someOnChange(ConfigChangeEvent changeEvent) {
		log.info("Some configures changed!");
	    for(String key : changeEvent.changedKeys()) {
	    	ConfigChange cc = changeEvent.getChange(key);
	    	log.info("-----{} is changed from {} to {}", key, cc.getOldValue(), cc.getNewValue());
	    	// TODO: Do something for the changed value
	    	
	    }
	}
}

4、增加/opt/settings/server.properties

并添加内容:env=DEV

apollo.meta=http://localhost:8080(config-service-url )
在这里插入图片描述
5、spring boot配置

在application.properties中按照如下样例配置即可

在bootstrap阶段注入非默认application namespace或多个namespace的配置示例

apollo.bootstrap.enabled = true

apollo.bootstrap.namespaces = application,datasource,activemq,redis

你可能感兴趣的:(springboot,spring,boot,app)