部署apollo多环节需要主机已部署以下组件:

  • docker
  • docker-compose
  • mysql

安装docker

在线安装

yum -y install docker-ce
systemctl enable docker
systemctl start docker

离线安装

​​离线部署包下载​​

#!/bin/bash
# 创建docker安装目录,离线部署包找作者拿
read -p "输入docker安装目录:" TempDockerDir

function Basic() {
mkdir $TempDockerDir
cp ./docker-20.10.12.tgz $TempDockerDir
cp ./docker-compose-linux-x86_64 $TempDockerDir
cd $TempDockerDir
tar -xf docker-20.10.12.tgz
cp docker/* /usr/bin/
mv docker docker_source
}

function EditServiceConfig() {
cat > /etc/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd -g /opt/docker
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target
EOF
chmod a+x /etc/systemd/system/docker.service
chmod 777 /etc/systemd/system/docker.service
systemctl daemon-reload
systemctl start docker
systemctl status docker
systemctl enable docker
}
function InstallCompose() {
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/
chmod +x /usr/local/bin/docker-compose
docker-compose -v
}

Basic
EditServiceConfig
InstallCompose

安装mysql

安装忽略自行安装
我这里借用已有mysql

多环境apollo安装

数据库初始化

拉取sql文件

git clone https://github.com/apolloconfig/apollo.git

初始化数据库结构

mysql -h xxx -uroot -pxxx -P31111 < apollo/scripts/sql/apolloconfigdb.sql
mysql -h xxx -uroot -pxxx -P31111 < apollo/scripts/sql/apolloportaldb.sql

多环镜数据库初始化

因为我这里有三个环境分别是dev,test,pro;所以apolloconfigdb.sql需要把数据库名字分别改成对应三个环境

mkdir ./sql
cp ../apollo/scripts/sql/apolloconfigdb.sql ./sql/
cp ../apollo/scripts/sql/apolloportaldb.sql ./sql/
cp apolloconfigdb.sql apolloconfigdbdev.sql
cp apolloconfigdb.sql apolloconfigdbtest.sql
cp apolloconfigdb.sql apolloconfigdbpro.sql
mysql -h xxx -uroot -pxxx -P31111 < ./apolloconfigdbdev.sql
mysql -h xxx -uroot -pxxx -P31111 < ./apolloconfigdbtest.sql
mysql -h xxx -uroot -pxxx -P31111 < ./apolloconfigdbpro.sql

配置文件

创建上下文目录

mkdir apollo
cd apollo

.env

# eth0网卡
MY_HOST=xxx
SPRING_DATASOURCE_HOST_PORT=xxx:31111
SPRING_DATASOURCE_USERNAME=root
SPRING_DATASOURCE_PASSWORD=xxx
SERVER_PORT_CONFIG_DEV=8081
SERVER_PORT_CONFIG_TEST=8082
SERVER_PORT_CONFIG_PRO=8083
SERVER_PORT_ADMIN_DEV=8091
SERVER_PORT_ADMIN_TEST=8092
SERVER_PORT_ADMIN_PRO=8093
SERVER_PORT_PORTAL=8071

apollo-compose.yml

version: '3'

services:
apolloConfigservice-dev:
container_name: apolloConfigservice-dev
image: apolloconfig/apollo-configservice
volumes:
- ./config_dev_logs:/opt/logs
ports:
- "${SERVER_PORT_CONFIG_DEV}:8080"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/ApolloConfigDBdev?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
restart: always

apolloConfigservice-test:
container_name: apolloConfigservice-test
image: apolloconfig/apollo-configservice
volumes:
- ./config_test_logs:/opt/logs
ports:
- "${SERVER_PORT_CONFIG_TEST}:8080"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/ApolloConfigDBtest?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
restart: always

apolloConfigservice-pro:
container_name: apolloConfigservice-pro
image: apolloconfig/apollo-configservice
volumes:
- ./config_pro_logs:/opt/logs
ports:
- "${SERVER_PORT_CONFIG_PRO}:8080"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/ApolloConfigDBpro?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
restart: always

apolloAdminservice-dev:
depends_on:
- apolloConfigservice-dev
container_name: apolloAdminservice-dev
image: apolloconfig/apollo-adminservice
volumes:
- ./admin_dev_logs:/opt/logs
ports:
- "${SERVER_PORT_ADMIN_DEV}:8090"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/ApolloConfigDBdev?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
restart: always

apolloAdminservice-test:
depends_on:
- apolloConfigservice-test
container_name: apolloAdminservice-test
image: apolloconfig/apollo-adminservice
volumes:
- ./admin_test_logs:/opt/logs
ports:
- "${SERVER_PORT_ADMIN_TEST}:8090"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/ApolloConfigDBtest?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
restart: always

apolloAdminservice-pro:
depends_on:
- apolloConfigservice-pro
container_name: apolloAdminservice-pro
image: apolloconfig/apollo-adminservice
volumes:
- ./admin_pro_logs:/opt/logs
ports:
- "${SERVER_PORT_ADMIN_PRO}:8090"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/ApolloConfigDBpro?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
restart: always

apolloPortal:
depends_on:
- apolloAdminservice-dev
- apolloAdminservice-test
- apolloAdminservice-pro
container_name: apolloPortal
image: apolloconfig/apollo-portal
volumes:
- ./portal_logs:/opt/logs
ports:
- 8071:8070
ports:
- "${SERVER_PORT_PORTAL}:8070"
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${SPRING_DATASOURCE_HOST_PORT}/ApolloPortalDB?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
- APOLLO_PORTAL_ENVS=dev,test,pro
- DEV_META=http://${MY_HOST}:${SERVER_PORT_CONFIG_DEV}
- TEST_META=http://${MY_HOST}:${SERVER_PORT_CONFIG_TEST}
- PRO_META=http://${MY_HOST}:${SERVER_PORT_CONFIG_PRO}
restart: always

验证

启动项目

docker-compose -f apollo-compose.yml up -d
docker-compose -f apollo-compose.yml ps

docker部署apollo多环境_容器

控制台访问

更换自己的ip
http://localhost:8071
默认账户密码apollo/admin

常见问题

# c.c.f.a.portal.component.PortalSettings  : Env is down
#ApolloConfigDBdev
update ServerConfig set Value = 'http://外部可访问域名或ip:8081/eureka/' where id =1;
#ApolloConfigDBtest
update ServerConfig set Value = 'http://外部可访问域名或ip:8082/eureka/' where id =1;
#ApolloConfigDBpro
update ServerConfig set Value = 'http://外部可访问域名或ip:8083/eureka/' where id =1;

# 查看注册的服务
http://localhost:8081/services/admin
http://localhost:8082/services/admin
http://localhost:8083/services/admin
# 可以看到的结果
[
{
"appName": "APOLLO-ADMINSERVICE",
"instanceId": "4e0d00c4e19e:apollo-adminservice:8090",
"homepageUrl": "http://localhost:8090/"
}
]