apollo 配置说明
# apollo配置,需要先创建一个应用,然后再创建这个应用相关的配置信息
# 指定apollo上面对应的应用id,程序会根据这个id去找到apollo上面对应的应用的配置
app.id: middleend-product
apollo:
#指定对应的集群
cluster: local
#apollo的配置中心地址
meta: http://dev-config.aeonbuy.com
bootstrap:
#在应用启动阶段是否向Spring容器注入被托管的properties文件配置信息
enabled: true
#指定要加载的命名空间
namespaces: application,common-ms,common-excel,common-exception,auth
eagerLoad:
#将Apollo配置加载提到初始化日志系统之前
enabled: true
其中 env 环境参数是在 命令行参数 来指定 -Denv=dev
apollo.meta参数,若是命令行指定,是 -Ddev_meta=http://config-service-url ,需要注意 dev 与 meta 间是下划线不是点 . !!!
/**
* Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
*
* <p>Configuration example:</p>
* <pre class="code">
* # set app.id
* app.id = 100004458
* # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
* apollo.bootstrap.enabled = true
* </pre>
*
* or
*
* <pre class="code">
* # set app.id
* app.id = 100004458
* # enable apollo bootstrap config
* apollo.bootstrap.enabled = true
* # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
* apollo.bootstrap.namespaces = application,FX.apollo
* </pre>
*
*
* If you want to load Apollo configurations even before Logging System Initialization Phase,
* add
* <pre class="code">
* # set apollo.bootstrap.eagerLoad.enabled
* apollo.bootstrap.eagerLoad.enabled = true
* </pre>
*
* This would be very helpful when your logging configurations is set by Apollo.
*
* for example, you have defined logback-spring.xml in your project, and you want to inject some attributes into logback-spring.xml.
*
*/
还有别的参数:
apollo.cacheDir 配置缓存路径,举例 apollo.cacheDir=/opt/data/some-cache-dir
apollo.accesskey.secret 访问ak
apollo.property.order.enable 保留配置文件中配置顺序,Configuration to keep properties order as same as line order in .yml/.yaml/.properties file.
配置使用举例
-Denv=dev -Ddev_meta="http://localhost:8080" -Dapp.id=test-demo -Dapollo.property.order.enable=false
-Denv=dev -Ddev_meta="http://localhost:8080" -Dapp.id=test-demo -Dapollo.property.order.enable=false -Dapollo.cluster=pandora
配置查询顺序注意
配置寻找顺序:先看当前 集群 cluster 下 namespace 是否存在,存在(已发布)则拿取;不存在,找 default 下同名的 namespace;
添加多环境
- 生成一个新的 configDB 数据库(注意修改表 serverconfig 的 eureka.service.url 值);
``sql UPDATE
apolloconfigdb8081.
serverconfigSET
Id='1',
Key='eureka.service.url',
Cluster='default',
Value='http://localhost:8081/eureka/',
Comment='Eureka服务Url,多个service以英文逗号分隔',
IsDeleted=b'0',
DataChange_CreatedBy='default',
DataChange_CreatedTime='2021-05-27 13:49:19',
DataChange_LastModifiedBy='',
DataChange_LastTime='2021-05-27 13:50:50' WHERE (
Id`=’1’);
主要是修改 key 为 eureka.service.url 的值 value 为新的 configservice 地址(比如:[http://localhost:8081/eureka/](http://localhost:8081/eureka/))
2. 启动新的 config、admin
D:\devTools\java\jdk1.8.0_261\bin\java.exe -Xmx128m -Xms128m -Dspring.datasource.url=”jdbc:mysql://localhost:3306/ApolloConfigDB8081?sslMode=DISABLED&serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false” -Dspring.datasource.username=root -Dspring.datasource.password=123456 -Dspring.jpa.database-platform=org.hibernate.dialect.MySQLDialect -Dfile.encoding=UTF-8 -Dserver.port=8081 -Dspring.application.name=apollo-configservice -Dlogging.file.name=”/opt/logs/100003171/apollo-configservice8081.log” -Deureka.client.serviceUrl.defaultZone=”http://localhost:8081/eureka/“ -jar D:\OtherProjects\apollo\apollo-configservice\target\apollo-configservice-1.8.1.jar
D:\devTools\java\jdk1.8.0_261\bin\java.exe -Xmx128m -Xms128m -Dspring.datasource.url=”jdbc:mysql://localhost:3306/ApolloConfigDB8081?sslMode=DISABLED&serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false” -Dspring.datasource.username=root -Dspring.datasource.password=123456 -Dspring.jpa.database-platform=org.hibernate.dialect.MySQLDialect -Dfile.encoding=UTF-8 -Dserver.port=8091 -Dspring.application.name=apollo-adminservice -Dlogging.file.name=”/opt/logs/100003172/apollo-adminservice8091.log” -Deureka.client.serviceUrl.defaultZone=”http://localhost:8081/eureka/“ -jar D:\OtherProjects\apollo\apollo-adminservice\target\apollo-adminservice-1.8.1.jar
启动中,不能修改参数 spring.application.name=apollo-adminservice,spring.application.name=apollo-configservice 等关键参数。<br />是否能够成功,看 http://config_url/services/admin 接口是否返回正确值,例如:
访问接口 http://localhost:8081/services/admin 返回 [{“appName”:”APOLLO-ADMINSERVICE”,”instanceId”:”localhost:apollo-adminservice:8091”,”homepageUrl”:”http://192.168.65.1:8091/"}]
访问接口 http://localhost:8080/services/admin 返回 [{“appName”:”APOLLO-ADMINSERVICE”,”instanceId”:”localhost:apollo-adminservice:8090”,”homepageUrl”:”http://192.168.65.1:8090/"}]
3. 添加新的环境参数,重启 portal
参数:
-Xmx128m -Xms128m -Dspring.datasource.url=”jdbc:mysql://localhost:3306/ApolloPortalDB?sslMode=DISABLED&serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false” -Dspring.datasource.username=”root” -Dspring.datasource.password=”123456” -Dprod_meta=”http://localhost:8081“ -Ddev_meta=”http://localhost:8080“ -Dfile.encoding=UTF-8 ```
- 进入 portal 管理界面,修改系统参数 apollo.portal.envs,添加上自己的环境(此次为 prod)
小结
关于 APP 项目、env 环境、cluster 集群、namespace 命名空间,可以按照自己的服务实际特点来整,即可以将所有的环境配置放到同一个 APP 的 同一 env 中管理。