这里直接复制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"
docker logs -f portal
由于没有配置dev的配置地址,所以会一直请求http://apollo.meta
apollo.portal.meta.servers
,然后点击查询
按钮{
"dev":"http://configservice:8080"
}
然后点击保存
再次查看portal日志,当出现下面的输出字样,代表dev环境初始化成功
这里我使用的是apollo: 2.0.0,因此客户端也需要2.0.0
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实现跨主机通信(使用静态路由方式)
高可用环境相关脚本下载地址
在apollo-ha目录下创建mysql目录
mkdir -p mysql && cd mysql
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
这里使用docker hub中提供的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
mkdir configservice && cd configservice
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
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
mkdir adminservice && cd adminservice
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
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
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
{
"dev": "http://configservice-ha:8080",
"fws": "http://configservice-ha:8080",
"pro": "http://configservice-ha:8080"
}
docker-compose up -d
app:
id: demo
apollo:
bootstrap:
enabled: true
eagerLoad:
enabled: true
namespaces: dev
meta: http://192.168.101.170:8080
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);
}
}