0%

Apollo 集成步骤

引入jar

1
2
3
4
5
6
7
8
9
10
11
12
13
 <!-- 阿波罗 -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${apollo.version}</version>
</dependency>

<!-- 阿波罗依赖guava包,注意guava包的版本 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>

启动类添加Apollo注解

1
2
3
4
5
6
7
@EnableApolloConfig
@SpringBootApplication
public class App{
public static void main( String[] args ){
new SpringApplicationBuilder(App.class).web(true).run(args);
}
}

新增一个类监听Apollo参数变更

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Component
@Slf4j
public class ApolloConfigRefresher implements ApplicationContextAware {

@Autowired
private ApplicationContext applicationContext;

@ApolloConfigChangeListener
public void onChange(ConfigChangeEvent changeEvent) {
// TODO 在此处监听发生改变的参数,若为连接池的参数需要重新加载链接池

this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

}

Apollo Demo提供的参考方法

  1. https://github.com/ctripcorp/apollo/blob/master/apollo-demo/src/main/java/com/ctrip/framework/apollo/demo/spring/springBootDemo/SpringBootSampleApplication.java

  2. apollo-use-cases

应用配置信息

1
2
3
4
5
6
7
app:
id: vota_business_mgmt_test

apollo:
autoUpdateInjectedSpringProperties: true
bootstrap:
enabled: true

Apollo 环境变量

文件目录

  • Window C:\opt\settings\server.properties
  • Liunx /opt/settings/server.properties

server.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#env=LOCAL

#dev
#env=DEV
#apollo.meta=http://apollo-dev.qa.xxxx.com

# 测试环境
#env=FAT / UAT
#apollo.meta=http://apollo.qa.xxxx.com/
#apollo.meta=http://apollo-pre.qa.xxxx.com/

# 生产
env=PRO
apollo.meta=http://apollo.ngi.xxxx.com/

env参数说明:

1
env=LOCAL  不会从服务器拉去最新的配置文件,只会使用当前的配置文件