一、springboot配置多个nacos服务器
1.1 bootstrap.yml配置单环境,Dockerfile打包时指定nacos地址
bootstrap.yml
server:
port: 9999
spring:
application:
name: @artifactId@
cloud:
nacos:
discovery:
server-addr: ${NACOS_HOST:cbam-register}:${NACOS_PORT:8848}
namespace: ${NACOS_NAME_SPACE:2b6e230c-de4e-4005-af5f-1ee07dc45a74}
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
namespace: ${NACOS_NAME_SPACE:2b6e230c-de4e-4005-af5f-1ee07dc45a74}
group: ${NACOS_GROUP:DEV}
file-extension: yml
shared-configs[0]:
data-id: application.${spring.cloud.nacos.config.file-extension}
group: ${NACOS_GROUP:DEV}
Dockerfile
FROM harbor.dcos.local/platpublic/java:8
MAINTAINER cbam-auth-gateway
ARG JAR_FILE
COPY target /usr/local/jar/
RUN cp /usr/local/jar/cbam-gateway.jar /usr/local/jar/cbam-auth-gateway.jar
ENTRYPOINT ["java","-jar","/usr/local/jar/cbam-auth-gateway.jar","--spring.profiles.active=tg","--spring.cloud.nacos.config.group=TG","--spring.cloud.nacos.discovery.server-addr=172.24.131.153:8848"]
ENV TZ=Asia/Shanghai \
DEBIAN_FRONTEND=noninteractive
RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& rm -rf /var/lib/apt/lists/*
1.2 bootstrap.yml配置多环境,Dockerfile打包时指定环境
bootstrap.yml
server:
port: 9999
spring:
profiles:
active: dev
---
# 开发环境配置
spring:
application:
name: @artifactId@
cloud:
nacos:
discovery:
server-addr: ${NACOS_HOST:cbam-register}:${NACOS_PORT:8848}
namespace: ${NACOS_NAME_SPACE:2b6e230c-de4e-4005-af5f-1ee07dc45a74}
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
namespace: ${NACOS_NAME_SPACE:2b6e230c-de4e-4005-af5f-1ee07dc45a74}
group: ${NACOS_GROUP:DEV}
file-extension: yml
shared-configs[0]:
data-id: application.${spring.cloud.nacos.config.file-extension}
group: ${NACOS_GROUP:DEV}
config:
activate:
on-profile: dev
---
# 测试环境配置
spring:
application:
name: @artifactId@
cloud:
nacos:
discovery:
server-addr: 172.24.131.153:8848
namespace: ${NACOS_NAME_SPACE:2b6e230c-de4e-4005-af5f-1ee07dc45a74}
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
namespace: ${NACOS_NAME_SPACE:2b6e230c-de4e-4005-af5f-1ee07dc45a74}
group: ${NACOS_GROUP:TG}
file-extension: yml
shared-configs[0]:
data-id: application.${spring.cloud.nacos.config.file-extension}
group: ${NACOS_GROUP:TG}
config:
activate:
on-profile: tg
Dockerfile
FROM harbor.dcos.local/platpublic/java:8
MAINTAINER cbam-auth-gateway
ARG JAR_FILE
COPY target /usr/local/jar/
RUN cp /usr/local/jar/cbam-gateway.jar /usr/local/jar/cbam-auth-gateway.jar
ENTRYPOINT ["java","-jar","/usr/local/jar/cbam-auth-gateway.jar","--spring.profiles.active=tg"]
ENV TZ=Asia/Shanghai \
DEBIAN_FRONTEND=noninteractive
RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& rm -rf /var/lib/apt/lists/*
二、k8s配置容器hosts
k8s deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cbam-auth-gateway-deployment
labels:
app: cbam-auth-gateway
spec:
replicas: 1
selector:
matchLabels:
app: cbam-auth-gateway
template:
metadata:
labels:
app: cbam-auth-gateway
spec:
hostAliases:
- ip: "172.24.131.153"
hostnames:
- "cbam-register"
- "cbam-gateway"
- "cbam-mysql"
- ip: "172.25.32.12"
hostnames:
- "cbam-redis"
- ip: "172.25.89.2"
hostnames:
- "cbam-oracle"
containers:
- name: cbam-auth-gateway
...
三、注意
1、由于我的spring.cloud.nacos.config.shared-configs 写死为 application.yml 了,所以nacos配置中心配置多环境时只能用Group来区分了,不可以使用类似application-dev.yml这种写法了,这样是读不到的。所以nacos配置管理中的不同环境shared-configs的Data Id应该名称都为application.yml,通过不同的Group值来区分不同环境的配置。
2、docker容器中应用使用hosts的话在deployment.yml中配置hosts即可,配置方式就是上文 “二、k8s配置容器hosts” 的方式,这样容器启动后进入容器通过cat /etc/hosts命令可以查到已配置的hosts信息。
3、通过2的方式配置的hosts在容器启动时还未生效,如果只配置此处可能导致容器无法正常启动,例如nacos注册中心地址也通过hosts的方式配在了微服务的boostrap.yml里面,则docker制作镜像时需要指定nacos地址,具体方法就是上文 " 一、springboot配置多个nacos服务器" 中的两种方法。