1. Introduction
1.绪论
YAML is a human-friendly notation used in configuration files. Why would we prefer this data serialization over the properties file in Spring Boot? Besides readability and reduction of repetition, YAML is the perfect language to write Configuration as Code for the deployments.
YAML是一种用于配置文件的人性化符号。为什么我们喜欢这种数据序列化而不是Spring Boot中的属性文件?除了可读性和减少重复之外,YAML还是为部署工作编写 “配置为代码 “的完美语言。
In the same way, the use of YAML for Spring DevOps facilitates the storage of the configuration variables in the environment as the 12 Factor Authenticator recommends.
同样,在Spring DevOps中使用YAML便于在环境中存储配置变量,正如12因素认证器所建议的那样。
In this tutorial, we’ll compare Spring YAML versus properties file in order to check the main advantages of using one over the other. But remember, the selection of YAML over properties file configuration is sometimes a decision of personal taste.
在本教程中,我们将比较Spring YAML和属性文件,以检查使用其中一个的主要优势。但请记住,选择YAML而不是属性文件的配置,有时是个人品味的决定。
2. YAML Notation
2.YAML 符号
YAML stands for a recursive acronym for “YAML Ain’t Markup Language“. It provides the following characteristics:
YAML 是”YAML Ain’t Markup Language“的递归首字母缩写。它提供了以下特点。
- More clarity and human-friendliness
- Perfect for hierarchical configuration data
- It supports enhance capabilities such as maps, lists, and scalar types
Those capabilities make YAML the perfect companion for Spring configuration files. A word of caution here for those starting out with YAML: writing it can be a bit tedious at the beginning due to its indentation rules.
这些功能使YAML成为Spring配置文件的完美伴侣。 这里要提醒那些刚开始使用YAML的人:由于YAML的缩进规则,编写YAML在开始时可能会有点乏味。
Let’s see how it works!
让我们看看它是如何工作的!
3. Spring YAML Configuration
3.Spring YAML配置
As it was mentioned in the previous sections, YAML is an extraordinary data format for configuration files. It’s much more readable, and it provides enhanced capabilities over the properties file. Therefore, it makes sense to recommend this notation over the properties file configuration. Furthermore, from version 1.2, YAML is a superset of JSON.
正如前几节所提到的,YAML是一种特殊的配置文件的数据格式。它的可读性更强,而且它提供了比属性文件更强的功能。因此,推荐这种符号而不是属性文件的配置是有意义的。此外,从1.2版本开始,YAML是JSON的超集。
In addition, in Spring the configuration files placed outside the artifact override those inside the packaged jar. Another interesting feature of Spring configuration is the possibility to assign environment variables at runtime. This is extremely important for DevOps deployments.
此外,在Spring中,放置在工件之外的配置文件会覆盖打包的jar中的文件。Spring配置的另一个有趣的功能是在运行时分配环境变量的可能性。这对于DevOps部署来说是非常重要的。
Spring profiles allow separating the environments and apply different properties to them. YAML adds the possibility to include several profiles in the same file.
Spring配置文件允许分离环境并对其应用不同的属性。YAML 增加了在同一文件中包含多个配置文件的可能性。
Note: this feature is also supported for properties files with Spring Boot 2.4.0.
注意:Spring Boot 2.4.0的属性文件也支持这一功能。
In our case, for deployment purposes, we’ll have three: testing, development, and production:
在我们的案例中,为了部署目的,我们将有三个:测试、开发和生产。
spring:
profiles:
active:
- test
---
spring:
config:
activate:
on-profile: test
name: test-YAML
environment: testing
servers:
- www.abc.test.com
- www.xyz.test.com
---
spring:
config:
activate:
on-profile: prod
name: prod-YAML
environment: production
servers:
- www.abc.com
- www.xyz.com
---
spring:
config:
activate:
on-profile: dev
name: ${DEV_NAME:dev-YAML}
environment: development
servers:
- www.abc.dev.com
- www.xyz.dev.com
Note: if we use a Spring Boot version prior to 2.4.0, we should use the spring.profiles property instead of the spring.config.activate.on-profile we used here.
注意:如果我们使用2.4.0之前的Spring Boot版本,我们应该使用spring.profiles属性,而不是我们在这里使用的spring.config.activate.on-profile。
Let’s now check the spring.profiles.active property which assigns the test environment by default. We can redeploy the artifact using different profiles without building again the source code.
现在让我们检查一下spring.profiles.active属性,它默认分配了测试环境。我们可以使用不同的配置文件重新部署工件,而无需重新构建源代码。
Another interesting feature in Spring is that you can enable the profile via the environment variable:
Spring中另一个有趣的功能是,你可以通过环境变量启用配置文件。
export SPRING_PROFILES_ACTIVE=dev
We’ll see the relevance of this environment variable in the Testing section. Finally, we can configure YAML properties assigning directly the value from the environment:
我们将在测试部分看到这个环境变量的相关性。最后,我们可以配置YAML属性,直接从环境中赋值。
name: ${DEV_NAME:dev-YAML}
We can see that if no environment variable is configured, a default value dev-YAML is used.
我们可以看到,如果没有配置环境变量,就会使用默认值dev-YAML。
4. Reduction of Repetition and Readability
4.减少重复性和可读性
The hierarchical structure of YAML provides ways of reducing the upper levels of the configuration properties file. Let’s see the differences with an example:
YAML的分层结构提供了减少配置属性文件的上层的方法。让我们通过一个例子来看看其中的区别。
component:
idm:
url: myurl
user: user
password: password
description: >
this should be a long
description
service:
url: myurlservice
token: token
description: >
this should be another long
description
The same configuration would become redundant using properties file:
同样的配置使用属性文件会变得多余。
component.idm.url=myurl
component.idm.user=user
component.idm.password=password
component.idm.description=this should be a long \
description
component.service.url=myurlservice
component.service.token=token
component.service.description=this should be another long \
description
The hierarchical nature of YAML greatly enhances legibility. It is not only a question of avoiding repetitions but also the indentation, well used, perfectly describes what the configuration is about and what is for. With YAML, as in the case of properties file with a backslash \, it is possible to break the content into multiple lines with > character.
YAML的层次性大大增强了可读性。这不仅是一个避免重复的问题,而且缩进也用得好,完美地描述了配置的内容和用途。使用YAML,就像使用反斜杠的属性文件一样,可以用>字符将内容分成多行。
5. Lists and Maps
5.名单和地图
We can configure lists and maps using YAML and properties file.
我们可以使用YAML和properties文件来配置列表和地图。
There are two ways to assign values and store them in a list:
有两种方法来赋值并将其存储在一个列表中。
servers:
- www.abc.test.com
- www.xyz.test.com
external: [www.abc.test.com, www.xyz.test.com]
Both examples provide the same result. The equivalent configuration using properties file would be more difficult to read:
两个例子都提供了相同的结果。使用属性文件的同等配置会更难阅读。
servers[0]=www.abc.test.com
servers[1]=www.xyz.test.com
external=www.abc.test.com, www.xyz.test.com
Again YAML version is more human-readable and clear.
同样YAML版本更适合人类阅读,也更清晰。
In the same way, we can configure maps:
以同样的方式,我们可以配置地图。
map:
firstkey: key1
secondkey: key2
6. Testing
6.测试
Now, let’s check if everything is working as expected. If we check the logging of the application, we can see that the environment selected by default is testing:
现在,让我们检查一下一切是否按预期工作。如果我们检查应用程序的日志,我们可以看到,默认选择的环境是测试。
2020-06-11 13:58:28.846 INFO 10720 --- [main] com.baeldung.yaml.MyApplication: ...
using environment:testing
name:test-YAML
servers:[www.abc.test.com, www.xyz.test.com]
external:[www.abc.test.com, www.xyz.test.com]
map:{firstkey=key1, secondkey=key2}
Idm:
Url: myurl
User: user
Password: password
Description: this should be a long description
Service:
Url: myurlservice
Token: token
Description: this should be another long description
We can overwrite the name by configuring DEV_NAME in the environment:
我们可以通过在环境中配置DEV_NAME来覆盖这个名字。
export DEV_NAME=new-dev-YAML
We can see that the name of the environment changes executing the application with dev profile:
我们可以看到,环境的名称在执行dev profile的应用程序时发生了变化。
2020-06-11 17:00:45.459 INFO 19636 --- [main] com.baeldung.yaml.MyApplication: ...
using environment:development
name:new-dev-YAML
servers:[www.abc.dev.com, www.xyz.dev.com]
Let’s run for the production environment using SPRING_PROFILES_ACTIVE=prod:
让我们使用SPRING_PROFILES_ACTIVE=prod为生产环境运行。
export SPRING_PROFILES_ACTIVE=prod
2020-06-11 17:03:33.074 INFO 20716 --- [main] ...
using environment:production
name:prod-YAML
servers:[www.abc.com, www.xyz.com]
7. Conclusion
7.结语
In this tutorial, we described the intricacies of the use of YAML configuration compared to the properties file.
在本教程中,我们描述了与属性文件相比,YAML配置使用的错综复杂性。
We showed that YAML provides human friendliness capabilities, it reduces repetition and is more concise than its properties file variant.
我们表明,YAML提供了人类友好的能力,它减少了重复,并且比其属性文件的变体更简洁。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。