文章

toml配置文件

总结

Go 类型特征tag 出现的关键字生成的 TOML备注
非匿名 struct无 squash[xxx]字段继续往下写
匿名 struct,squash没有 [xxx],字段直接摊平到上层俗称“扁平化”
切片/数组无特殊关键字两种等价写法:
[[xxx]] 多次出现
xxx = [{…}, {…}]
① 易读;② 省行
map[K]V 且 K=string无特殊关键字[parent.xxx] 下再挂任意子键子键名就是 map 的 key
基本类型、字符串、数字、bool、duration任意key = value直接写
嵌套指针任意与值类型写法完全一样TOML 无指针概念

配置管理神器Viper

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
type Config struct 
	Spu                 SpuConfig                       `mapstructure:"spu"`
	Sdks                []SDKConf                        `mapstructure:"sdks"` //
	SpiHttpClients SpiHttpClientConfig `mapstructure:"spi_http_clients"`
	Person  //
	MockConfig `mapstructure:",squash"` //
	QueueConfig QueueConfig         `mapstructure:"queue_config"` 
}

type QueueConfig struct {
	Subscribers map[string]QueueSubConfig `mapstructure:"subscribers"`
	Publishers  map[string]QueuePubConfig `mapstructure:"publishers"`
}

type QueuePubConfig struct {
	QueueType              QueueType `mapstructure:"queue_type,omitempty" json:"queue_type,omitempty"`
	pubsub.QueuePubOptions `mapstructure:",squash"`
}

type QueueSubConfig struct {
	QueueType              QueueType `mapstructure:"queue_type,omitempty" json:"queue_type,omitempty"`
	pubsub.QueueSubOptions `mapstructure:",squash"`
}

type Person struct {
	Name            string     `mapstructure:"name"`
}

type MockConfig struct {
	Whitelist      map[string][]string `mapstructure:"whitelist"`      //
	ShopGoodsLimit int              `mapstructure:"shop_goods_limit"` 
}

type SpuConfig struct {
	DB            string     `mapstructure:"db"`
	OrmConfig       orm.Config `mapstructure:"orm_config"`
}

type SpiHttpClientConfig struct {
	Clients []Client `mapstructure:"clients"`   
}

type Client struct {
	Name    string `mapstructure:"name"`
	Address string `mapstructure:"address"`
	Timeout int    `mapstructure:"timeout"`
}

type SDKConf struct {
	Platform        platform.TPlatform  `mapstructure:"platform" json:"platform" toml:"platform"`
	SdkType         TSdkType            `mapstructure:"sdk_type" json:"sdk_type" toml:"sdk_type"`
	DataConsistency TSdkDataConsistency `mapstructure:"data_consistency" json:"data_consistency" toml:"data_consistency"`
	Url             string              `mapstructure:"url" json:"url" toml:"url"`
	GoodsSiteProxy  string              `mapstructure:"goods_site_proxy" json:"goods_site_proxy" toml:"goods_site_proxy"`
	GoodsTypes      TGoodsTypes         `mapstructure:"goods_types" json:"goods_types" toml:"goods_types"` 
} 

写法1

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 1. Person 段(没有 squash,所以必须独立一层)
[person]
name = "demo-instance"

# 2. MockConfig 被 squash 到顶层,直接写   //map
[whitelist]      
  tb = ["shop1", "shop2"]
  jd = ["shop3"]
shop_goods_limit = 100

# 3. SpuConfig
[spu]
db = "spu_db"
[spu.orm_config]
driver = "mysql"
dsn    = "user:pwd@tcp(127.0.0.1:3306)/spu?charset=utf8mb4&parseTime=True&loc=Local"

# 4. sdks
[[sdks]]      # 双括号表示数组  // []
platform         = "tb"
sdk_type         = "official"
data_consistency = "strong"
url              = "http://sdk-tb.example.com"
goods_site_proxy = "http://proxy-tb.example.com"
goods_types      = [0, 1]

[[sdks]]
platform         = "jd"
sdk_type         = "third"
data_consistency = "eventual"
url              = "http://sdk-jd.example.com"
goods_site_proxy = ""
goods_types      = [0]

# 5. spi_http_clients
[spi_http_clients]
[[spi_http_clients.clients]]
name = "message-receiver"
address = "http://localhost:8095"
timeout = 30

[[spi_http_clients.clients]]
name = "sdk-dy"
address = "http://localhost:8092"
timeout = 30

[queue_config]
# 订阅者列表
[queue_config.subscribers.goods_sub] #商品增量同步
queue_type = "rocketmq"
url = ""
# 发布者列表
[queue_config.publishers.goods_notify_pub]
queue_type = "rocketmq"
url = "

写法2(推荐)

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
34
35
36
37
38
39
# 1.Person 段(没有 squash,所以必须独立一层)
[person]
name = "demo-instance"

# 2. MockConfig(squash 到顶层)  //map
[whitelist]
  tb = ["shop1", "shop2"]
  jd = ["shop3"]
shop_goods_limit = 100

# 3. SpuConfig
[spu]
db = "spu_db"
[spu.orm_config]
driver = "mysql"
dsn    = "user:pwd@tcp(127.0.0.1:3306)/spu?charset=utf8mb4&parseTime=True&loc=Local"

# 4. sdks —— 内联表数组写法  // []
sdks = [ # 内联表示数组  // []
  { platform = "tb", sdk_type = "official", data_consistency = "strong", url = "http://sdk-tb.example.com", goods_site_proxy = "http://proxy-tb.example.com", goods_types = [0, 1] },
  { platform = "jd", sdk_type = "third",  data_consistency = "eventual", url = "http://sdk-jd.example.com", goods_site_proxy = "", goods_types = [0] }
]

# 5. spi_http_clients
[spi_http_clients]
clients = [  # 内联表示数组
  { name = "message-receiver", address = "http://localhost:8095", timeout = 30 },
  { name = "sdk-dy",        address = "http://sdk-dy-test-service:8080", timeout = 30 }
]

[queue_config]
# 订阅者列表
[queue_config.subscribers.goods_sub] #商品增量同步
queue_type = "rocketmq"
url = ""
# 发布者列表
[queue_config.publishers.goods_notify_pub]
queue_type = "rocketmq"
url = ""

© 2024- lfj