springboot bootstrap.yml多环境配置 & k8s配置容器hosts

一、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服务器" 中的两种方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值