整合apollo

  Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

Apollo和Spring Cloud Config对比

  

 

 

 搭建教程

  https://github.com/ctripcorp/apollo/wiki/Quick-Start

pom依赖

    <dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- 为了编码方便,并非apollo 必须的依赖 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>

application.yml

server:
   port: 6001

app:
  id: boot-apollo
apollo:
  meta: http://localhost:8080/   //server 地址
  bootstrap:
    enabled: true
    eagerLoad:
       enabled: true
  • app.id:AppId是应用的身份信息,是配置中心获取配置的一个重要信息
  • apollo.bootstrap.enabled:在应用启动阶段,向Spring容器注入被托管的application.properties文件的配置信息。
  • apollo.bootstrap.eagerLoad.enabled:将Apollo配置加载提到初始化日志系统之前。

HelloController

package com.smart.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/hello")
public class HelloController {

    @Value("${server.port}")
    private int serverPort;

    @GetMapping("apollo")
    public String helloApollo(){
        log.debug("-----------this is just a test-----------------");
        return "hi Apollo "+serverPort;
    }
}

启动类

@SpringBootApplication
@EnableApolloConfig
public class BootApolloApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootApolloApplication.class, args);
    }

}

配置中心配置

创建项目

 

 填写配置信息

  • 部门:选择应用所在的部门。
  • 应用AppId:用来标识应用身份的唯一id,格式为string,需要和客户端。application.properties中配置的app.id对应。
  • 应用名称:应用名,仅用于界面展示。
  • 应用负责人:选择的人默认会成为该项目的管理员,具备项目权限管理、集群创建、Namespace创建等权限。
点击 “新增配置”,配置需要管理的 application.properties 中的属性

发布配置

配置只有在发布后才会真的被应用使用到,所以在编辑完配置后,需要发布配置。点击“发布按钮”。

注意:

  • 服务的端口依然还是 6001,这是因为 apollo 修改配置,不会像Spring Cloud Config 那样去重启应用。
  • 重启应用后,客户端会加载使用 配置中心中配置的属性的值,例如我们重启我们的应用,服务端口会变成6666。
需求:
  一般在开发环境下使用DEBUG级别的日志输出,为了方便查看问题,而在线上一般都使用INFO或者ERROR级别的日志,主要记录业务操作或者错误的日志。把日志的配置部署到Apollo配置中心,但在配置中心修改日志等级,依然需要重启应用才生效,下面我们就通过监听配置的变化,来达到热更新的效果。
package com.smart.config;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Set;

@Slf4j
@Configuration
public class LoggerConfig {
    private static final String LOGGER_TAG = "logging.level.";

    @Autowired
    private LoggingSystem loggingSystem;

    @ApolloConfig
    private Config config;

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

    @PostConstruct
    private void refreshLoggingLevels() {
        Set<String> propertyNames = config.getPropertyNames();
        for (String key : propertyNames) {
            if (StringUtils.contains(key, LOGGER_TAG)) {
                String strLevel = config.getProperty(key, "info");
                LogLevel logLevel = LogLevel.valueOf(strLevel.toUpperCase());
                loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), logLevel);
                log.info("{}:{}",key,logLevel);
            }
        }
    }
}
  • @ApolloConfig注解:    将Apollo服务端的中的配置注入这个类中。
  • @ApolloConfigChangeListener注解:监听配置中心配置的更新事件,若该事件发生,则调用refreshLoggingLevels方法,处理该事件。
  • ConfigChangeEvent参数:可以获取被修改配置项的key集合,以及被修改配置项的新值、旧值和修改类型等信息。

   


  

 


 

 


 

 

 

 

posted on 2019-10-13 17:27  溪水静幽  阅读(195)  评论(0)    收藏  举报