搭建学习环境
编写docker-compose脚本
这里直接复制docker hub中镜像仓库中提供的脚本,镜像地址请点击下面apollo字样的连接即可
apollo
version: '3'
services:
portal:
image: lhstack/apollo:portal-2.0.0
container_name: portal
restart: always
environment:
DB_HOST: "apollo-mysql"
DB_PASSWORD: "654321"
depends_on:
mysql:
condition: service_healthy
configservice:
condition: service_healthy
ports:
- '8070:8080'
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
adminservice:
image: lhstack/apollo:adminservice-2.0.0
container_name: adminservice
restart: always
ports:
- '8090:8080'
environment:
EUREKA_SERVER_ENABLE: "true"
EUREKA_URLS: "http://configservice:8080/eureka"
DB_HOST: "apollo-mysql"
DB_PASSWORD: "654321"
depends_on:
mysql:
condition: service_healthy
configservice:
condition: service_healthy
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
configservice:
image: lhstack/apollo:configservice-2.0.0
container_name: configservice
restart: always
ports:
- '8080:8080'
environment:
EUREKA_SERVER_ENABLE: "true"
DB_HOST: "apollo-mysql"
DB_PASSWORD: "654321"
depends_on:
mysql:
condition: service_healthy
healthcheck:
test: ["CMD-SHELL","wget --no-cache --spider 'http://localhost:8080/health' || exit 1"]
timeout: 2s
interval: 10s
retries: 5
start_period: 15s
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
mysql:
image: mysql:5.7.28
container_name: apollo-mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: "654321"
healthcheck:
test: ["CMD","mysqladmin","-uroot","-p$${MYSQL_ROOT_PASSWORD}","ping","-h","localhost"]
timeout: 2s
interval: 10s
retries: 5
start_period: 5s
volumes:
- ./data:/var/lib/mysql
- ./script:/docker-entrypoint-initdb.d/
deploy:
resources:
limits:
memory: "256M"
cpus: "50m"
创建script目录,并导入sql脚本
- 点击镜像地址中的携程Apollo Gitee地址
- 切换到对应版本分支,复制sql脚本,并放到script目录中
- 最终目录结构如下
执行docker-compose创建容器
- 浏览器访问configservice
- 访问portal
初始化环境
- 查看portal日志,可以发现,一直在重试请求http://apollo.meta
docker logs -f portal
由于没有配置dev的配置地址,所以会一直请求http://apollo.meta
- 这里我们点击portal应用的系统参数选项,把dev环境的地址配置上去
key后面的输入框输入apollo.portal.meta.servers
,然后点击查询
按钮
将value修改为以下内容
{
"dev":"http://configservice:8080"
}
然后点击保存
再次查看portal日志,当出现下面的输出字样,代表dev环境初始化成功
创建测试应用
启动demo,测试拉取配置
添加依赖
这里我使用的是apollo: 2.0.0,因此客户端也需要2.0.0
添加配置
在portal中增加以下配置,并点击发布
应用配置
启动类开启ApolloConfig配置,并设置环境为dev
- application.yml设置apollo相关配置
app:
id: test
apollo:
bootstrap:
enabled: true
eagerLoad:
enabled: true
config-service: http://192.168.101.170:8080
启动如下
搭建高可用生产环境
以下操作都在apollo-ha目录下面操作
由于高可用需要使用服务发现,这里docker-compose里面的应用注册的ip是内网地址,如果要让windows能访问虚拟机上面docker中的内网地址,请参考我下面这篇博客,后面相关访问问题不在这做解释
docker实现跨主机通信(使用静态路由方式)
搭建mysql
创建mysql目录并初始化脚本
在apollo-ha目录下创建mysql目录
mkdir -p mysql && cd mysql
- 编写docker-compose.yml脚本
version: '3'
services:
apollo-mysql:
image: mysql:5.7.28
container_name: apollo-mysql
restart: always
ports:
- '3306:3306' # 这里需要暴露端口,用mysql客户端修改apollo一些配置
environment:
MYSQL_ROOT_PASSWORD: "123456"
deploy:
resources:
limits:
cpus: '30m'
memory: '256M'
networks:
- apollo
volumes:
- ./data:/var/lib/mysql
- ./script:/docker-entrypoint-initdb.d/
healthcheck:
test: ["CMD","mysqladmin","-uroot","-p123456","ping","-h","localhost"]
timeout: 2s
interval: 10s
retries: 5
start_period: 5s
networks:
apollo:
name: apollo
driver: bridge
ipam:
driver: default
config:
- subnet: 10.42.0.0/16
- 目录最终如下
启动和远程访问
- 启动
docker-compose up -d
- 远程访问
搭建eureka集群
这里使用docker hub中提供的eureka镜像
eureka
创建eureka目录并初始化脚本
mkdir eureka && cd eureka
version: '3'
services:
eureka-1:
container_name: eureka-1
image: lhstack/eureka
ports:
- 8761:8761
deploy:
resources:
limits:
cpus: '1'
memory: '256M'
logging:
options:
max-size: "1kb"
max-file: "1"
environment:
JAVA_OPTS: "-Xmx128m -Xms128m"
SECURITY_ENABLE: true
SECURITY_USERNAME: admin
SECURITY_PASSWORD: admin
EUREKA_SERVER_URLS: "http://admin:admin@eureka-1:8761/eureka,http://admin:admin@eureka-2:8761/eureka"
networks:
- apollo
eureka-2:
container_name: eureka-2
image: lhstack/eureka
ports:
- 8762:8761
deploy:
resources:
limits:
cpus: '1'
memory: '256M'
logging:
options:
max-size: "1kb"
max-file: "1"
environment:
JAVA_OPTS: "-Xmx128m -Xms128m"
SECURITY_ENABLE: true
SECURITY_USERNAME: admin
SECURITY_PASSWORD: admin
EUREKA_SERVER_URLS: "http://admin:admin@eureka-1:8761/eureka,http://admin:admin@eureka-2:8761/eureka"
networks:
- apollo
networks:
apollo:
name: apollo
最终目录如下
启动并通过浏览器访问
docker-compose up -d
搭建apollo-configservice集群
创建configservice目录并初始化脚本
mkdir configservice && cd configservice
- docker-compose.yaml
version: '3'
services:
configservice-ha:
image: nginx:alpine
container_name: configservice-ha
restart: always
ports:
- '8080:8080'
logging:
options:
max-file: '1'
max-size: '16k'
deploy:
resources:
limits:
memory: '16M'
cpus: '16k'
volumes:
- ./conf.d:/etc/nginx/conf.d
networks:
- apollo
depends_on:
configservice1:
condition: service_healthy
configservice2:
condition: service_healthy
configservice1:
image: lhstack/apollo:configservice-2.0.0
container_name: configservice1
restart: always
environment:
DB_HOST: "apollo-mysql"
DB_PASSWORD: "123456"
networks:
- apollo
healthcheck:
test: ["CMD-SHELL","wget --no-cache --spider 'http://localhost:8080/health' || exit 1"]
timeout: 2s
interval: 10s
retries: 5
start_period: 15s
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
configservice2:
image: lhstack/apollo:configservice-2.0.0
container_name: configservice2
restart: always
environment:
DB_HOST: "apollo-mysql"
DB_PASSWORD: "123456"
networks:
- apollo
healthcheck:
test: ["CMD-SHELL","wget --no-cache --spider 'http://localhost:8080/health' || exit 1"]
timeout: 2s
interval: 10s
retries: 5
start_period: 15s
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
networks:
apollo:
name: apollo
- nginx配置
mkdir conf.d && cd conf.d
upstream configservice{
server configservice1:8080;
server configservice2:8080;
}
server {
listen 8080;
listen [::]:8080;
server_name configservice.lhstack.com;
root /usr/share/nginx/html;
client_max_body_size 50m;
location / {
proxy_pass http://configservice;
}
}
修改数据库配置
将数据库ApolloConfigDB中ServerConfig表中的eureka.service.url值修改为如下内容
http://admin:admin@eureka-1:8761/eureka,http://admin:admin@eureka-2:8761/eureka
- 最终目录如下
启动并通过浏览器检查
docker-compose up -d
- 查看eureka集群是否有configservice服务
搭建apollo-adminservice集群
创建adminservice目录并初始化脚本
mkdir adminservice && cd adminservice
- docker-compose.yaml
version: '3'
services:
adminservice-ha:
image: nginx:alpine
container_name: adminservice-ha
restart: always
ports:
- '8090:8090'
logging:
options:
max-file: '1'
max-size: '16k'
deploy:
resources:
limits:
memory: '16M'
cpus: '16k'
volumes:
- ./conf.d:/etc/nginx/conf.d
networks:
- apollo
depends_on:
adminservice1:
condition: service_healthy
adminservice2:
condition: service_healthy
adminservice1:
image: lhstack/apollo:adminservice-2.0.0
container_name: adminservice1
restart: always
environment:
DB_HOST: "apollo-mysql"
DB_PASSWORD: "123456"
networks:
- apollo
healthcheck:
test: ["CMD-SHELL","wget --no-cache --spider 'http://localhost:8080/health' || exit 1"]
timeout: 2s
interval: 10s
retries: 5
start_period: 15s
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
adminservice2:
image: lhstack/apollo:adminservice-2.0.0
container_name: adminservice2
restart: always
environment:
DB_HOST: "apollo-mysql"
DB_PASSWORD: "123456"
networks:
- apollo
healthcheck:
test: ["CMD-SHELL","wget --no-cache --spider 'http://localhost:8080/health' || exit 1"]
timeout: 2s
interval: 10s
retries: 5
start_period: 15s
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
networks:
apollo:
name: apollo
- nginx配置
mkdir conf.d && cd conf.d
upstream adminservice{
server adminservice1:8080;
server adminservice2:8080;
}
server {
listen 8090;
listen [::]:8090;
server_name adminservice.lhstack.com;
root /usr/share/nginx/html;
client_max_body_size 50m;
location / {
proxy_pass http://adminservice;
}
}
- 最终目录如下
启动并通过浏览器访问
docker-compose up -d
搭建apollo-portal
创建portal目录并初始化脚本
mkdir portal && cd portal
version: '3'
services:
portal:
image: lhstack/apollo:portal-2.0.0
container_name: portal
restart: always
ports:
- '8070:8080'
environment:
DB_HOST: "apollo-mysql"
DB_PASSWORD: "123456"
networks:
- apollo
deploy:
resources:
limits:
memory: "384M"
cpus: '50m'
logging:
options:
max-file: '2'
max-size: '32k'
networks:
apollo:
name: apollo
最终目录如下
修改数据库配置(避免通过浏览器进行初始化)
在数据库ApolloPortalDB的ServerConfig表中修改以下字段内容如下
apollo.portal.envs
添加测试和生产环境
dev,fws,pro
apollo.portal.meta.servers
补充各环境的meta地址{ "dev": "http://configservice-ha:8080", "fws": "http://configservice-ha:8080", "pro": "http://configservice-ha:8080" }
启动并通过浏览器访问
docker-compose up -d
编写demo并测试
创建应用
添加配置
应用配置
app:
id: demo
apollo:
bootstrap:
enabled: true
eagerLoad:
enabled: true
namespaces: dev
meta: http://192.168.101.170:8080
启动并加载配置
- dev
- fws
- pro
测试修改配置
- 编写接口
package com.example.demo;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableApolloConfig
@RestController
public class DemoApplication {
@Value("${msg:hello world}")
private String msg;
@GetMapping
public String msg(){
return this.msg;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 打开浏览器访问
- 修改配置