java练习生 - 使用apollo

一、添加依赖

<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.4.0</version>
</dependency>

二、添加配置(bootstrap.yml)

 bootstrap.yml 先于 application.yml 加载

bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,可以使用来配置application.yml中使用到参数等
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。

app:
  id: ttt # apollo配置的appid
 
apollo:
  meta: http://dev.config.apollo.xxx.com.local
  bootstrap:
    enabled: true
    namespaces: application,test
    eagerLoad:
      enabled: true
 
---
spring:
  profiles: sit
 
apollo:
  meta: http://sit.config.apollo.xxx.com.local
  bootstrap:
    enabled: true
    namespaces: application,test
    eagerLoad:
      enabled: true
---
spring:
  profiles: uat
 
apollo:
  meta: http://uat.config.apollo.xxx.com.local
  bootstrap:
    enabled: true
    namespaces: application,test
    eagerLoad:
      enabled: true
---
spring:
  profiles: prod
 
apollo:
  meta: http://pro.config.apollo.xxx.com.local
  bootstrap:
    enabled: true
    namespaces: application,test
    eagerLoad:
      enabled: true

文件中加---分隔符,相当于后面的内容独立出一个配置文件,用于区分运行环境。

DEV环境:DEV顾名思义就是develop,即代码开发的环境。

SIT环境:System Integration Test系统集成测试,开发人员自己测试流程是否走通。

UAT环境:User Acceptance Test用户验收测试,由专门的测试人员验证,验收完成才能上生产环境。

PRO环境:production 生产环境。

三、使用

3.1 在其他配置文件中读取apollo配置

cur-application-port: ${server.port}

3.2 在代码中读取apollo配置

@Value("${server.port:8888}")
public String curAppPort;

可以通过冒号给配置添加默认值,这样当配置不存在时会直接使用默认值,而避免抛异常。

 

posted @ 2020-12-31 13:37  Ariter  阅读(292)  评论(0编辑  收藏  举报