Directory
Spring Cloud YAML configuration detailed explanation 1
Properties detailed explanation 2
Custom parameters 2
Parameter reference 3
Random number 4
External parameters 4
Multiple environment configuration 5
YAML6
YAML has the following basic rules: 6
Use YAML to complete multi-environment configuration 7
In the previous example, we used src/main/resources/application.properties to personalize our project. This configuration method is simple and clear. It is also what we often use
But in response to complex business needs, multi-environment and programmatic configuration cannot be met
For this reason, Spring Cloud provides us with a YAML configuration method to enrich our Function and simplify our development while also being able to distinguish configuration content simply and clearly.
Detailed explanation of Properties
Custom parameters
Custom parameters allow us to define some parameters in the configuration file for Use in the program
Here we use Spring annotations to implement this function
First create an entity class
@Component
public class Dalao {
@Value("${dalao.name}")
private String name;
@Value("${dalao.yanzhi}")
private String yanzhi;
…..getter setter omitted
@Component Note:
Instantiate ordinary pojo into the spring container, which is equivalent to
in the configuration file Modify application.propertie and add the following configuration parameters
dalao.name=mashibing
dalao.yanzhi=100
Use custom parameters
Spring managed objects
Let Spring inject objects. What needs to be noted here is that if your new object is not managed by the Spring container, it will not automatically Inject the attribute value to us
@Autowired
private Dalao dalao;
Test printing
System .out.println(ToStringBuilder.reflectionToString(dalao));
System.out.println(ToStringBuilder.reflectionToString(new Dalao( )));
ToStringBuilder
ToStringBuilder is used here to convert the object into a string. To use this object, you need to introduce Spring’s toolkit commons-lang3
Pom
Parameter reference
The parameter values in application.propertie can refer to each other
Let’s modify the previous configuration
dalao.name=mashibing
dalao.yanzhi=100
dalao.desc=${dalao.name}is a good teacher, bing bu shi yin wei ${dalao.name} de yan zhi = ${dalao.yanzhi}
Add
@Value("${dalao. desc}")
private String desc;
Output result
Random number
For some special needs, we do not want the attribute value to be set to a fixed value, such as a random server port number, certain numbers, etc. We can use ${radom} to generate random int, long or string in the configuration
${random.int()} = random int
${random.long} = random long
${random.int(50)} = within 50 Random number
${random.int(50,100)} = int random number between 50~100
${random.value}= random string
Used in the configuration file
dalao.xiaodi.zhangyang.yanzhi=${random.int(50,100)}
dalao.xiaodi.zhangyang.xinqing=$ {random.value}
External input
Automatic operation and maintenance deployment tools are often used in microservice architecture. Use these tools to start our services
Our Spring boot program usually uses java –jar to start and run
For the service port number or some other values that need to be determined when starting the service, if Hard-coding or randomization in the configuration obviously cannot meet the needs
We can use external parameters to replace the customized parameters
For example, temporarily determine the service port:
java -jar demo-0.0.1-SNAPSHOT.jar --server.port=60
The appearance changes at the same time:
java -jar demo-0.0.1-SNAPSHOT.jar --server.port=60 --dalao.xiaodi.zhangyang.yanzhi
Using external configuration allows us to change the service port and database connection password when the service is started. , custom attribute values, etc.
Multiple environment configuration
In actual development, our set of codes may be deployed to multiple servers such as development, testing, and production at the same time. Personalized configurations such as database passwords in each environment are unavoidable, although we can External parameters are used to temporarily replace attribute values when the service is started through automated operation and maintenance deployment, but this also means increased operation and maintenance costs.
We can use multiple sets of configurations to avoid modifying different configuration properties for different environments
Usage:
First create multiple sets of configuration files
The naming rules are:
Application-*,properties
Application-dev,properties = development environment
Application-test,properties=test environment
Application-prod,properties=Generation environment
Next we set the switch in application.properties which set of configuration takes effect
Use spring.profiles.active=dev
When using java –jar to start the service, we can change the entire configuration through external parameters
java -jar demo-0.0.1-SNAPSHOT.jar -- spring.profiles.active= test
YAML
YAML is the foreign language abbreviation of "YAML Ain't Markup Language YAML is not a markup language"
But in order to emphasize this The language is centered on data, rather than focusing on markup language, and is renamed with back-to-basics words. It is an intuitive data serialization format that can be recognized by computers. It is a programming language that is highly readable and easy to be read by humans. It is easy to interact with script languages and is used to express data sequences.
It is a data description language similar to XML, a subset of Standard Universal Markup Language, and its syntax is much simpler than XML.
YAML has the following basic rules:
1. Case sensitive
2. Use indentation to indicate hierarchical relationships
3 , It is forbidden to use tab indentation, only the space key can be used
4. There is no limit on the indentation length. As long as the elements are aligned, it means that these elements belong to the same level.
5. Use # to indicate comments
6. Strings can be marked without quotation marks
Use YAML to complete multi-environment configuration
Method 1:
Single yml file with multiple properties files
Method 2:
Configure all variables in a single yml file
Related recommendations:
spring cloud tutorial config modification configuration method introduction
Spring, Spring MVC, MyBatis integration files Configuration details
The above is the detailed content of Basics of java architecture: Detailed explanation of Spring Cloud YAML and Properties configuration. For more information, please follow other related articles on the PHP Chinese website!