Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (ii)

Source: Internet
Author: User
Tags server port

Setting property values from the command line

Believe that users who have used spring boot for a period of time must know this command: Java-jar Xxx.jar--server.port=8888, set the Xxx.jar app port to 8888 by using the –server.port property.

At the command line, two consecutive minus signs-the identification of the value of the property in Application.properties. So, the Java-jar xxx.jar--server.port=8888 command, which is equivalent to adding an attribute server.port=8888 in Application.properties, is visible in the sample project, The reader can verify by deleting the value or setting the value using the command line.

Modifying property values from the command line is a good convenience, but is it not safe to change the parameters of the application run from the command line? Yes, so spring boot also provides a nice way to mask the settings of the command-line Access property, which can be masked only by this setting: Springapplication.setaddcommandlineproperties (False).

Multi-environment configuration

When we develop spring boot applications, the same set of programs is usually applied and installed into several different environments, such as: development, testing, production, etc. Each environment's database address, server port and so on configuration will be different, if you are packaging for different environments to frequently modify the configuration file, it will be a very cumbersome and prone to error.

For a multi-environment configuration, the basic idea of a variety of project building tools or frameworks is consistent, and the Spring boot is no exception, or simpler, by configuring multiple profiles of different environments and then packaging them to specify what needs to be packaged.

The multi-Environment profile file name in spring boot needs to meet the application-{profile}.properties format, where {profile} corresponds to your environment ID, such as:

Application-dev.properties: Development environment
Application-test.properties: Test environment
Application-prod.properties: Production environment
As to which specific configuration file will be loaded, it needs to be set in the Application.properties file by the Spring.profiles.active property, whose value corresponds to the {profile} value.

such as: Spring.profiles.active=test will load the contents of the Application-test.properties configuration file

Below, a sample experiment is performed with different service ports configured in different environments.

Create different profiles for each environment application-dev.properties, application-test.properties, application-prod.properties

All three files are set with different server.port properties, such as: Dev environment set to 1111,test environment set to 2222,prod environment set to 3333

Set Spring.profiles.active=dev in Application.properties, which means that the dev environment is set by default

Test loading of different configurations

Performing Java-jar Xxx.jar, you can observe that the service port is set to 1111, which is also the default development environment (DEV)
Performing the Java-jar Xxx.jar--spring.profiles.active=test, you can observe that the service port is set to 2222, which is the configuration of the test environment (test)
Performing the Java-jar Xxx.jar--spring.profiles.active=prod, you can observe that the service port is set to 3333, which is the configuration of the production environment (PROD)
According to the above experiment, we can summarize the multi-environment configuration ideas as follows:

Configure common content in application.properties and set Spring.profiles.active=dev to the default configuration of the development environment
Configure different environments in Application-{profile}.properties
Activating configuration of different environments by command-line mode

Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.