Nishuixingzhou - SpringBoot production options / development / test environment

Multi-choice environment

  • General set of procedures will be run in multiple different environments, such as development, test and production environments, the address of each database environments, server port through these are not the same, because if the environment changes and go change the configuration of parameters obviously unreasonable and could easily lead to errors

  • For different environments, it requires different profile, or perform a different class, and this selection may be selected in the main configuration file SpringBoot

Implementation of multiple configuration files

First, we define multiple profiles, of course, the file name must have complex rules: application-xxx.yml

  

Development environment configuration file as follows:

  

Production environment configuration file as follows:

  

Our main configuration file follows, we chose to read a production environment configuration file

  

Profile read reading choice, we have to do, let's do it using different implementation in different environments, finally made uniform test

We define an interface

  

Our aim is to think in a different environment, different implementations, so with the following two different implementations

  

  

  • Through the above label, we can know, on the implementation class by a comment @Profile ( ""), may indicate that the implementation class is under what circumstances the use of

  • Then we wrote a Controller, call them and returned to a String page

  • Our current SpringBoot choose to read the main configuration file is: application-prod.yml

  • Access path is: localhost: 9999 / test3 / stu

  

If we want to change a setting, only need to modify a master configuration file configuration, change it to test or dev

Now I will change it to dev, test

  

Can be found, the port has changed, blocking the way changed, said the service implementation calls also changed, which is under different environments, multi-environment of choice SpringBoot

Implementation using a single configuration file

But the configuration file, you can only use yml mode, select and configure the environment in application.yml as follows

More than three parts, acts as the equivalent of three profiles, --- divided in the middle, can be self-test

Guess you like

Origin www.cnblogs.com/msi-chen/p/10959559.html