Original: Taste of Little Sister (wechat official ID: XjjDog), welcome to share, please reserve the source.

To my shame, the configuration file world is dominated by yaml, TOMl, and JSON, which makes javaer, which has always used properties files, very sad.

Don’t worry, when you get to the end of the article, you’ll be burying your head in shame, too.

Like various artificial intelligence tuning parameters, K8S tuner has been successfully upgraded to YML configuration master. As a long-time user of the SpringBoot framework with YML files, it’s an added shame that you can sometimes be confused about the representation of YML.

YAML, it turns out, is a subset of XML, so its complexity has its roots, having been around since 2009.

The first problem you encounter with yML files is their indentation. Just as in Python, the presentation hierarchy of YML files is accomplished by nested indentation. Instead of using tabs, it uses Spaces for indentation.

The trick is, it doesn’t matter how many Spaces there are, as long as the left side of elements of the same level is aligned. This is a nightmare for the CV party.

What problems does a configuration file solve? Redis has answered. Just like when you learn a new language, you can solve its string and collection representation and basically write code. Let’s take a look at them one by one.

The following methods are based on the YML file format of SpringBoot. Parsers in other scenarios may vary slightly. To debug these values, we simply write a test class, then set the values and print the following.

@EnableAutoConfiguration
@Configuration
public class TestConfig implements InitializingBean {
    @Value("${str1}")
    String str1;
    @Override
    public void afterPropertiesSet(a) throws Exception {
        System.out.println(this); }}Copy the code

1. The string

Strings are the simplest configuration and the most common. In Spring, strings can be quoted with or without quotes. So the configuration of the next three lines is the same.

str1: ksdfjsdlkfjdsf skdfljs
str1: 'ksdfjsdlkfjdsf skdfljs'
str1: "ksdfjsdlkfjdsf skdfljs"
Copy the code

So how do you support multiple lines of text? Some needs, after all, are dead. It is written as follows:

str1: | ksdfjsdlkfjdsf skdfljs ksdfjsdlkfjdsf skdfljs ksdfjsdlkfjdsf skdfljsCopy the code

Note that there is no need for any additional closing notation, everything is proved by indentation. Of course, you can also change | to >, effect is the same.

str1: > ksdfjsdlkfjdsf skdfljs ksdfjsdlkfjdsf skdfljs ksdfjsdlkfjdsf skdfljsCopy the code

And, crucially, there’s a third way to write it.

str1: "ksdfjsdlkfjdsf skdfljs ksdfjsdlkfjdsf skdfljs ksdfjsdlkfjdsf skdfljs"
Copy the code

2. Digital

When our receiver is a number, like the one below.

@Value("${a}")
int a ;
Copy the code

So, even if you write the configuration file as a string, it will force the conversion to a number.

a: "014"
Copy the code

At that point, the value of A will be set to the integer 14.

And the amazing thing is, if you take the quotes out, it looks like this.

a: 014
Copy the code

At this point, the value of A actually becomes 12!

I have encountered such a terrible bug, wasting a lot of brain cells, WTF. Because starting with 0 means octal, there is a layer of conversion in the middle of the parser. So, according to this logic, 0x14 is 20, which must be used with care. Clever students can use it to bury the pit.

There are also some special ways to write it.

float: 1.23 e+3     # floating point number
fixed: 13.67       # fixed decimal
minmin: -.inf      Minus infinity
notNumber: .NaN    # invalid number

boolean: [true.false] # Boolean value
string: '12345'    # string

date: 2021-06-03   # date
Copy the code

3. The dictionary

Let’s look at a common dictionary. In fact, listing all the configurations is itself a dictionary, that is, KV configuration.

It is split with:, so the left half requires no special characters, otherwise it would be confusing. No, no, no, no, no, no, it’s not sick, because it’s picking out the characters right. Such as the following YML configuration.

a&& xk@71: 0x14
Copy the code

Such code receives.

@Value("${a&& xk@71}")
int a ;
Copy the code

Yeah, easy to beat to death. So, you know.

Or am I being naive? The YML file doesn’t even say that key can’t have special characters, it allows you to do that.

4. The object

From dictionaries, it is easy to extend to objects. Because an object is also a collection of properties. Json has shown that these properties are a bunch of KV, and so is our YAML.

Suppose we have the following code, where we need to construct the data in dog.

@Data
public static class Dog{
    private String xjjdog1;
    private String xjjdog2;
}

@Bean
@ConfigurationProperties(prefix = "dog")
public Dog getDog(a){
    return new Dog();
}
Copy the code

So the first way to write yML is like this.

dog:
  xjjdog1: i am xjjdog1
  xjjdog2: i am xjjdog1++
Copy the code

Alternatively, you can write the JSON data directly to a file.

dog: {xjjdog1: 'i am xjjdog1'.xjjdog2: 'i am xjjdog++'}
Copy the code

Of course, multiple layers can be laid out in a row. For example, if prefix is super.dog, the yML file could be written like this.

super.dog: {xjjdog1: 'i am xjjdog1'.xjjdog2: 'i am xjjdog++'}
Copy the code

5. List support

A list, which is a list, we can use an array to receive, we can use a list, etc.

It can also be written two ways. This is the most common type.

animal:
  - dog
  - cat
  - monkey
Copy the code

Of course, you can put it on one line.

animal: [dog.cat.monkey]
Copy the code

This is fine, the point is that YML files support nesting. For example, a Map inside a List, or a List inside a Map. When the nesting level is deep, or the indentation is irregular, it can be very messy.

Take the following POD configuration for the K8S.

apiVersion: v1
kind: Pod
metadata:
  name: xjjdog-Pod
  labels:
    app: front-web
spec:
  containers:
    - name: front-web
      image: nginx
      ports:
        - containerPort: 80
    - name: front-app
      image: xjjdog/frontapp
      ports:
        - containerPort: 14000
  storages:
  .
Copy the code

The more complex is spec, which has containers, Storages, and other configurations. Containers is a list, and between the lists is a map, in which the ports properties are a list… Such nesting, if the configuration file is long, unfamiliar with the business attributes of students will be easily confused.

6. Special data

Even so, YAML is much simpler than XML. It can also be written in many special ways.

Like this one.

str1: !!!!! str 2021-06-03
Copy the code

It means that you force 2021-06-04 into a string. There are many such forcings, but most of the time you won’t use them. But if you want to make your YAML files so complicated that others won’t touch them, that’s fine.

 !!!!! int               Integer type
 !!!!! float             Float type
 !!!!! bool              # Boolean type
 !!!!! str               # string type
 !!!!! binary            # is also a string
 !!!!! timestamp         # datetime type
 !!!!! null              # a null value
 !!!!! set               # set
 !!!!! omap, !!!!! pairs     # keyvalue list or object list
 !!!!! seq               # sequence, also a list
 !!!!! map               # key value table
Copy the code

Since there are so many complicated ways to write YML files, we can play with them. For example, the following way.

from: &d !!!!! str 2021-06-04
str1: *d
Copy the code

This configuration, which is the same as the one above, ampersand stands for tag, and we’ve given it a name, d; * means quote, we just quote a handful where we need it.

Keys in YML can also use objects or complex structures as keys. In order to identify a special key, we need to do a little processing.

? [blue, reg, green]: Color
Copy the code

This configuration up here? That is to say, I am going to perform a more complex configuration. Are you ready?

7. End

After learning these tricks, are you itching to try them? Want to do something fun with your SpringBoot project? So that your infrastructure department can’t scan your configuration, why not?

This is the configuration file of a common datasource that I modified.

h2: &sa !!!!! str sa
driver: &driver !!!!! str org.h2.Driver
defaults: &defaults
  ? username: *sa
  ? password:
  ? driverClassName: *driver
spring:
  datasource:
    < < : *defaults
    ? url: !!!!! str > jdbc:h2:mem:h2test; DB_CLOSE_DELAY=-1; DB_CLOSE_ON_EXIT=FALSECopy the code

Do you think it’s beautiful? My leg hurts anyway.

Xjjdog is a public account that doesn’t allow programmers to get sidetracked. Focus on infrastructure and Linux. Ten years architecture, ten billion daily flow, and you discuss the world of high concurrency, give you a different taste. My personal wechat xjjdog0, welcome to add friends, further communication.