安装教程:https://blog.csdn.net/fy_java1995/article/details/106438325
1.打开apollo :新建项目
访问管理页面 http://localhost:8070
apollo/admin
2.新建配置项
3.应用读取配置
打开idea,新建apollo-quickstart项目
添加依赖
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.28</version>
</dependency>
编写测试类
public static void main(String[] args) {
Config config = ConfigService.getAppConfig();
String someKey = "sms.enable";
String value = config.getProperty(someKey, null);
System.out.println("sms.enable: " + value);
}
设置启动参数
-Dapp.id=apollo-quickstart -Denv=DEV -Ddev_meta=http://localhost:8080
观察控制台
4.修改配置
修改sms.enable的值为false
再次运行GetConfifigTest,可以看到输出结果已为false
5.热发布
修改代码为每3秒获取一次
public static void main(String[] args) {
Config config = ConfigService.getAppConfig();
String someKey = "sms.enable";
while (true) {
String value = config.getProperty(someKey, null);
System.out.printf("now: %s, sms.enable: %s%n", LocalDateTime.now().toString(),
value);
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行GetConfigTest观察输出结果。在apollo修改配置,然后发布,可以看到控制台的sms.enable会更改。