search
HomeJavajavaTutorialWhat is the method of multi-level and multi-environment yml design in SpringBoot project?

Requirement scenario

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?

What you want to achieve

  • Reuse the fixed configuration in the infrastructure module.

  • Distinguish the configuration of multiple environments.

  • logback log introduces multi-environment configuration.

Implementation

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.

What is the method of multi-level and multi-environment yml design in SpringBoot project?

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.

What is the method of multi-level and multi-environment yml design in SpringBoot project?

Then use the springProperty tag in the logback-spring.xml file to read the path configured in the multi-environment yml.

What is the method of multi-level and multi-environment yml design in SpringBoot project?

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft