分布式配置中心Apollo

Apollo服务端搭建

推荐
使用dockerfile构建

version: "3"
services:
  apollo-configservice: #Config Service提供配置的读取、推送等功能,服务对象是Apollo客户端
    image: apolloconfig/apollo-configservice:1.8.1
    restart: always
    #container_name: apollo-configservice
    volumes:
          - ./logs/apollo-configservice:/opt/logs
    ports:
      - "8080:8080"
    environment:
      - TZ='Asia/Shanghai'   
      - SERVER_PORT=8080
      - EUREKA_INSTANCE_IP_ADDRESS=xxx.xxx.xxx.xxx
      - EUREKA_INSTANCE_HOME_PAGE_URL=http://xxx.xxx.xxx.xxx:8080
      - SPRING_DATASOURCE_URL=jdbc:mysql://xxx.xxx.xxx.xxx:3306/ApolloConfigDB?characterEncoding=utf8&serverTimezone=Asia/Shanghai
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=MysqkPassWord!
       
       
  apollo-adminservice: #Admin Service提供配置的修改、发布等功能,服务对象是Apollo Portal(管理界面)
    image: apolloconfig/apollo-adminservice:1.8.1
    restart: always
    #container_name: apollo-adminservice
    volumes:
      - ./logs/apollo-adminservice:/opt/logs
    ports:
      - "8090:8090"
    depends_on:
      - apollo-configservice
    environment:
      - TZ='Asia/Shanghai'   
      - SERVER_PORT=8090
      - EUREKA_INSTANCE_IP_ADDRESS=xxx.xxx.xxx.xxx
      - SPRING_DATASOURCE_URL=jdbc:mysql://xxx.xxx.xxx.xxx:3306/ApolloConfigDB?characterEncoding=utf8&serverTimezone=Asia/Shanghai
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=MysqkPassWord!
       
       
  apollo-portal: #管理界面
    image: apolloconfig/apollo-portal:1.8.1
    restart: always
    container_name: apollo-portal
    volumes:
      - ./logs/apollo-portal:/opt/logs
    ports:
      - "8070:8070"
    depends_on:
      - apollo-adminservice
    environment:
      - TZ='Asia/Shanghai'   
      - SERVER_PORT=8070
      - EUREKA_INSTANCE_IP_ADDRESS=xxx.xxx.xxx.xxx
      - APOLLO_PORTAL_ENVS=dev
      - DEV_META=http://xxx.xxx.xxx.xxx:8080
      - SPRING_DATASOURCE_URL=jdbc:mysql://xxx.xxx.xxx.xxx:3306/ApolloPortalDB?characterEncoding=utf8&serverTimezone=Asia/Shanghai
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_PASSWORD=MysqkPassWord!

与 Spring Boot 整合使用

创建一个springboot项目,
pom.xml
添加 Apollo 客户端的依赖,为了编码方便引入commons-lang3。

<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.3.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>

application.yml

server:
  port: 8761
app:
  id: springboot-apollo
apollo:
  meta: http://127.0.0.1:8080
  bootstrap:
    enabled: true
    eagerLoad:
      enabled: true
logging:
  level:
    com:
      gf:
        controller: debug

实现动态刷新配置
apollo与spring实现动态刷新配置,主要有2种刷新
基于普通字段刷新
基于bean上使用了@ConfigurationProperties刷新
一、普通字段刷新
在需刷新的字段上配置@Value注解,如:

@Value("${ve.auth-url}")
private String authUrl;

二、bean使用@ConfigurationProperties动态刷新
bean使用@ConfigurationProperties注解目前还不支持自动刷新,需要编写额外的代码。目前官方提供2种刷新方案

基于RefreshScope实现刷新
基于EnvironmentChangeEvent实现刷新

项目选用基于RefreshScope实现刷新。

1.bean上使用@RefreshScope注解

@ConfigurationProperties(prefix = "partners")
@Component("partnersConfig")
@RefreshScope
@Slf4j
@Data
public class PartnersConfig {
    private List<FundProvider> fundProviders = Lists.newArrayList();
    private List<LogisticsCompany> logisticsCompanies = Lists.newArrayList();
    private List<InsuranceCompany> insuranceCompanies = Lists.newArrayList();
}

2.利用RefreshScope搭配@ApolloConfigChangeListener监听实现bean的动态刷新

其代码实现如下:

@Configuration
public class ApolloLoggerConfig {

    private static final Logger logger = LoggerFactory.getLogger(LoggerConfig.class);
    private static final String LOGGER_TAG = "logging.level.";

    @Autowired
    private LoggingSystem loggingSystem;

    @ApolloConfig
    private Config config;

    @ApolloConfigChangeListener
    private void configChangeListter(ConfigChangeEvent changeEvent) {
        refreshLoggingLevels();
    }

    @PostConstruct
    private void refreshLoggingLevels() {
        Set<String> keyNames = config.getPropertyNames();
        for (String key : keyNames) {
            if (StringUtils.containsIgnoreCase(key, LOGGER_TAG)) {
                String strLevel = config.getProperty(key, "info");
                LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
                loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level);
                logger.info("{}:{}", key, strLevel);
            }
        }
    }


}
上一篇:冬季实战营第二期:Linux操作系统实战入门学习报告


下一篇:工具分享--可视化管理与监控(三)