#apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=用户名
apollo_config_db_password=密码(如果没有密码,留空即可)
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=用户名
apollo_portal_db_password=密码(如果没有密码,留空即可)
==== starting service ====
Service logging file is ./service/apollo-service.log
Started [10768]
Waiting for config service startup.......
Config service started. You may visit http://localhost:8080 for service status now!
Waiting for admin service startup....
Admin service started
==== starting portal ====
Portal logging file is ./portal/apollo-portal.log
Started [10846]
Waiting for portal startup......
Portal started. You can visit http://localhost:8070 now!
/**
* Java Config方式
*
* @author simon
* @create 2018-11-02 15:00
**/
@Configuration
public class JavaConfigBean {
@Value("${timeout:20}")
private int timeout;
public int getTimeout() {
return timeout;
}
}
Spring Boot提供了@ConfigurationProperties把配置注入到bean对象中。Apollo也支持这种方式,下面的例子会把redis.cache.expireSeconds和redis.cache.commandTimeout分别注入到SampleRedisConfig的expireSeconds和commandTimeout字段中。
新增配置类SampleRedisConfig如下:
/**
* ConfigurationProperties使用方式
*
* @author simon
* @create 2018-11-02 9:30
**/
@Configuration
@ConfigurationProperties(prefix = "redis.cache")
public class SampleRedisConfig {
private int expireSeconds;
private int commandTimeout;
public void setExpireSeconds(int expireSeconds) {
this.expireSecOnds= expireSeconds;
}
public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
}
public int getExpireSeconds() {
return expireSeconds;
}
public int getCommandTimeout() {
return commandTimeout;
}
}
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
//update injected value of batch if it is changed in Apollo
if (changeEvent.isChanged("timeout")) {
System.out.println(config.getIntProperty("timeout", 0));
}
}
测试
在Apollo服务端修改timeout配置的值为300,发布后,控制台打印300
2.2.2.3 @ApolloJsonValue的使用
新增User如下:
/**
* 用户
*
* @author simon
* @create 2018-11-02 16:41
**/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}