Directory
Spring Cloud YAML Configuration detailed 1
Properties Detailed 2
Custom Parameter 2
Parameter Reference 3
Random number 4
External Join 4
Multi-Environment Configuration 5
YAML6
YAML has the following basic rules: 6
Using YAML to complete a multi-environment configuration 7
In the previous example, we used src/main/resources/application.properties to personalize our project, which is simple and straightforward, and is often used
However, in response to complex business requirements, multi-environment and programmatic configuration cannot be met
For this spring cloud provides us with the configuration of Yaml to enrich our capabilities and simplify our development while also providing a simple and straightforward way to differentiate configuration content.
Properties detailed
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 notes:
Instantiate the normal pojo into the spring container, equivalent to the <bean id= "" class= "/> in the configuration file
Modify Application.propertie to add the following configuration parameters
Dalao.name=mashibing
dalao.yanzhi=100
Using custom parameters
Spring Management Objects
To inject spring into the object, it is important to note that if your new object is not managed by the spring container, the attribute value is not automatically injected to us
@Autowired
Private Dalao Dalao;
Test print
System. out. println (tostringbuilder.reflectiontostring (Dalao));
System. out. println (tostringbuilder.reflectiontostring (new Dalao ()));
Tostringbuilder
Here we use the Tostringbuilder to complete the object to the string, to use this object, you need to introduce spring's toolkit Commons-lang3
Pom
<!--Https://mvnrepository.com/artifact/org.apache.commons/commons-lang3--
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
Parameter reference
Each parameter value in the Application.propertie can be referenced 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}
Entities are added
@Value ("${dalao.desc}")
Private String desc;
Output results
Random number
Some special needs, we do not want to set the value of the property is a fixed value, such as the server random port number, some number, etc., we can use ${radom} in the configuration to generate a random int,long or string
${random.int ()} = random int
${random.long} = Random Long
${random.int (50)} = random number within 50
${random.int (50,100)} = int random number between 50~100
${random.value}= Random string
Used in configuration files
Dalao.xiaodi.zhangyang.yanzhi=${random.int (50,100)}
Dalao.xiaodi.zhangyang.xinqing=${random.value}
External parameter entry
Automated operations deployment tools are often used in microservices architectures to launch our services using these tools
Our spring boot program typically uses Java–jar to start a running
For a service port number or some other value that needs to be determined when the service is started, if it is written dead in the configuration or is obviously not enough to meet the demand
We can replace a custom parameter with an external parameter
For example, temporarily determine the service port:
Java-jar Demo-0.0.1-snapshot.jar--server.port=60
Changes in the value of the Yan:
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, database connection password, custom attribute value and so on when the service starts
Multi-environment configuration
In real-world development, our set of code might be deployed to multiple servers in development, test, production, and so on, in each environment, such as a database password, and so on, although we can use external parameters to temporarily replace the value of the property when the service starts by automating operations deployment. But it also means higher operational costs.
We can use multiple sets of configurations to avoid modifying different configuration properties for different environments
How to use:
Create multiple sets of configuration files first
The naming rules are:
Application-*,properties
Application-dev,properties = Development Environment
application-test,properties= test Environment
application-prod,properties= Build Environment
Next we set the switch in the application.properties which sets the configuration to take effect
Using Spring.profiles.active=dev
When we start the service using Java–jar, we can change the entire configuration with external parameters.
Java-jar Demo-0.0.1-snapshot.jar--Spring.profiles.active=test
Yaml
Yaml is a foreign language abbreviation for "Yaml Ain ' t Markup Language Yaml is not a markup language"
But in order to emphasize that the language is centered on data, instead of focusing on the labeling language, it is renamed with a back-up word. It is an intuitive data serialization format that can be recognized by the computer, and is a programming language that is readable and easy to be read by humans, and easily interacts with the scripting language to express data sequences.
It is a data description language that is similar to a subset of standard generic Markup language XML, and syntax is much simpler than XML.
YAML has the following basic rules:
1, Case sensitive
2. Use indentation to represent hierarchical relationships
3, prohibit the use of tab indentation, you can only use the space key
4. There is no limit to the indentation length, as long as the element alignment indicates that the elements belong to a hierarchy.
5. Use # to indicate comments
6. Strings can be marked without quotation marks
Using YAML to complete a multi-environment configuration
Way One:
Single yml file with multiple propertys files
Way two:
Configure all variables within a single yml file