Springboot Configuration Detailed

Source: Internet
Author: User

Springboot configuration detailed springboot automation configuration

In the previous section we implemented a simple RESTful API application using spring boot, which, in addition to some configuration of the Maven Pom file, did not make any other configuration, which was the benefit of Spring boot's automated configuration, but We also need to know how to modify the content of these automation configurations in spring boot to address some of the special scenario requirements.

Profile-spring boot supports YAML configuration files and properties profiles

The default configuration file location for Spring boot is in: Src/main/resources/application.properties. The configuration content about spring boot can be centralized in this file.

Spring boot profiles can use a very popular Yaml file in addition to the traditional properties file.

About YAML

Yaml is a highly readable format used to express data sequences.

YAML focuses on data rather than markup language.

Yaml is similar to other high-order languages, and can simply express lists, hash lists, scalars, and so on. It uses blank symbol indentation and a large number of dependent appearance features, especially suitable for expressing or editing data structures, various settings documents, file outlines.

Yaml can also represent data that is relevant.

The difference between YAML and properties

The configuration file format for YAML is:

Environments:

Dev

Url:http://dev.bar.com

Name:developer Setup

Prod

Url:http://foo.bar.com

Name:my Cool App

Properties that are equivalent to:

Environments.dev.url=http://dev.bar.com

Environments.dev.name=develop Setup

Environments.prod.url=http://foo.bar.com

Environments.prod.name=my Cool App

With the configuration of Yaml we can see that the configuration of characters is much less than properties, and the structure becomes clearer.

Yaml can also define multiple different environment configurations in a single file by using the Spring.profiles property. For example:

Server

port:8881

---

Spring

Profiles:test

Server

port:8882

----

Spring

Profiles:prod

Server

port:8883

When specified as the test environment, Server.port will use port 8882, while PROD environment, Server.port will use 8883 ports and 8881 ports when not specified.

Yaml loads the properties into memory in an orderly manner, so YAML is configured to have an advantage over the properties configuration file when the information in the configuration file needs to have sequential meanings.

Custom parameters

In addition to setting the predefined configuration properties in each starter module in the Spring boot configuration file, we can also define some custom properties that we need in the configuration file. For example, add in Application.properties:

Book.name=springcloudinaction

Book.author=zhaiyongchao

These custom parameters can then be loaded with @value annotations in the application, such as:

@Component

public class book{

@Value ("${book.name}")

private String name;

@Value ("${book.author}")

Private String author;

Omit getter and setter

}

Then the main class of spring boot indicates that the class is in the same package.

@Value annotations can be configured by supporting two expressions when loading properties:

    • One is the placeholder method described above, in the format ${...}, in curly braces for placeholder.
    • The other is to use the SPEL expression in the format #{...}, which is the spel expression in curly braces.
Parameter reference

Book.name=spring Cloud

Book.author=zhaiyongchao

Book.desc=${book.author} is writing "${book.name}"

In this way, the value of Book.desc is "Spring Cloud", Zhaiyongchao is writing.

Using random numbers

Use ${random.int} in the configuration file to get a random int type of data.

Name

Describe

${random.value}

Get random string

${random.int} Get random int type data
${random.long} Get Random Long data
${random.int (10)} Get a random number within 10
${RANDOM.INT[10,20]} Get the random number of 10~20
Command-line arguments

We use the Java-jar command to launch a spring boot application that, when started, can set parameters such as:

Java–jar Xxx.jar--server.port=8081

This command specifies the port number for the spring boot app to start as 8081, directly on the command line.

When you start the Spring boot application as a command line, continuous-is the identification of the attribute in the application.properties.

Multi-environment configuration

It may not be a good way to modify the application's configuration file by the command line above, when the application is particularly long. Therefore, a multi-environment configuration is required.

In spirng boot, the file name of the multi-environment configuration needs to meet the application-{profile}.properties format, for example:

    • Application-dev.properties Development environment
    • Application-test.properties test Environment
    • Application-prod.properties production Environment

Which configuration file is loaded and needs to be set in the Application.properties file by the String.profiles.active property, whose value corresponds to the {profile} value in the configuration file (you can also use the Java-jar The Xxx.jar--spring.profiles.active command specifies the configuration file that needs to be loaded. The contents of the Application-test.properties configuration file are loaded as spring.profiles.active=test.

Load order

Monitoring and management

a brief preface

in the service architecture, by splitting a large monolithic system into multiple applications that provide different services. While the internal logic of each application is simplified by decomposition, the complexity of the system is greatly increased by the number of deployed applications.

With the increasing application, the number of failures in the system cluster is more and more, although under the protection of the high availability mechanism, individual failures will not affect the external services of the system, but these frequent applause need to be discovered and processed in time to ensure the system is in a healthy and usable state. To achieve this, we need an automated monitoring and maintenance mechanism, which is capable of continuously collecting indicators for each micro-service application and developing monitoring and early warning rules based on these basic indicator information.

Initial Actuator

This module is introduced to automatically provide a series of endpoints for monitoring for applications built by spring boot. Spring Cloud has made a number of extensions to the module as it implements the various service components. For example, more metrics and metrics are added to the native endpoint.

Introduction of Actuator
    1. Spring-boot-starter-actuator dependencies are introduced in the Pom.xml file of the existing spring boot application.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. Restart the app after you increase the dependency. You can see the following in the console at this point:

Displays a batch of endpoint definitions that are not defined in the program, but are automatically created by the Spring-boot-starter-actuator module to monitor and manage endpoints based on application dependencies and configurations. Endpoint specific also to no longer explain, you can Baidu itself.

Springboot Configuration Detailed

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.