文章目录
- 1、apollo
- 2、数据库准备
- 3、启动后会用到的几个地址
- 4、docker-compose运行 apollo
- 方式一:使用容器 hostName 作为网络媒介
- apollo 客户端通过宿主机端口 拉取配置(推荐)
- apollo 客户端通过 自定义hostName 拉取配置
- 方式二:使用端口映射+固定 ip 作为网络媒介
- 6、客户端
1、apollo
https://www.apolloconfig.com/#/zh/deployment/quick-start-docker
单机单环境
单机双环境
单机三环境
2、数据库准备
- 启动本地 mysql
- 初始化数据库
CREATE DATABASE ApolloConfigDB;
CREATE DATABASE ApolloPortalDB;
- 新增用户
userapollo/userapollo
CREATEUSER'userapollo'@'%' IDENTIFIED BY'userapollo';
GRANTALL PRIVILEGES ON ApolloConfigDB.*TO'userapollo'@'%';
GRANTALL PRIVILEGES ON ApolloPortalDB.*TO'userapollo'@'%';
FLUSH PRIVILEGES;
- 表结构&数据初始化
https://github.com/apolloconfig/apollo/tree/master/scripts/sql
3、启动后会用到的几个地址
-
http://127.0.0.1:8070/
Portal 登录地址,默认登录账户 apollo/admin -
http://127.0.0.1:8080/
Eureka 管理页面
-
http://127.0.0.1:8080/services/config
service-config 配置查询接口
未来客户端通过下面 url 获取配置信息的拉取 url,即 homepageUrl 字段
客户端访问 http://127.0.0.1:8080/services/config
[{"appName":"APOLLO-CONFIGSERVICE","instanceId":"config-service:apollo-configservice:8080","homepageUrl":"http://config-service:8080"}]
-
http://127.0.0.1:8070/system_info.html
Portal 登录页面,查看 config-service、admin-service 运行状态
-
http://127.0.0.1:8070/server_config_manage.html#/portal_db
配置 apollo.portal.meta.servers -
http://127.0.0.1:8070/server_config_manage.html#/config_db
配置 eureka.service.url
4、docker-compose运行 apollo
须知1:https://blog.csdn.net/bestcxx/article/details/140250249
须知 2: 在启动顺序上,首次启动可能会失败,原因是 admin-service 、portal 需要等待 config-service 完全启动之后才能启动,这里建议首次启动后,重启 admin-service 和 portal
docker-compose 首次启动后,可以借助 Docker Desktop 管理Docker 容器,来进行重启操作
方式一:使用容器 hostName 作为网络媒介
- 修改数据库配置
UPDATE `ApolloConfigDB`.`ServerConfig` SET `Value` = 'http://config-service:8080/eureka/' WHERE (`Id` = '1');
UPDATE `ApolloPortalDB`.`ServerConfig` SET `Value` = '{\"dev\":\"http://config-service:8080\"}' WHERE (`Id` = '8');
apollo 客户端通过宿主机端口 拉取配置(推荐)
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://127.0.0.1:8080
客户端通过 http://127.0.0.1:8080/services/config 获得
[{"appName":"APOLLO-CONFIGSERVICE","instanceId":"config-service:apollo-configservice:8080","homepageUrl":"http://127.0.0.1:8080"}]
此时 homepageUrl 为宿主机 IP:端口,通过端口映射方式完成请求转发
version: '3.6'
services:
config-service:
image: apolloconfig/apollo-configservice
container_name: apollo-config-service
hostname: config-service
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://127.0.0.1:8080
ports:
- 8080:8080 # 宿主机端口 : 容器端口
networks:
apollo-network:
admin-service:
image: apolloconfig/apollo-adminservice
container_name: apollo-admin-service
hostname: admin-service
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- CONFIGSERVICE_URL=http://config-service:8080
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://admin-service:8090
ports:
- 8090:8090
depends_on:
- config-service
networks:
apollo-network:
portal:
image: apolloconfig/apollo-portal
container_name: apollo-portal
hostname: portal
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- APOLLO_PORTAL_ENVS=dev
- DEV_META=http://config-service:8080
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://portal:8070
ports:
- 8070:8070
depends_on:
- config-service
- admin-service
networks:
apollo-network:
networks:
apollo-network:
#driver: bridge
apollo 客户端通过 自定义hostName 拉取配置
- 修改 宿主机 /etc/hosts 文件
127.0.0.1 config-service
- docker-compose.yml
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://config-service:8080
客户端通过 http://127.0.0.1:8080/services/config 获得
[{"appName":"APOLLO-CONFIGSERVICE","instanceId":"config-service:apollo-configservice:8080","homepageUrl":"config-service:8080"}]
此时 homepageUrl 为自定义 hostName:端口,宿主机将根据 /etc/hosts 配置将请求映射本机 127.0.0.1,然后通过端口映射方式完成转发,这种方式需要本地配置私有化域名
version: '3.6'
services:
config-service:
image: apolloconfig/apollo-configservice
container_name: apollo-config-service
hostname: config-service
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://config-service:8080
ports:
- 8080:8080 # 宿主机端口 : 容器端口
networks:
apollo-network:
admin-service:
image: apolloconfig/apollo-adminservice
container_name: apollo-admin-service
hostname: admin-service
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- CONFIGSERVICE_URL=http://config-service:8080
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://admin-service:8090
ports:
- 8090:8090
depends_on:
- config-service
networks:
apollo-network:
portal:
image: apolloconfig/apollo-portal
container_name: apollo-portal
hostname: portal
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- APOLLO_PORTAL_ENVS=dev
- DEV_META=http://config-service:8080
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://portal:8070
ports:
- 8070:8070
depends_on:
- config-service
- admin-service
networks:
apollo-network:
networks:
apollo-network:
#driver: bridge
方式二:使用端口映射+固定 ip 作为网络媒介
- 修改数据库配置
UPDATE `ApolloConfigDB`.`ServerConfig` SET `Value` = 'http://192.168.128.2:8080/eureka/' WHERE (`Id` = '1');
UPDATE `ApolloPortalDB`.`ServerConfig` SET `Value` = '{\"dev\":\"http://192.168.128.2:8080\"}' WHERE (`Id` = '8');
- docker-compose.yml
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://config-service:8080
客户端通过 http://127.0.0.1:8080/services/config 获得
[{"appName":"APOLLO-CONFIGSERVICE","instanceId":"config-service:apollo-configservice:8080","homepageUrl":"config-service:8080"}]
此时 homepageUrl 为容器内部 hostName:端口,宿主机将根据 /etc/hosts 配置将请求映射本机 127.0.0.1,然后通过端口映射方式完成转发
version: '3.6'
services:
config-service:
image: apolloconfig/apollo-configservice
container_name: apollo-config-service
hostname: config-service
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- JAVA_OPTS=-Deureka.instance.homePageUrl=http://127.0.0.1:8080
ports:
- 8080:8080 # 宿主机端口 : 容器端口
networks:
apollo-network:
ipv4_address: 192.168.128.2
admin-service:
image: apolloconfig/apollo-adminservice
container_name: apollo-admin-service
hostname: admin-service
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- CONFIGSERVICE_URL=http://192.168.128.2:8080
ports:
- 8090:8090
depends_on:
- config-service
networks:
apollo-network:
ipv4_address: 192.168.128.3
portal:
image: apolloconfig/apollo-portal
container_name: apollo-portal
hostname: portal
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://host.docker.internal:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=userapollo
- SPRING_DATASOURCE_PASSWORD=userapollo
- APOLLO_PORTAL_ENVS=dev
- DEV_META=http://192.168.128.2:8080
ports:
- 8070:8070
depends_on:
- config-service
- admin-service
networks:
apollo-network:
ipv4_address: 192.168.128.4
networks:
apollo-network:
driver: bridge
ipam:
config:
- subnet: 192.168.128.0/24
6、客户端
{
"apollo": {
"AppId": "helloabp",
"Env": "dev",
"Cluster": "default",
"MetaServer": "http://localhost:8080",
"Namespaces": [
"helloapollo.web.json"
]
},
"ClientId": "bestcxxLocal"
}