There are some common fixed basic configurations in the infrastructure module. For example, including but not limited to log configuration, Spring core configuration and MyBatis Plus related common configuration. These configurations are often irrelevant to the environment. How can they be reused?
# 日志配置 logging: level: # 记得配置到包名 com.agileboot: debug org.springframework: info pattern: console: "%date %thread %green(%level) [%cyan(%logger{10}):%magenta(%line)] %red(%method) | %msg%n" # Spring配置 spring: # 文件上传 servlet: multipart: # 单个文件大小 max-file-size: 10MB # 设置总上传的文件大小 max-request-size: 20MB mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER jackson: deserialization: fail-on-unknown-properties: false serialization: write-dates-as-timestamps: false date-format: yyyy-MM-dd HH:mm:ss
There are also some environment-independent configurations in the infrastructure module. For example, Mysql, Redis or MQ. How to distinguish between environments?
# 数据源配置 spring: # redis 配置 redis: # 地址 host: localhost # 端口,默认为6379 port: 36379 # 数据库索引 database: 0 # 密码 password: 12345 # 连接超时时间 timeout: 10s lettuce: pool: # 连接池中的最小空闲连接 min-idle: 0 # 连接池中的最大空闲连接 max-idle: 8 # 连接池的最大数据库连接数 max-active: 8 # #连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1ms
We generally configure log-related configurations in the logback.xml file. How to implement different log paths in different environments?
Reuse the fixed configuration in the infrastructure module.
Distinguish the configuration of multiple environments.
logback log introduces multi-environment configuration.
We can define an application.yml file for the Admin module. The module-specific configuration is described in the file. For example, port, access path, etc.
# 开发环境配置 server: # 服务器的HTTP端口,默认为8080 port: 8080 servlet: # 应用的访问路径 context-path: / tomcat: # tomcat的URI编码 uri-encoding: UTF-8 # 连接数满后的排队数,默认为100 accept-count: 1000 threads: # tomcat最大线程数,默认为200 max: 800 # Tomcat启动初始化的线程数,默认值10 min-spare: 100 # Spring配置 如果需要无Mysql 无Redis直接启动的话 dev改为test # 生产环境把dev改为prod spring: profiles: active: basic,dev
Create a new
applicaiton-basic.yml in the infrastructure module and configure some basic fixed reuse configurations that are independent of the environment.
application-dev.yml, configure some environment-related configurations, such as database configuration.
Then specify the basic and dev files in the spring.profiles.active configuration.
At this time springboot will automatically find the applicaiton-basic.yml and application-dev.yml files.
So our final configuration file is the configuration superimposed by applicaiton.yml applicaiton-basic.yml applicaiton-dev.yml.
We can create a new logback-spring.xml file to configure logs for multiple environments. Suppose we want to configure different log paths depending on the environment. In files such as application-dev.yml, you can configure logging.file.path.
Then use the springProperty tag in the logback-spring.xml file to read the path configured in the multi-environment yml.
At this time, the path of the log will read the paths configured in different environments respectively.
The above is the detailed content of What is the method of multi-level and multi-environment yml design in SpringBoot project?. For more information, please follow other related articles on the PHP Chinese website!