配置文件格式

本文最后更新于:2024年3月18日 凌晨

配置文件格式

  • 配置文件是工程中常用的初始化参数的配置方式,而配置文件的格式有很多种,不同的操作系统,编程语言都会有不同的配置文件的格式,本文罗列了一些常见的配置文件的格式。
  • 不同的配置文件格式有不同的用户友好性,对于功能的支持也有简单和复杂之分,很难简单说那种配置文件是最好的,有时候需要从多个方面去考虑,比如Windows较早的开发喜欢使用int,java喜欢使用properties,通用的编程喜欢yaml,json等格式,本文也不会对这些格式进行排名,而是简单介绍一下这些格式,用户可以根据自己的实际情况进行选择。

ini

  • ini文件是一个无固定标准格式的配置文件,它以简单的文字与简单的结构组成,常常使用在Windows操作系统,ini文件的命名来源,是取自英文"初始(Initial)”的首字缩写,正与它的用途——初始化程序相应。
  • 文件格式比较简单,分为 ,参数,注释
1
2
3
4
5
6
7
8
9
10
11
12
13
[DriverConfig]
DriverCategory=PrintFax.Printer
DataFile=usb_host_based_sample.gpd

; Note: Please replace the GUID below when building a production driver.
PrinterDriverID={00000000-0000-0000-0000-000000000000}
Flags=HostBasedDevice
EventFile=usb_host_based_sample_events.xml
RequiredFiles=UNIRES.DLL,STDNAMES.GPD,MSXPSINC.GPD

[BidiFiles]
BidiUSBFile=usb_host_based_sample_extension.xml
BidiUSBJSFile=usb_host_based_sample.js

properties

  • properties是一种主要在Java相关技术中用来存储应用程序的可配置参数的文件的文件扩展名,它们也可以存储用于国际化和本地化的字符串,这种文件被称为属性资源包(Property Resource Bundles)
  • 每个参数被存储为一对字符串:一个存储名称参数(被称为"键”),另一个存储值。
  • 每个properties 文件中的行通常存储单个属性,对于每一行可能有这么几种格式,包括键=值,键 = 值,键:值,以及键值。
    .properties文件可以使用井号(#)或叹号(!)作为一行中第一个非空白字符来表示它后面的所有文本都是一个注释,反斜杠(\)用于转义字符。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection

# Connection url for the database "netgloo_blog"
spring.datasource.url = jdbc:mysql://localhost:3306/netgloo_blog?useSSL=false

# Username and password
spring.datasource.username = root
spring.datasource.password = root

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

Json

  • JSON(JavaScript Object Notation)是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读,尽管JSON是Javascript的一个子集,但JSON是独立于语言的文本格式,并且采用了类似于C语言家族的一些习惯。
  • JSON 数据格式与语言无关,脱胎于 JavaScript,但目前很多编程语言都支持 JSON 格式数据的生成和解析,所以也常常用作配置文件。
  • JSON用于描述数据结构,有以下形式存在。
    • 对象(object):一个对象以{开始,并以}结束,一个对象包含一系列非排序的名称/值对,每个名称/值对之间使用,分区。
    • 名称/值(collection):名称和值之间使用:隔开,一般的形式是:{name:value}
    • 值的有序列表(Array):一个或者多个值用,分区后,使用[,]括起来就形成了这样的列表。
    • 字符串:以""括起来的一串字符。
    • 数值:一系列0-9的数字组合,可以为负数或者小数,还可以用e或者E表示为指数形式。
    • 布尔值:表示为true或者false
  • JSON的格式描述可以参考RFC 4627
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"listener": [{
"tcp": {
"address": "127.0.0.1:443"
}
}],
"cluster_cipher_suites": "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
"storage": {
"consul": {
"foo": "bar",
"disable_clustering": "true"
}
},
"telemetry": {
"statsite_address": "baz"
},
"max_lease_ttl": "10h",
"default_lease_ttl": "10h",
"cluster_name":"testcluster",
"ui":true
}

xml

  • xml是一种标记语言,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等。
  • XML是从1995年开始有其雏形,并向W3C(万维网联盟)提案,而在1998年二月发布为W3C的标准(XML1.0)
  • XML设计用来传送及携带数据信息,所以也经常用来做配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:property-placeholder location="
${config.uri}/company.properties,
classpath:product.properties,
${config.uri}/${product.name}/${product.version}/common.properties,
${config.uri}/${product.name}/${product.version}/${config.stage}/stage.properties,
${config.uri}/${product.name}/${product.version}/${config.stage}/${config.node}/node.properties"
system-properties-mode="FALLBACK"/>

<context:annotation-config/>

<context:component-scan base-package="spring"/>

</beans>

yaml

  • YAML(/ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达数据序列的格式,Clark Evans在2001年首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者,目前已经有数种编程语言或脚本语言支持(或者说解析)这种语言。

基础语法

  • 空格不能省略。

  • 以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。

  • 属性和值的大小写都是十分敏感的。

  • 字面量直接写在后面就可以,字符串默认不用加上双引号或者单引号。

  • 注意

    • " 双引号:会转义字符串里面的特殊字符,特殊字符会作为本身想表示的意思,比如: name: "hello \n world" 输出:hello 换行 world

    • '单引号:不会转义特殊字符,特殊字符最终会变成和普通字符一样输出,比如:name: ‘hello \n world’输出:hello \n world

1
字面量:普通的值  [ 数字,布尔值,字符串  ]

数据类型

对象

1
2
3
4
5
6
student:
name: test
age: 3

# 行内写法
student: {name: test,age: 3}

数组(List,set )

  • -表示数组中的一个元素。
1
2
3
4
5
6
7
pets:
- cat
- dog
- pig

# 行内写法
pets: [cat,dog,pig]

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# my global config
global:
scrape_interval: 15s
evaluation_interval: 30s
# scrape_timeout is set to the global default (10s).

external_labels:
monitor: codelab
foo: bar

rule_files:
- "first.rules"
- "my/*.rules"

remote_write:
- url: http://remote1/push
write_relabel_configs:
- source_labels: [__name__]
regex: expensive.*
action: drop
- url: http://remote2/push

toml

  • TOML是一种旨在成为一个小规模,易于使用的语义化的配置文件格式,它被设计为可以无二义性的转换为一个Hash表。
    -"TOML”这个名字是"Tom’s Obvious, Minimal Language(汤姆的浅显的,极简的语言)”的首字母略写词,"Tom”指它的作者Tom Preston-Werner
  • TOML已在一些软件工程中使用,并且可在很多程序语言中执行。
  • TOML的语法广泛地由key = "value",[节名]#注释构成。
  • 它支持以下数据类型:字符串,整形,浮点型,布尔型,日期时间,数组和图表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# This is a TOML document.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

[servers]

# Indentation (tabs and/or spaces) is allowed but not required
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"

[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]

HOCON

  • HOCON,全称Human-Optimized Config Object Notation(人性化配置对象表示法)是一种人类可读的数据格式,并是JSON和properties的一个超集,它由Lightbend(用Scala开发的人都知道)开发,主要与Play框架结合使用,它也在Puppet中作为配置格式使用。
  • 基本上也算是Scala官方开发,所以在Scala的一些项目中得到使用,由于它是JSON和properties格式的超集,所以它的格式比较灵活。
  • 格式定义可以参考官方文档:HOCON
1
2
3
4
5
6
7
8
9
10
11
12
13
play.http.secret.key = "changeme"

play.modules {
# Disable built-in i18n module
disabled += play.api.i18n.I18nModule

# Enable Hocon module
enabled += com.marcospereira.play.i18n.HoconI18nModule
}

play.i18n {
langs = [ "en" ]
}

plist

  • 在OS X的Cocoa,NeXTSTEP和GNUstep编程框架中,属性列表(Property List)文件是一种用来存储序列化后的对象的文件,属性列表文件的文件扩展名为.plist,因此通常被称为plist文件。
  • Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息,该功能在旧式的Mac OS中是由资源分支提供的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
https://github.com/wahlmanj/com.plex.pms.plist/blob/master/com.plex.pms.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.plex.pms</string>
<key>KeepAlive</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Applications/Plex\ Media\ Server.app/Contents/MacOS/Plex\ Media\ Server</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>__USERNAME__</string>
<key>WorkingDirectory</key>
<string>/Applications</string>
<key>ServiceDescription</key>
<string>PMS</string>
</dict>
</plist>

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!