springcloud + nacos config多环境配置文件实践

背景:

1、基于开发环境(兼测试环境)、生产环境,双环境下的配置文件管理

2、多环境理论是上类开发环境/生产环境的多重组合

问题:

一、关于bootstrap和application两个配置文件的区别和先后顺序

二、配置文件放置位置的选择

1、测试环境:配置文件打算直接放置于项目内,本地直接启动,默认spring.profiles.active=dev

2、生产环境:指向性的参数直接放置于项目内,项目打包发布线上,jar包启动命令带上spring.profiles.active=prod

最后的解决方案:

1、测试环境:

顺序:

bootstrap.yaml -> (放置于当前模块)

bootstrap-dev.yaml ->(放置于当前模块)

bootstrap-common-dev.yaml ->(放置于公共引入模块)

application-dev.yaml->(放置于当前模块)

application-common-dev.yaml->(放置于公共引入模块)

当前模块可在pom.xml中使用dependency引入公共模块

1)bootstrap.yaml:

spring:
  application:
    #项目名
    name: user-service
  profiles:
    active: dev

2)bootstrap-dev.yaml:spring.profiles.include=common-dev,由于多模块的配置一致,提取到功能模块

spring:
  profiles:
    include: common-dev
server:
  port: 8083

3)bootstrap-common-dev.yaml:

spring:
  cloud:
    nacos:
      #配置直接放项目里
      config:
        enabled: false
      #注册中心(服务发现)
      discovery:
        server-addr: localhost:8888
management:
  endpoints:
    web:
      exposure:
        include: "*"
feign:
  sentinel:
    enabled: true

4)application-dev.yaml:放置当前模块的独有配置

统一配置采用spring.profiles.include=common-dev,由于多模块的配置很多是一致,提取到功能模块

spring:
  profiles:
    include: common-dev

5) application-common-dev.yaml:

management:
  endpoints:
    web:
      exposure:
        include: "*"
feign:
  sentinel:
    enabled: true
spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:9999

2、正式环境(user-service是当前模块的服务名)

顺序:

bootstrap.yaml -> (放置于当前模块,正式环境启动命令指定spring.profiles.active=prod)

bootstrap-prod.yaml ->(放置于当前模块)

bootstrap-common-prod.yaml ->(放置于公共引入模块)

user-service-prod.yaml->(放置于nacos)

platform-share-prod.yaml->(放置于nacos)

1)bootstrap.yaml

spring:
  application:
    #项目名
    name: user-service
  profiles:
    active: dev

2)bootstrap-prod.yaml

spring:
  profiles:
    include: common-prod
  cloud:
    nacos:
      config:
#用于指定nacos公共配置文件,data-id+group
        shared-configs:
          - data-id: platform-share-prod.yaml
            group: DEFAULT_GROUP

3)bootstrap-common-prod.yaml(指定nacos的地址,这块因为都是同样的配置,这边提取出来)

spring:
  cloud:
    nacos:
      config:
        server-addr: localhost:8888
        file-extension: yaml
      discovery:
        server-addr: localhost:8888

4)user-service-prod.yaml(等同于测试环境当前模块的application-dev.yaml)

server:
  port: 6688

5)platform-share-prod.yaml(等同于测试环境的application-common-dev.yaml)

management:
  endpoints:
    web:
      exposure:
        include: "*"
feign:
  sentinel:
    enabled: true
经验分享 程序员 微信小程序 职场和发展