引言:目前主流框架Spring cloud,Spring boot,此类项目启动时候非常重要的一点就是加载配置文件,bootstrap.yml的加载顺序优先于application.yml这个是一个共识,在开中,我们经常有dev、sit、uat、pro多环境,为了便捷的切换环境,引用相应的配置文件,项目中可能会有多个配置文件,那么他们都分别怎么加载呢?
server:
port: 8030
servlet:
context-path: /api/csdn-boot
management:
endpoint:
health:
show-details: ALWAYS
server:
port: 8031
endpoints:
web:
exposure:
include: '*'
health:
sentinel:
enabled: false
spring:
profiles:
active: dev #设置激活
application:
name: csdn-boot
cloud: #这一块其实是多余的,激活dev,会去读取bootstrapt-dev.yml覆盖下面的
nacos:
config:
server-addr: nacos.csdn-boot:8848
group: PO_ROS_PLATFORM
file-extension: yaml
shared-configs[0]:
dataId: po_ros-common.yaml
group: PO_ROS_PLATFORM
refresh: true
enabled: true
max-retry: 10
ext-config[0]:
dataId: tibcorv.properties
group: PO_ROS_PLATFORM
refresh: true
discovery:
server-addr: nacos.csdn-boot:8848
group: DEFAULT_GROUP
metadata: {"service.desc": "csdn-boot业务"}
enabled: true
service: csdn-boot
sentinel:
#取消Sentinel控制台 懒加载
#eager: true
enabled: false
transport:
dashboard: sentinel.csdn-boot:8080
port: 8032
datasource:
ds1:
nacos:
server-addr: nacos.csdn-boot:8848
dataId: csdn-boot.json
groupId: PO_ROS_PLATFORM
ruleType: flow
dataType: json
spring:
#profiles:
#active: sit #没有意义的配置
zipkin:
base-url: http://
enabled: false
discovery-client-enabled: false
compression:
enabled: true
sender:
type: web
service:
name: ${spring.application.name}
sleuth:
sampler:
probability: 1
devtools:
restart:
enabled: true
#自动化配置 例外处理
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
type: com.alibaba.druid.pool.DruidDataSource
dynamic:
primary: dataTest # 默认db
datasource:
dw4MesTest:
url: jdbc:oracle:thin:@IP:1521:实例名
username: 123456
password: 123
driver-class-name: oracle.jdbc.OracleDriver
druid:
max-active: 50
max-wait: 10000
min-idle: 3
initial-size: 50
mvc:
throw-exception-if-no-handler-found: true
main:
allow-bean-definition-overriding: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
redis: #不配置 默认就是localhost
host: IP
port: 6379
password:
database: 1
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml,classpath:/mapper/*/*Mapper.xml,classpath:/mapper/*/*/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package:
#打印sql日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
feign:
sentinel:
enabled: true
httpclient:
enabled: true
# 最大连接数,默认:200
max-connections: 1000
# 最大路由,默认:50
max-connections-per-route: 50
# 连接超时,默认:2000/毫秒
connection-timeout: 3000
# 生存时间,默认:900L
time-to-live: 900
time-to-live-unit: seconds
client:
config:
default:
connectTimeout: 3000
readTimeout: 40000
po_ros:
gateway:
enabled: false
gatewayUrl: http://kong.
debug: true
sername: csdn-boot
#系统标识,读取maven property
syscode: '@syscode@'
# swagger配置
swagger:
version: 1.0
title: twork服务
#rest响应aop
base-package: cn.companyName
rest-aop:
base-packages: cn.companyName
permission:
enabled: false
debug: false
server:
port: 8030
servlet:
context-path: /api/csdn-boot
management:
endpoint:
health:
show-details: ALWAYS
server:
port: 8031
endpoints:
web:
exposure:
include: '*'
health:
redis:
enabled: false
sentinel:
enabled: false
spring:
#profiles:
#active: dev 没有意义的配置,如果上面的配置和bootstrapt.yml一直也可以不要,引入的意义一般是nacos地址不同。
application:
name: csdn-boot
cloud:
nacos:
config:
server-addr: nacos.csdn-boot:8848
group: PO_ROS_PLATFORM
file-extension: yaml
shared-configs[0]:
dataId: po_ros-common.yaml
group: PO_ROS_PLATFORM
refresh: true
enabled: false
max-retry: 10
discovery:
server-addr: nacos.csdn-boot:8848
group: DEFAULT_GROUP
metadata: { "service.desc": "csdn-boot业务" }
enabled: false
service: csdn-boot
sentinel:
#取消Sentinel控制台 懒加载
#eager: true
enabled: false
transport:
dashboard: sentinel.csdn-boot:8080
port: 8032
datasource:
ds1:
nacos:
server-addr: nacos.csdn-boot:8848
dataId: csdn-boot.json
groupId: PO_ROS_PLATFORM
ruleType: flow
dataType: json
spring:
#profiles:
#active: local #没有意义的配置
zipkin:
base-url: http://
enabled: false
discovery-client-enabled: false
compression:
enabled: true
sender:
type: web
service:
name: ${spring.application.name}
sleuth:
sampler:
probability: 1
#自动化配置 例外处理
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
type: com.alibaba.druid.pool.DruidDataSource
dynamic:
primary: databaseb #默认db
datasource:
dw4testdb:
url: jdbc:oracle:thin:@IP:1521:实例 #dev
username: 123
password: 123456
driver-class-name: oracle.jdbc.OracleDriver
druid:
max-active: 50
max-wait: 10000
min-idle: 3
initial-size: 50
mvc:
throw-exception-if-no-handler-found: true
main:
allow-bean-definition-overriding: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
#redis 配置
redis:
# host: 127.0.0.1
host: 10.XX.X.XX
port: 6379
password:
database: 1
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml,classpath:/mapper/*/*Mapper.xml,classpath:/mapper/*/*/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: cn.company.package.modules.*.entity
# 打印sql日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
feign:
sentinel:
enabled: true
httpclient:
enabled: true
# 最大连接数,默认:200
max-connections: 1000
# 最大路由,默认:50
max-connections-per-route: 50
# 连接超时,默认:2000/毫秒
connection-timeout: 3000
# 生存时间,默认:900L
time-to-live: 900
time-to-live-unit: seconds
client:
config:
default:
connectTimeout: 3000
readTimeout: 40000
po_ros:
gateway:
enabled: false
gatewayUrl: http://kong.
debug: true
sername: csdn-test
#系统标识,读取maven property
syscode: '@syscode@'
# swagger配置
swagger:
version: 1.0
title: twork服务
#rest响应aop
base-package: cn.company
rest-aop:
base-packages: cn.companyname
permission:
enabled: false
debug: false
prod
prod
dev
dev
true
sit
sit
将bootstrapt-dev.yml的context-path修改为api2/csdn-boot,启动项目发现访问地址为api2/csdn-boot。但是这可能是pom起作用,直接读取了bootstrapt-dev.yml,也可能是读取了bootstrapt.yml来激活的dev配置。
接着我们把bootstrapt.yml删除掉active的标签配置,启动项目显示访问地址为api/csdn-boot,同时报错连接不上application.yml中配置的redis地址。
结论:没有active的配置,并不会读取dev,pom中的配置并不能直接激活dev配置。
bootstrapt.yml--->bootstrapt-dev.yml--->application-dev.yml
完结!!!