apollo 配置说明

  1. # apollo配置,需要先创建一个应用,然后再创建这个应用相关的配置信息
  2. # 指定apollo上面对应的应用id,程序会根据这个id去找到apollo上面对应的应用的配置
  3. app.id: middleend-product
  4. apollo:
  5. #指定对应的集群
  6. cluster: local
  7. #apollo的配置中心地址
  8. meta: http://dev-config.aeonbuy.com
  9. bootstrap:
  10. #在应用启动阶段是否向Spring容器注入被托管的properties文件配置信息
  11. enabled: true
  12. #指定要加载的命名空间
  13. namespaces: application,common-ms,common-excel,common-exception,auth
  14. eagerLoad:
  15. #将Apollo配置加载提到初始化日志系统之前
  16. enabled: true

其中 env 环境参数是在 命令行参数 来指定 -Denv=dev
apollo.meta参数,若是命令行指定,是 -Ddev_meta=http://config-service-url ,需要注意 dev 与 meta 间是下划线不是点 . !!!

  1. /**
  2. * Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
  3. *
  4. * <p>Configuration example:</p>
  5. * <pre class="code">
  6. * # set app.id
  7. * app.id = 100004458
  8. * # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
  9. * apollo.bootstrap.enabled = true
  10. * </pre>
  11. *
  12. * or
  13. *
  14. * <pre class="code">
  15. * # set app.id
  16. * app.id = 100004458
  17. * # enable apollo bootstrap config
  18. * apollo.bootstrap.enabled = true
  19. * # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
  20. * apollo.bootstrap.namespaces = application,FX.apollo
  21. * </pre>
  22. *
  23. *
  24. * If you want to load Apollo configurations even before Logging System Initialization Phase,
  25. * add
  26. * <pre class="code">
  27. * # set apollo.bootstrap.eagerLoad.enabled
  28. * apollo.bootstrap.eagerLoad.enabled = true
  29. * </pre>
  30. *
  31. * This would be very helpful when your logging configurations is set by Apollo.
  32. *
  33. * for example, you have defined logback-spring.xml in your project, and you want to inject some attributes into logback-spring.xml.
  34. *
  35. */

还有别的参数:
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.

配置使用举例

  1. -Denv=dev -Ddev_meta="http://localhost:8080" -Dapp.id=test-demo -Dapollo.property.order.enable=false
  2. -Denv=dev -Ddev_meta="http://localhost:8080" -Dapp.id=test-demo -Dapollo.property.order.enable=false -Dapollo.cluster=pandora

配置查询顺序注意

配置寻找顺序:先看当前 集群 cluster 下 namespace 是否存在,存在(已发布)则拿取;不存在,找 default 下同名的 namespace;

添加多环境

  1. 生成一个新的 configDB 数据库(注意修改表 serverconfig 的 eureka.service.url 值); ``sql UPDATEapolloconfigdb8081.serverconfigSETId='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’);
  1. 主要是修改 key eureka.service.url 的值 value 为新的 configservice 地址(比如:[http://localhost:8081/eureka/](http://localhost:8081/eureka/))
  2. 2. 启动新的 configadmin

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

  1. 启动中,不能修改参数 spring.application.name=apollo-adminservicespring.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/"}]

  1. 3. 添加新的环境参数,重启 portal
  2. 参数:

-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 ```

  1. 进入 portal 管理界面,修改系统参数 apollo.portal.envs,添加上自己的环境(此次为 prod)

小结

关于 APP 项目、env 环境、cluster 集群、namespace 命名空间,可以按照自己的服务实际特点来整,即可以将所有的环境配置放到同一个 APP 的 同一 env 中管理。