最近用 Go 写后端写得很开心,写篇比较实用的博客总结下如何通过 Spring Cloud Config Server 管理 Go 程序中的配置。 实现并不复杂,因此也可以很轻易地推广到其他语言的程序中。
先来说说为什么要做集中配置管理。在单体应用时代配置管理并不是什么大问题, 一般配置文件就和源码一起放在代码仓库中, 要查看或者修改直接到 conf
目录里找就完事儿了。但到了微服务时代,服务的数量比过去多了几十倍,再到茫茫多的代码仓库里找配置可就没这么简单了。因此我们需要一个能够统一查看修改配置,能够对配置进行版本控制的地方,这就是配置中心了。
在 Google 上搜索 "配置中心" 能找到不少不错的开源软件,但大部分都比较重,并且需要引入特定的客户端。这对没到那么大规模的中小团队来说未免太过折腾,因此反而像 Spring Cloud Config Server 这样的轻量级配置中心比较适合,几分钟就能跑起来, 而且和配置本身相关的功能也足够丰富了。
因此我们的架构就像下面这样:
作为演示我们用 Go 写一个很简单的搜索服务,只要访问 GET /search?q=<keyword>
服务就会把搜索引擎查到的结果展示出来,用 Go 实现只要一个文件。
main.go
1package main
2
3import ...
4
5func main() {
6 http.HandleFunc("/search", func(w http.ResponseWriter, req *http.Request) {
7 q := req.URL.Query().Get("q")
8 fmt.Fprintf(w, `<iframe width="100%%" height="98%%" scrolling="auto" frameborder="0" src="https://cn.bing.com/search?q=%v">`, q)
9 })
10 log.Fatal(http.ListenAndServe(":8081", nil))
11}
接着把服务跑起来:
1go run main.go
在浏览器中访问 http://localhost:8081/search?q=golang
很简单是不是?但这里的问题在于我们把 https://cn.bing.com
写死在了代码中,如果要切换搜索引擎就得重新编译程序,真的是费时费力。 这时候我们就需要将配置解耦到配置文件中了。
我们先在本地建一个配置文件 go-app.yml
1app:
2 search_url: https://cn.bing.com/search?q=%v
然后通过 viper 这个比较流行的配置库加载这个配置。
conf/conf.go
1package conf
2
3import ...
4
5func init() {
6 viper.SetConfigName("go-app")
7 viper.AddConfigPath(os.ExpandEnv(`$GOPATH\src\github.com\GotaX\config-server-demo`))
8 viper.SetConfigType("yaml")
9 if err := viper.ReadInConfig(); err != nil {
10 log.Fatal("Fail to load config", err)
11 }
12}
现在我们就把搜索引擎的地址解耦到配置文件中去了。
main.go
1package main
2
3import ...
4
5func main() {
6 http.HandleFunc("/search", func(w http.ResponseWriter, req *http.Request) {
7 q := req.URL.Query().Get("q")
8 src := fmt.Sprintf(viper.GetString("app.search_url"), q)
9 fmt.Fprintf(w, `<iframe width="100%%" height="98%%" scrolling="auto" frameborder="0" src="%v">`, src)
10 })
11 log.Fatal(http.ListenAndServe(":8081", nil))
12}
接下来我们将配置文件从本地转移到 Git 中,处于方便我就直接放在当前仓库的 config 分支中了。
地址为: https://github.com/GotaX/config-server-demo/tree/config
配置文件上传完毕,我们再新开一个 config-server 空分支搭建配置中心。
首先到 https://start.spring.io/ 页面新建一个 Java + Gradle 的 Spring Boot 工程,依赖项选 Config Server。
点击 "Generate Project" 将下载压缩包, 并解压。
修改 Application.java
1package com.example.demo;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5import org.springframework.cloud.config.server.EnableConfigServer;
6
7@EnableConfigServer // 添加这行
8@SpringBootApplication
9public class DemoApplication {
10 public static void main(String[] args) {
11 SpringApplication.run(DemoApplication.class, args);
12 }
13}
修改 application.yml, 填入我们存放配置文件的仓库地址。
1spring.cloud.config.server.git.uri: https://github.com/GotaX/config-server-demo.git
在工程根目录启动 config server。
1gradle bootrun
访问 http://localhost:8080/config/go-app-default.yml
查看配置。
1app:
2 search_url: https://cn.bing.com/search?q=%v
这样我们的配置中心就启动完毕了。
最后就是在应用中使用 Spring Cloud Config Server 中的配置了。如果是基于 Spring Boot 的应用可以直接使用 spring-cloud-config-client
加载配置。在 Go 中就需要稍微写点代码了,不过并不多。
我们先在 config.go 中添加一个 loadRemote()
函数,用来从配置中心读取配置:
conf/conf.go
1// ...
2const (
3 kAppName = "APP_NAME"
4 kConfigServer = "CONFIG_SERVER"
5 kConfigLabel = "CONFIG_LABEL"
6 kConfigProfile = "CONFIG_PROFILE"
7 kConfigType = "CONFIG_TYPE"
8)
9
10func loadRemoteConfig() (err error) {
11 // 组装配置文件地址: http://localhost:8080/config/go-app-default.yaml
12 confAddr := fmt.Sprintf("%v/%v/%v-%v.yml",
13 viper.Get(kConfigServer), viper.Get(kConfigLabel),
14 viper.Get(kAppName), viper.Get(kConfigProfile))
15 resp, err := http.Get(confAddr)
16 if err != nil {
17 return
18 }
19 defer resp.Body.Close()
20
21 // 设置配置文件格式: yaml
22 viper.SetConfigType(viper.GetString(kConfigType))
23 // 载入配置文件
24 if err = viper.ReadConfig(resp.Body); err != nil {
25 return
26 }
27 log.Println("Load config from: ", confAddr)
28 return
29}
当然,我们需要知道配置中心的入口,因此还需一个 initDefault()
函数来初始化这些配置:
conf/conf.go
1func initDefault() {
2 viper.SetDefault(kAppName, "go-app")
3 viper.SetDefault(kConfigServer, "http://localhost:8080")
4 viper.SetDefault(kConfigLabel, "config")
5 viper.SetDefault(kConfigProfile, "default")
6 viper.SetDefault(kConfigType, "yaml")
7}
现在我们的 init()
函数变成了这样:
conf/conf.go
1func init() {
2 viper.AutomaticEnv()
3 initDefault()
4
5 if err := loadRemoteConfig(); err != nil {
6 log.Fatal("Fail to load config", err)
7 }
8}
其中的 viper.AutomaticEnv()
可以让我们通过环境变量修改任意配置,因此 initDefault()
中的配置也不是写死在代码中的了。其中比较常见的用法是通过 CONFIG_PROFILE=prod
环境变量来切换 profile。
最后我们希望 viper 仅在 conf 包中出现, 而对外隐藏我们加载配置的具体实现。 因此我们将配置读到结构体中再对外提供:
conf/conf.go
1var App AppConfig
2
3type AppConfig struct {
4 SearchUrl string `mapstructure:"search_url"`
5}
6
7func init() {
8 // ...
9 if err := sub("app", &App); err != nil {
10 log.Fatal("Fail to parse config", err)
11 }
12}
13
14func sub(key string, value interface{}) error {
15 sub := viper.Sub(key)
16 sub.AutomaticEnv()
17 sub.SetEnvPrefix(key)
18 return sub.Unmarshal(value)
19}
这时我们就可以从 main.go 中去掉 viper.Get()
调用了:
main.go
1import ...
2
3func main() {
4 http.HandleFunc("/search", func(w http.ResponseWriter, req *http.Request) {
5 q := req.URL.Query().Get("q")
6 src := fmt.Sprintf(conf.App.SearchUrl, q)
7 fmt.Fprintf(w, `<iframe width="100%%" height="98%%" scrolling="auto" frameborder="0" src="%v">`, src)
8 })
9 log.Fatal(http.ListenAndServe(":8081", nil))
10}
我们通过 Git + Spring Could Config Server + Viper + 少量 Go 代码, 实现了基于配置中心的配置管理及使用
我们甚至可以在 Go 中使用类似于 Spring Boot 的 Profile 管理, 对比下:
完整的代码可以参考 https://github.com/GotaX/config-server-demo 下的 3 个分支:
当然, 目前这种使用方式还比较简陋, 还有很多可以改进的地方, 比如:
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有