官方:GitHub - apolloconfig/apollo: Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.
1.1 直接下载安装包
- 从GitHub Release下载最新版本的
apollo-configservice-x.x.x-github.zip
、apollo-adminservice-x.x.x-github.zip
和apollo-portal-x.x.x-github.zip
1.2源码构建--定制开发
- 对Apollo的做定制开发,从GitHub Release页面下载Source code包或直接clone源码后在本地构建
1.2.1配置数据库信息
- 数据库中独立建表导数据:ApolloPortalDB和ApolloConfigDB
- 数据库连接参数
-
#apollo config db info apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8 apollo_config_db_username=用户名 apollo_config_db_password=密码 # apollo portal db info apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8 apollo_portal_db_username=用户名 apollo_portal_db_password=密码
- 配置环境meta service
dev_meta=http://1.1.1.1:8080
fat_meta=http://apollo.fat.xxx.com
uat_meta=http://apollo.uat.xxx.com
pro_meta=http://apollo.xxx.com
META_SERVERS_OPTS="-Ddev_meta=$dev_meta -Dfat_meta=$fat_meta -Duat_meta=$uat_meta -Dpro_meta=$pro_meta"
1.2.2 编译打包
./build.sh
- 依次打包apollo-configservice, apollo-adminservice, apollo-portal
- 获取apollo-configservice安装包:
apollo-configservice/target/
目录下的apollo-configservice-x.x.x-github.zip
- 获取apollo-adminservice安装包:
apollo-adminservice/target/
目录下的apollo-adminservice-x.x.x-github.zip
- 获取apollo-potalservice安装包.
apollo-portal/target/
目录下的apollo-portal-x.x.x-github.zip
2 部署服务
apollo-configservce,apollo-adminservice都需要向eureka 服务注册,需要配置Eureka 服务地址。apollo-configservc就是一个Eureka服务,只要写入apolo-configservice 地址。
mysql> update ApolloConfigDB.ServerConfig set Value='http://localhost:9100/eureka/,http://localhost:8080/eureka/' where `key`='eureka.service.url';
注意:apollo-adminservice端口与数据库设置保持一致 .
配置端口(start.sh):
configservice端口: 9100, adminservice端口:9101, portalservice端口 9100.
最后启动:apollo-portalservice
- DEV环境 (192.168.31.252)
- 部署apollo-configservce :
apollo-configservice-x.x.x-github.zip 上传,解压执行./script/start.sh,停止服务。
- 部署apollo-adminservice
apollo-adminservice-x.x.x-github.zip 上传,解压执行./script/start.sh,停止服务。
- Pro环境 (192.168.31.251)
- 部署apollo-configservce :
apollo-configservice-x.x.x-github.zip 上传,解压执行./script/start.sh,停止服务。
- 部署apollo-adminservice
apollo-adminservice-x.x.x-github.zip 上传,解压执行./script/start.sh,停止服务。
- 本地环境 (192.168.31.250)
- 部署apollo-portalservice
apollo-portal-x.x.x-github.zip 解压执行./script/start.sh,停止服务。
补充说明:直接下载 apollo-configservice-x.x.x-github.zip
、apollo-adminservice-x.x.x-github.zip
和apollo-portal-x.x.x-github.zip
apollo-configservice-x.x.x
、apollo-adminservice-x.x.x. 配置数据库(
config/application-github.properties):
ApolloConfigDB
# DataSource
spring.datasource.url = jdbc:mysql://192.0.0.187:3306/ApolloConfigDB?characterEncoding=utf8&serverTimezone=Asia
spring.datasource.username = someuser
spring.datasource.password = somepwd
apollo-portal-x.x.x 数据库(config/
application-github.properties)
:
ApolloPortalDB、meta service信息(config/apollo-env.properties)
# DataSource
spring.datasource.url = jdbc:mysql://192.0.0.187:3306/ApolloPortalDB?characterEncoding=utf8&serverTimezone=Asia
spring.datasource.username = someuser
spring.datasource.password = somepwd
- 默认情况下,meta service和config service是部署在同一个JVM进程,所以meta service的地址就是config service的地址。
- Apollo Portal需要在不同的环境访问不同的meta service(apollo-configservice)地址,所以我们需要在配置中提供这些信息
- apollo也支持在运行时指定meta service 优先级高于
apollo-env.properties
方式配置。
dev.meta=http://1.1.1.1:9100
fat.meta=http://apollo.fat.xxx.com
uat.meta=http://apollo.uat.xxx.com
pro.meta=http://apollo.xxx.com
4 访问测试
- 访问不同环境euraka服务.查看服务情况
- DEV环境 (http://192.168.31.252:9100)
- Pro环境 (http://192.168.31.251:9100)
3 本地环境 (http://192.168.31.250:9100) apollo/admin 登录
5 添加环境变量 即 调整protal配置
- mysql> update ApolloPortalDB.ServerConfig set Value="dev,pro" where `key`='apollo.portal.envs';
- 页面修改应用配置 apollo.portal.envs值为 dev,pro, 添加缺失变量。
spring boot 集成方式
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.7.0</version>
</dependency>
- application.properties/bootstrap.properties来配置
#apollo 应用唯一标识
app.id=SampleApp
#配置 注册中心
apollo.meta = http://localhost:8080
# 设置多个namespace 用逗号隔开
apollo.bootstrap.namespaces=application
#环境变量
apollo.portal.envs=DEV
#注入默认application
apollo.bootstrap.enabled = true
#Apollo的加载顺序放到日志系统加载之前,导致Apollo的启动过程无法通过日志的方式输出
apollo.bootstrap.eagerLoad.enabled=true
#关闭placeholder在运行时自动更新功能
#apollo.autoUpdateInjectedSpringProperties=false
#配置集群
#apollo.cluster=cluster1
#拉取配置时间间隔 单位分
#apollo.refreshInterval: 1
1、XML使用方式
- TestXmlBean,它有两个配置项需要注入:
public class TestXmlBean {
private int timeout;
private int batch;
//get ,set 方法 }
- 默认的application namespace中有timeout和batch的配置项
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:apollo="http://www.ctrip.com/schema/apollo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
<apollo:config/>
<bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
<property name="timeout" value="${timeout:100}"/>
<property name="batch" value="${batch:200}"/>
</bean>
</beans>
2、Java
Config 使用方式
- Java Config的方式还可以使用@Value的方式注入:Placeholder方式
- 代码中直接使用,如:
@Value("${someKeyFromApollo:someDefaultValue}")
- 配置文件中使用替换placeholder,如:
spring.datasource.url: ${someKeyFromApollo:someDefaultValue}
- 直接托管spring的配置,如在apollo中直接配置
spring.datasource.url=jdbc:mysql://localhost:3306/somedb?characterEncoding=utf8
- laceholder来注入配置,使用的格式形如${someKey:someDefaultValue},如${timeout:100}。冒号前面的是key,冒号后面的是默认值。
public class TestJavaConfigBean {
@Value("${timeout:100}")
private int timeout;
private int batch;
@Value("${batch:200}")
public void setBatch(int batch) {
this.batch = batch;
}
public int getTimeout() {
return timeout;
}
public int getBatch() {
return batch;
}
}
- 在Configuration类中按照下面的方式使用:默认的application namespace中有
timeout
和batch
的配置项
@Configuration
@EnableApolloConfig
public class AppConfig {
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
}
API 使用方式
获取默认namespace 配置方式
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
String someKey = "someKeyFromDefaultNamespace"; // apollo namespacee 中key
String someDefaultValue = "someDefaultValueForTheKey";
String value = config.getProperty(someKey, someDefaultValue);
3、ConfigurationProperties 使用方式
- Spring Boot提供了@ConfigurationProperties把配置注入到bean对象中,
redis.cache.expireSeconds
和redis.cache.commandTimeout
分别注入到SampleRedisConfig的expireSeconds
和commandTimeout
字段中。
@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;
}
}
- 默认的application namespace中有
redis.cache.expireSeconds
和redis.cache.commandTimeout
的配置项)
@Configuration
@EnableApolloConfig
public class AppConfig {
@Bean
public SampleRedisConfig sampleRedisConfig() {
return new SampleRedisConfig();
}
}
4、Spring Annotation方式(推荐)
- @ApolloConfig 用来自动注入Config对象
- @ApolloConfigChangeListener 用来自动注册ConfigChangeListener
- @ApolloJsonValue 用来把配置的json字符串自动注入为对象
public class TestApolloAnnotationBean {
@ApolloConfig
private Config config;
@ApolloConfig("application")
private Config anotherConfig;
@ApolloConfig("FX.apollo")
private Config yetAnotherConfig;
@ApolloConfig("application.yml")
private Config ymlConfig;
@ApolloJsonValue("${jsonBeanProperty:[]}")
private List<JsonBean> anotherJsonBeans;
@Value("${batch:100}")
private int batch;
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
if (changeEvent.isChanged("batch")) {
batch = config.getIntProperty("batch", 100);
}
}
//config change listener for namespace application
@ApolloConfigChangeListener("application")
private void anotherOnChange(ConfigChangeEvent changeEvent) {
//do something
}
@ApolloConfigChangeListener({"application", "FX.apollo", "application.yml"})
private void yetAnotherOnChange(ConfigChangeEvent changeEvent) {
//do something
}
public int getTimeout() {
return config.getIntProperty("timeout", 200);
}
@ApolloConfigChangeListener
public int getBatch() {
return this.batch;
}
private static class JsonBean{
private String someString;
private int someInt;
}
}
- Configuration类中按照下面的方式使用
@Configuration
@EnableApolloConfig
public class AppConfig {
@Bean
public TestApolloAnnotationBean testApolloAnnotationBean() {
return new TestApolloAnnotationBean();
}
}