跳到主要内容

nginx+Gateway集群部署

这里采用的是 单nginx,多Gateway实例的方式

修改DockerFile

修改构建镜像的DockerFile,这里因为是通过桥接模式,所以需要在不同的bootstrap.yml中指定注册的ip和端口,所以在构建镜像的时候去用不同的bootstrap.yml,具体的路径通过环境变量传入

FROM adoptopenjdk:8-jdk-hotspot

WORKDIR /opt

ADD *.jar casll-gateway.jar

EXPOSE 8080

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone

# ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /opt/casll-gateway.jar --spring.config.location=/opt/config/bootstrap.yml"]
# 允许通过环境变量指定配置文件路径,默认为 /opt/config/bootstrap.yml
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /opt/casll-gateway.jar --spring.config.location=${CONFIG_PATH:-/opt/config/bootstrap.yml}"]

修改docker-compose

docker-compose.yml中添加一个服务

修改网关端口和容器名称,写一个和之前不一样的即可,并且需要指定bootstrap.yml的具体位置,在不同的bootstrap中,写不同的注册ip和端口:

version: '3'
services:
casll-gateway:
image: casll/gateway:latest
container_name: casll-gateway
restart: always
ports:
- "18001:8080"
volumes:
- ./config:/opt/config
- ./logs:/opt/logs
network_mode: bridge
environment:
- CONFIG_PATH=/opt/config/bootstrap.yml # 指定实例1的配置文件路径

casll-gateway-2:
image: casll/gateway:latest
container_name: casll-gateway-2
restart: always
ports:
- "18011:8080"
volumes:
- ./config:/opt/config
- ./logs:/opt/logs
network_mode: bridge
environment:
- CONFIG_PATH=/opt/config/bootstrap2.yml # 指定实例2的配置文件路径

启动

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

修改nginx


upstream gateway_cluster {
# 配置两个网关服务器的地址和权重(可选)
server 127.0.0.1:18001 ;
server 127.0.0.1:18011 ;
}
location / {
proxy_pass http://gateway_cluster;
}