"Springcloud micro-service Combat Learning Series" configuration detailed

Source: Internet
Author: User
Tags readable

Objective

Spring Boot provides a series of automated configurations for common development scenarios to reduce the amount of templated configuration content that is inherently complex and rarely changed.

One, configuration file

The default configuration file location for Spring boot is src/main.resources/application.properties. The configuration of the spring boot application can be centralized in this file, according to our introduction of the less starter module, we extracted here to define the container port number, database connection information, log level and other configuration information. Eg: we need to customize the service port number of the Web module to add server.port=8888 to the application.properties to specify that the service ports are 8888, or you can use the Spring.application.name= Hello to specify the name of the app (the name will be registered as the service name in the subsequent spring cloud)

The Spring boot configuration file supports Yaml files that are now widely recommended in addition to the traditional properties files.

Yaml is a highly readable format used to express data sequences. Yaml references a variety of other languages, including C, Python, Perl, and the data format of the XML e-mail to get two of its features using white space symbol indentation and a large number of dependent appearances, particularly suitable for expressing or editing data structures, various settings documents, file outlines.

YAML uses a configuration format that is not represented by a simple key-value pair as the configuration of properties, but rather as an indented form of a similar outline:

1 environments: 2     Dev:3         url:http://dev.bar.com4         name=Developer Setup5    prod:6         url:http://foo.bar.com 7         Name:my Cool App

Equivalent to

1 environments.dev.url=http://dev.bar.com2 environments.dev.name=  Developer Setup3 environments.prod.url=http://foo.bar.com4 Environments.prod.name=my Cool App

You can see how the YAML is configured to be clearly readable, while configuring the amount of content characters to decrease. In addition, YAML can define multiple different environment configurations in a single file by using the Spring.profiles property. For example, the following indicates that Server.port will use port 8882 when specified as the test environment, Server.port will use 8883 ports in the PROD environment, and if no environment is specified, Server.port will use 8881 ports.

  1  server:   2  port:8881 3 --- 4  spring:   5   profiles:test   6  server:
     7  port:8882 8 --- 9  spring:  10   Profiles:prod  11  server:< /span>12  port:888313  

Note: YAML cannot load the configuration with @propertysource annotations, but Yaml loads the properties into memory as ordered, so Yaml's configuration is more advantageous than the properties configuration file when the information in the configuration file needs to have sequential meanings.

Ii.. Custom Parameters

In addition to setting the predefined configuration properties in each starter module in the Spring boot configuration file, you can also define some of the custom properties that we need in the configuration file. For example, adding a application.properties

1 book.name=springcloudinaction2 BOOK.AUTHOR=SLP

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

1 @Component 2  Public class book{3     @Value ("${book.name}")4     private  String name; 5     @Value ("${book.author}")6     private  String author; 7     // omit getter and setter 8 }

@Value annotations can be configured to support two expressions when loading property values

1, placeholder mode, the format is ${...},{} not placeholder

2, use Spel expression, format is #{...},{} for Spel expression

Third, parameter reference

The various parameters in the application.properties can be applied directly using the placeholder method, such as:

1 book.name=springcloud2 book.author=Slp3 Book.desc=${book.author} is Read <${book.name}>
Iv. using random numbers

In some special cases, we want some parameters to be loaded each time not a fixed value, such as secret key, service port, etc. In the Spring boot configuration file, you can generate a random int value, a Long value, or string string by using the ${random} configuration. This does not have to be coded to implement these logic.

1 #随机字符串2Com.slp.blog.value=${random.value}3 #随机int4Com.slp.blog.number=${random.int}5 #随机long6Com.slp.blog.bignumber=${random.Long}7 #10以内的随机数8Com.slp.blog.test1=${random.int(10)}9#10-random number of 20TenCom.slp.blog.test2=${random.int[10,20]}

"Springcloud micro-service Combat Learning Series" 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.