编程知识 cdmana.com

Change of grouping configuration before and after spring boot 2.4 and its influence on multi environment configuration structure

A few days ago 《Spring Boot 2.4 Support changes for multi environment configuration 》 In the article , I've explained it to you Spring Boot 2.4 Configuration changes of version to multi environment configuration . besides , There are some other configuration changes , So today we're going to talk about other updates !

spring.profiles.include For this configuration item , Are you familiar with ? The literal meaning is not difficult to understand , It should be the configuration used to introduce some other configuration ( Because there is a include Well ), The actual effect is exactly the same !

When our application has a lot of configuration information , For example, when a lot of middleware is used MySQL、Redis、MQ etc. , The configuration of each middleware is a large string , Well, at this time, in order to make the configuration more concise , It might be grouped .

If you've ever used this configuration , So it's upgrading 2.4 Be sure to pay attention to , Because the original configuration method will fail !

2.4 The previous grouping configuration

First look at it. 2.4 Group configuration before version , Let's use the following example to introduce :

spring:
  profiles:
    active: "dev"

---
spring.profiles: "dev"
spring.profiles.include: "dev-db,dev-mq"

---
spring.profiles: "dev-db"

db: dev-db.didispace.com

---
spring.profiles: "dev-mq"

mq: dev-mq.didispace.com

among :

  1. first spring.profiles.active: dev, Represents default activation dev To configure
  2. The second paragraph dev Configuration uses spring.profiles.include To introduce additional configuration information , Here's a simulation. One is dev Of db To configure , One is dev Of mq To configure . stay 2.3 And the previous version , This is how we usually configure different middleware groups .
spring.profiles: "dev"
spring.profiles.include: "dev-db,dev-mq"

At the end of the paper, we provide a sample project , You can modify it spring boot Version to 2.3 And configuration information using the above example , To start the application and see how this configuration works . No accident , You can start with the startup log , See the active configuration information as follows :

The following profiles are active: dev,dev-db,dev-mq

It also activates dev、dev-db、dev-mq Three group configuration ,include Is effective .

2.4 Group configuration of

The upgrade spring boot To 2.4 after , Restart the previous application , You'll find that the configuration doesn't work , It's not just because spring.profiles The cause of the failure , Even if we change it all to spring.config.activate.on-profile, And still can't activate dev-db and dev-mq Configuration of . Because in 2.4 After the version , We need to use spring.profiles.group To configure , At the same time, the organizational structure has changed .

Try to change the configuration to the following format :

spring:
  profiles:
    active: "dev"
    group:
      "dev": "dev-db,dev-mq"
      "prod": "prod-db,prod-mq"

---
spring:
  config:
    activate:
      on-profile: "dev-db"

db: dev-db.didispace.com

---
spring:
  config:
    activate:
      on-profile: "dev-mq"

mq: dev-mq.didispace.com

---
spring:
  config:
    activate:
      on-profile: "prod-db"

db: prod-db.didispace.com

---
spring:
  config:
    activate:
      on-profile: "prod-mq"

mq: prod-mq.didispace.com

Start the application to see the active configuration group at the beginning of the log ( The default activation is dev, So the corresponding is dev-db and dev-mq):

The following profiles are active: dev,dev-db,dev-mq

、 modify spring.profiles.active: "prod" Switch directly to another environment .

Restart the application to see the active configuration group at the beginning of the log :

The following profiles are active: prod,prod-db,prod-mq

You can see , stay 2.4 Version configuration , The configuration definitions for different environments are in the first section of the default configuration , All the environmental definitions have been transferred to spring.profiles.group Of key Field ( It's configured dev and prod),value The field represents the different configuration groups that each environment needs to load .

Recall that we were in 《Spring Boot 2.4 Support changes for multi environment configuration 》 Multi environment configuration mentioned in , Is the configuration ID of different environments defined in each spring.config.activate.on-profile in . And this time the configuration of the group has changed , Let's activate the configuration 、 The environment configuration is centralized in the default configuration , Other profile The definition is the environment + Configure the composition of the group .

For such an adjustment , The most intuitive feeling is when choosing the environment , I don't need to go down and find out what profile You'll know what options are available , It's more convenient . So what do you think of this configuration adjustment ? Feel free to leave a comment !

Code example

For an example of this article, see the following in the warehouse chapter1-3 Catalog :

If you think this article is good , welcome Star Support , Your concern is the driving force of my persistence !

版权声明
本文为[Procedure ape DD]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224214805965v.html

Scroll to Top