最新Apollo 版本釋出2.1.0
https://www.apolloconfig.com/#/zh/design/apollo-design
環境說明
- ecs 主機一台
- 資料庫mysql 8.0
- docker 環境
apollo 是内網可信應用,最好是部署在内網裡面,外網不可使用,避免配置資訊洩漏,這裡為了友善示範,直接把端口暴露到了外網
導入sql
官方的sql位址:https://github.com/apolloconfig/apollo/tree/master/scripts/sql
注意對應分支與版本,部署的時候是最新版本是以我直接拿master的
三個元件:admi-service、config-service 、 portal 環境與db的關系,可以參考這張圖
- admin-service、config-service 連config (每個環境都需要獨立部署一個config 的db)這樣做到隔離
- portal 連portal
可以簡單了解為:
admin 負責的是管理端的接口
config 負責用戶端:java 連的就是config
portal 負責的是界面和權限,管理端的那邊邏輯
docker 部署
安裝docker 比較多教程這邊就不贅述了
腳本:https://raw.githubusercontent.com/freshgeek/linux_software/master/apollo-docker-start.sh
其中需要修改的是,對應的資料庫連接配接資訊和cs 對應的通路路徑(docker 部署最大的問題是:IP和端口在外網情況下不識别。是以這裡也直接吧端口映射出去了)
修改完成後,直接 sh apollo-docker-start.sh
注意:主機的防火牆問題,可能導緻内部連不上、外部通路不了等情況。務必檢查一下
登陸之後的配置
- 登陸apollo : portal 的位址和端口 ;預設是apollo/admin;注意修改
-
系統管理工具 -> 系統參數
将環境 修改為前面docker 腳本中對應的兩個環境和對應的位址
- 在config service 中的eureka 改為自己可以通路的config service 位址
準備springboot 項目連接配接apollo
- 我這邊是springboot :2.2.6.RELEASE
- apollo client : 2.1.0
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>2.1.0</version>
</dependency>
配置檔案
application.properties 中隻需要添加
app.id=應用名
apollo.bootstrap.enabled=true
apollo.bootstrap.namespaces=application
apollo.bootstrap.eagerLoad.enabled=true
# 測試環境 cs 位址 或者 線上環境cs位址
apollo.meta=http://x.x.x.x:8060
apollo.cluster=default
注解@EnableApolloConfig
添加到啟動類上 即可
其他的都可以放apollo動态配置了,注意配置需要釋出之後才能拿到
更多好玩的,官方還推薦的:https://github.com/apolloconfig/apollo-use-cases
官方的那個動态調整日志的寫得有點啰嗦,這裡貼下我的
import cn.hutool.core.util.StrUtil;
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.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* @author chenchao
*/
@Slf4j
@Configuration
public class DynamicLogLevelConfig {
private static final String LOGGER_TAG = "logging.level.";
@Resource
private LoggingSystem loggingSystem;
@ApolloConfig
private Config config;
@ApolloConfigChangeListener
private void onChange(ConfigChangeEvent changeEvent) {
changeEvent.changedKeys().forEach(this::changeLevel);
}
@PostConstruct
private void refreshLoggingLevels() {
config.getPropertyNames().forEach(this::changeLevel);
}
private void changeLevel(String key) {
if (!StrUtil.startWith(key, LOGGER_TAG)) {
return;
}
String strLevel = config.getProperty(key, "info");
LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
loggingSystem.setLogLevel(key.replaceFirst(LOGGER_TAG, StrUtil.EMPTY), level);
log.info("{}={}", key, strLevel);
}
}