分布式配置中心有很多,springcloud的config、百度的disconfig、携程的apollo、淘宝的diamond。由于apollo极少的侵入性,以及面对springcloud开发,适用于微服务配置管理场景,这里我们选择交付Apollo至Kubernetes集群。
Apollo简介
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
官方GitHub地址:https://github.com/ctripcorp/apollo
- apollo-configservice:提供配置获取接口,提供配置更新推送接口,接口服务对象为Apollo客户端
- apollo-adminservice:提供配置管理接口,提供配置修改、发布等接口,接口服务对象为Portal,以及Eureka
- apollo-portal:提供Web界面供用户管理配置
- apollo-client:Apollo提供的客户端程序,为应用提供配置获取、实时更新等功能
官方Apollo框架:
简化Apollo框架:
交付apollo-configservice
部署MariaDB
MySQL版本应为5.6或以上!
[root@node7-11 ~]# vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64/
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
[root@node7-11 ~]# rpm --import https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
[root@node7-11 ~]# yum makecache
[root@node7-11 ~]# yum list mariadb-server --show-duplicates
[root@node7-11 ~]# yum install mariadb-server -y
[root@node7-11 ~]# mysql -V
mysql Ver 15.1 Distrib 10.2.40-MariaDB, for Linux (x86_64) using readline 5.1
[root@node7-11 ~]# vi /etc/my.cnf.d/server.cnf
[mysqld]
character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
init_connect = "SET NAMES 'utf8mb4'"
[root@node7-11 ~]# vi /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set = utf8mb4
[root@node7-11 ~]# systemctl start mariadb
[root@node7-11 ~]# mysqladmin -uroot password
执行数据库脚本及用户授权
[root@node7-11 ~]# wget https://raw.githubusercontent.com/ctripcorp/apollo/master/scripts/sql/apolloconfigdb.sql -O apolloconfigdb.sql
[root@node7-11 ~]# mysql -uroot -p < apolloconfigdb.sql
MariaDB [ApolloConfigDB]> grant INSERT,DELETE,UPDATE,SELECT on ApolloConfigDB.* to 'apolloconfig'@'10.4.7.%' identified by "123456";
修改Eureka服务Url
MariaDB [ApolloConfigDB]> update ApolloConfigDB.ServerConfig set ServerConfig.Value="http://config.od.com/eureka" where ServerConfig.Key="eureka.service.url";
解析域名
[root@node7-11 ~]# vi /var/named/od.com.zone
[root@node7-11 ~]# systemctl restart named
[root@node7-11 ~]# dig -t A config.od.com @10.4.7.11 +short
10.4.7.10
制作docker镜像
[root@node7-200 ~]# cd /opt/src/
[root@node7-200 src]# wget https://github.com/ctripcorp/apollo/releases/download/v1.5.1/apollo-configservice-1.5.1-github.zip
[root@node7-200 src]# mkdir /data/dockerfile/apollo-configservice
[root@node7-200 src]# unzip -o apollo-configservice-1.5.1-github.zip -d /data/dockerfile/apollo-configservice
[root@node7-200 src]# cd /data/dockerfile/apollo-configservice/
[root@node7-200 apollo-configservice]# rm -f apollo-configservice-1.5.1-sources.jar
配置数据库连接
[root@node7-200 apollo-configservice]# vi config/application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = apolloconfig
spring.datasource.password = 123456
#apollo.eureka.server.enabled=true
#apollo.eureka.client.enabled=true
[root@node7-200 apollo-configservice]# rm -f scripts/shutdown.sh
修改启动脚本
[root@node7-200 apollo-configservice]# vi scripts/startup.sh
#!/bin/bash
SERVICE_NAME=apollo-configservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/apollo-config-server
## Adjust server port if necessary
SERVER_PORT=8080
APOLLO_CONFIG_SERVICE_NAME=$(hostname -i)
SERVER_URL="http://${APOLLO_CONFIG_SERVICE_NAME}:${SERVER_PORT}"
## Adjust memory settings if necessary
export JAVA_OPTS="-Xms128m -Xmx128m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:SurvivorRatio=8"
## Only uncomment the following when you are using server jvm
#export JAVA_OPTS="$JAVA_OPTS -server -XX:-ReduceInitialCardMarks"
########### The following is the same for configservice, adminservice, portal ###########
export JAVA_OPTS="$JAVA_OPTS -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+DisableExplicitGC -XX:+ScavengeBeforeFullGC -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+ExplicitGCInvokesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom"
export JAVA_OPTS="$JAVA_OPTS -Dserver.port=$SERVER_PORT -Dlogging.file=$LOG_DIR/$SERVICE_NAME.log -XX:HeapDumpPath=$LOG_DIR/HeapDumpOnOutOfMemoryError/"
# Find Java
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
javaexe="$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then
javaexe=$(type -p java)
elif [[ -x "/usr/bin/java" ]]; then
javaexe="/usr/bin/java"
else
echo "Unable to find Java"
exit 1
fi
if [[ "$javaexe" ]]; then
version=$("$javaexe" -version 2>&1 | awk -F '"' '/version/ {print $2}')