亚洲一区精品自拍_2021年国内精品久久_男同十八禁gv在线观看_免费观看a级性爱黄片

Article / 文章中心

Gin 框架: 基于云原生環(huán)境,區(qū)分配置文件

發(fā)布時(shí)間:2021-11-25 點(diǎn)擊數(shù):784

介紹

通過一個(gè)完整例子,在 Gin 框架中,根據(jù)環(huán)境區(qū)分配置文件。也就是如何在【測試】,【線上】等環(huán)境中,讀取不同的配置文件。

我們將會(huì)使用 rk-boot 來啟動(dòng) Gin 框架微服務(wù)。

請(qǐng)?jiān)L問如下地址獲取完整教程:

  • https://rkdocs.netlify.app/cn

安裝

go get github.com/rookie-ninja/rk-boot

快速開始

我們會(huì)創(chuàng)建 config/beijing.yaml, config/shanghai.yaml, config/default.yaml 三個(gè)配置文件,然后根據(jù)不同的環(huán)境變量讀取不同的文件。

rk-boot 使用 REALM,REGION,AZ,DOMAIN 環(huán)境變量來區(qū)分不同的環(huán)境。這也是我們推薦的云原生環(huán)境分辨法。
比如,REALM="你的業(yè)務(wù)",REGION="北京",AZ="北京一區(qū)",DOMAIN="測試環(huán)境"。

rk-boot 集成了 viper 來處理配置文件。

1.創(chuàng)建配置文件

  • config/beijing.yaml
--- region: beijing
  • config/shanghai.yaml
--- region: shanghai
  • config/default.yaml
--- region: default

2.創(chuàng)建 boot.yaml

boot.yaml 文件告訴 rk-boot 如何啟動(dòng) Gin 服務(wù)。

我們使用 config 作為 boot.yaml 中配置文件的入口,可以提供多個(gè) config 文件路徑。

locale 代表 Config 的環(huán)境,我們使用 locale 來區(qū)分不同的 Config。

為什么 config.name 使用同一個(gè)名字?

我們希望使用同一套代碼,但是讀取不同的文件,并且希望文件的名字也不一樣。
所以通過 locale 來區(qū)分不同文件。我們?cè)诤竺婢唧w介紹 locale 的邏輯。

config: # 默認(rèn) - name: my-config locale: "*::*::*::*" path: config/default.yaml # 如果環(huán)境變量 REGION=beijing,讀取此文件 - name: my-config locale: "*::beijing::*::*" path: config/beijing.yaml # 如果環(huán)境變量 REGION=shanghai,讀取此文件 - name: my-config locale: "*::shanghai::*::*" path: config/shanghai.yaml gin: - name: greeter port: 8080 enabled: true

3.創(chuàng)建 main.go

設(shè)置環(huán)境變量:REGION="beijing",然后讀取配置文件,config/beijing.yaml 會(huì)被讀取。

package main import ( "context" "fmt" "github.com/rookie-ninja/rk-boot" "os" ) // Application entrance. func main() { // Set REGION=beijing os.Setenv("REGION", "beijing") // Create a new boot instance. boot := rkboot.NewBoot() // Load config which is config/beijing.yaml fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("region")) // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background())
}

4.文件夾結(jié)構(gòu)

$ tree
.
├── boot.yaml
├── config │   ├── beijing.yaml
│   ├── default.yaml
│   └── shanghai.yaml
├── go.mod
├── go.sum
└── main.go

5.驗(yàn)證

$ go run main.go

我們會(huì)得到如下的輸出:

beijing

6.未找到匹配的環(huán)境變量

如果 REGION="not-matched",即未找到匹配的環(huán)境變量,則會(huì)讀取默認(rèn)的配置文件(config/default.yaml)。因?yàn)?config/default.yaml 的 locale 屬性為 *::*::*::*

// Application entrance. func main() { // Set REGION=not-matched os.Setenv("REGION", "not-matched")
    
    ... // Load config which is config/beijing.yaml fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("region"))
    ...
}
default

7.環(huán)境變量未配置

如果我們沒有配置 REGION 環(huán)境變量,則會(huì)讀取 config/default.yaml 文件。

// Application entrance. func main() {
    ... // Load config which is config/beijing.yaml fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("region"))
    ...
}
default

概念

rk-boot 使用 REALM,REGION,AZ,DOMAIN 四個(gè)環(huán)境變量來區(qū)分配置文件。

這四個(gè)環(huán)境變量可以是任意的值。

最佳實(shí)踐

舉個(gè)例子,我們有一個(gè)【云相冊(cè)】業(yè)務(wù)。此業(yè)務(wù)在不同環(huán)境里使用的 MySQL 的 IP 地址不一樣,則可以這么配置。

架構(gòu)

假定,我們的業(yè)務(wù)在【北京】,【上?!慷加蟹?wù)器,同時(shí)為了提高服務(wù)可用性,在【北京】和【上海】又各開了2個(gè)區(qū)。

這時(shí)候,我們可以機(jī)器上配置如下的環(huán)境變量,可以通過 Ansible 等工具來批量設(shè)置。

環(huán)境 對(duì)應(yīng)環(huán)境變量
北京,一區(qū),測試 REALM="cloud-album",REGION="bj",AZ="bj-1",DOMAIN="test"
北京,一區(qū),線上 REALM="cloud-album",REGION="bj",AZ="bj-1",DOMAIN="prod"
北京,二區(qū),測試 REALM="cloud-album",REGION="bj",AZ="bj-2",DOMAIN="test"
北京,二區(qū),線上 REALM="cloud-album",REGION="bj",AZ="bj-2",DOMAIN="prod"
上海,一區(qū),測試 REALM="cloud-album",REGION="sh",AZ="sh-1",DOMAIN="test"
上海,一區(qū),線上 REALM="cloud-album",REGION="sh",AZ="sh-1",DOMAIN="prod"
上海,二區(qū),測試 REALM="cloud-album",REGION="sh",AZ="sh-2",DOMAIN="test"
上海,二區(qū),線上 REALM="cloud-album",REGION="sh",AZ="sh-2",DOMAIN="prod"

同時(shí),如果我們不使用類似 ETCD,Consul 等服務(wù)遠(yuǎn)程拉取配置文件,可以直接在機(jī)器中添加如下文件。每個(gè)文件都有不同的 MySQL IP 地址。

文件夾結(jié)構(gòu)

.
├── boot.yaml ├── config │   ├── bj-1-test.yaml │   ├── bj-1-prod.yaml │   ├── bj-2-test.yaml │   ├── bj-2-prod.yaml │   ├── sh-1-test.yaml │   ├── sh-1-prod.yaml │   ├── sh-2-test.yaml │   ├── sh-2-prod.yaml │   └── default.yaml ├── go.mod ├── go.sum └── main.go

boot.yaml

接下來,我們?cè)?boot.yaml 里添加如下 config 入口。

config: # 默認(rèn)入口 - name: my-config locale: "*::*::*::*" path: config/default.yaml # 北京,一區(qū),測試環(huán)境 - name: my-config locale: "cloud-album::bj::bj-1::test" path: config/bj-1-test.yaml # 北京,一區(qū),線上環(huán)境 - name: my-config locale: "cloud-album::bj::bj-1::prod" path: config/bj-1-prod.yaml # 北京,二區(qū),測試環(huán)境 - name: my-config locale: "cloud-album::bj::bj-2::test" path: config/bj-2-test.yaml # 北京,二區(qū),線上環(huán)境 - name: my-config locale: "cloud-album::bj::bj-2::prod" path: config/bj-2-prod.yaml # 上海,一區(qū),測試環(huán)境 - name: my-config locale: "cloud-album::sh::sh-1::test" path: config/sh-1-test.yaml # 上海,一區(qū),線上環(huán)境 - name: my-config locale: "cloud-album::sh::sh-1::prod" path: config/sh-1-prod.yaml # 上海,二區(qū),測試環(huán)境 - name: my-config locale: "cloud-album::sh::sh-2::test" path: config/sh-2-test.yaml # 上海,二區(qū),線上環(huán)境 - name: my-config locale: "cloud-album::sh::sh-2::prod" path: config/sh-2-prod.yaml gin: - name: greeter port: 8080 enabled: true

main.go 中讀取配置文件。

因?yàn)?,所有?Config 都命名為 my-config,在 main.go 中讀取的時(shí)候,我們可以使用 my-config 獲取 ConfigEntry。

package main import ( "context" "fmt" "github.com/rookie-ninja/rk-boot" "os" ) // Application entrance. func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Get viper instance based on environment variable boot.GetConfigEntry("my-config").GetViper() // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background())
}