Springboot configures multiple NACOS servers

1.1 Bootstrap. yml is configured with a single environment, and the nacOS address is specified when Dockerfile is packaged

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}
Copy the code

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/*
Copy the code

1.2 Bootstrap. yml configure multiple environments and specify the environment when Dockerfile is packaged

bootstrap.yml

server:
  port: 9999
spring:
  profiles:
    active: dev

---
Configure the development environment

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

---
# Test environment configurationSpring: 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
Copy the code

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/*
Copy the code

K8s configures container 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
       
       ...
Copy the code

Third, pay attention to

1, because of my spring. Cloud. Nacos. Config. The Shared – configs write die for application. Yml, so nacos configuration center configuration environment can only be used when more Group to differentiate, You can’t use a script like application-dev.yml because you can’t read it. Therefore, the Data Id of the shared-configs in nacOS configuration management should be named application.yml, and different Group values should be used to distinguish the configurations of different environments.

2. If hosts is used in the Docker container, configure hosts in the deployment.yml file. After the container is started, you can enter the container and run the cat /etc/hosts command to check the configured hosts information.

3. The hosts configured in the mode of 2 does not take effect when the container is started. If only configured in this mode, the container may fail to start normally. The specific methods are the two methods in the above “A, Springboot configure multiple NACOS servers”.