目录:
- yml与properties区别
- bootstrap与application的区别
一. yml与properties
其实yml和properties文件是一样的原理,且一个项目上要么yml或者properties,二选一的存在。
推荐使用yml,更简洁。
二. bootstrap与application
application.yml 和bootStrap.yml 在同一目录下,则bootStrap.yml 的加载顺序要高于application.yml,即bootStrap.yml 会优先被加载。
1. 加载顺序
这里主要是说明application和bootstrap的加载顺序。
- bootstrap.yml(bootstrap.properties)先加载,bootstrap.yml 用于应用程序上下文的引导阶段,bootstrap.yml 由父Spring ApplicationContext加载。
可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。
在bootstrap文件里面可以配置包括tomcat的端口和应用的名称spring.name等 - application.yml(application.properties)后加载
可以用来定义应用级别的,用户级的资源配置项
2. 配置区别
bootstrap.yml 和application.yml 都可以用来配置参数。
- bootstrap.yml 可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。
- application.yml 可以用来定义应用级别的,如果搭配 spring-cloud-config 使用 application.yml 里面定义的文件可以实现动态替换。
3. 示例
使用naocs注册中心时,应在 bootstrap.yml 中指定:
bootstrap.properties
spring.profiles.active=test
bootstrap-test.properties
#spring.profiles.active=test bootstrap.properties中已经有了
#examples-dev.yml = {spring.application.name}-{spring.profiles.active}.{spring.cloud.nacos.config.file-extension} 此文件的为:oauth2-test.yml
spring.application.name=oauth2
spring.cloud.nacos.config.file-extension=yml
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.cloud.nacos.config.server-addr=http://localhost:8848
spring.cloud.nacos.config.namespace=af908ff6-ab87-43c5-8c72-123a3c7751e3
spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[0].data-id=application-test.yml
spring.cloud.nacos.config.extension-configs[1].data-id=application-redis.properties
spring.cloud.nacos.config.extension-configs[2].data-id=application-activemq.properties
spring.cloud.nacos.config.extension-configs[3].data-id=oauth2-config.properties
spring.cloud.nacos.config.extension-configs[3].refresh=true
oauth2-test.yml
server:
port: 50002
logging:
level:
cn.rntd.oauth2.mapper : debug
spring:
shardingsphere:
datasource:
names: ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
username: rntd_cs
password: rntd123qwe
jdbc-url: jdbc:mysql://39.104.137.81:3306/iiot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
url: jdbc:mysql://39.104.137.81:3306/iiot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
filters: stat,wall,log4j
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000
tempVar: 66666666
ident:
address: https://dev-agent.fuxingfuli.com/#/login?
vrk:
host: http://127.0.0.1:8088
seata:
enabled: true
application-id: ${spring.application.name}
enable-auto-data-source-proxy: false
tx-service-group: iiot-seata-group
service:
vgroup-mapping:
iiot-seata-group: default #key与上面的tx-service-group的值对应
application-test.yml
server:
port: 50002
logging:
level:
cn.rntd.oauth2.mapper : debug
spring:
shardingsphere:
datasource:
names: ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
username: rntd_cs
password: rntd123qwe
jdbc-url: jdbc:mysql://39.104.137.81:3306/iiot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
url: jdbc:mysql://39.104.137.81:3306/iiot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
filters: stat,wall,log4j
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
connection-properties: druid.stat.merggSql=ture;druid.stat.slowSqlMillis=5000
tempVar: 66666666
ident:
address: https://dev-agent.fuxingfuli.com/#/login?
vrk:
host: http://127.0.0.1:8088
seata:
enabled: true
application-id: ${spring.application.name}
enable-auto-data-source-proxy: false
tx-service-group: iiot-seata-group
service:
vgroup-mapping:
iiot-seata-group: default #key与上面的tx-service-group的值对应