当前位置:网站首页>Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)
Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)
2022-06-23 02:36:00 【Trespass 】
Introduce
Through a complete example , stay gogf/gf In the frame , Differentiate profiles by environment . That is how to be in 【 test 】,【 on-line 】 Wait for the environment , Read different configuration files .
We will use rk-boot To start up gogf/gf Microservices .
Please visit the following address for a complete tutorial :
install
go get github.com/rookie-ninja/rk-boot/gf
Quick start
We will create config/beijing.yaml, config/shanghai.yaml, config/default.yaml Three configuration files , Then read different files according to different environment variables .
rk-boot Use REALM,REGION,AZ,DOMAIN Environment variables to distinguish different environments . This is also the cloud native environment resolution method we recommend .
such as ,REALM=" Your business ",REGION=" Beijing ",AZ=" Beijing district one ",DOMAIN=" Test environment ".
rk-boot Integrated viper To handle configuration files .
1. create profile
- config/beijing.yaml
--- my-region: beijing
- config/shanghai.yaml
--- my-region: shanghai
- config/default.yaml
--- my-region: default
2. establish boot.yaml
boot.yaml File tell rk-boot How to start gogf/gf service .
We use config As boot.yaml Entry of configuration file in , You can offer multiple config File path .
locale representative Config Environment , We use locale To differentiate Config.
Why? config.name Use the same name ?
We want to use the same set of code , But read different files , And I hope the name of the file is different . So pass locale To distinguish between different files . We will introduce in detail later locale The logic of .
config:
# Default
- name: my-config
locale: "*::*::*::*"
path: config/default.yaml
# If the environment variable REGION=beijing, Read this file
- name: my-config
locale: "*::beijing::*::*"
path: config/beijing.yaml
# If the environment variable REGION=shanghai, Read this file
- name: my-config
locale: "*::shanghai::*::*"
path: config/shanghai.yaml
gf:
- name: greeter
port: 8080
enabled: true3. establish main.go
Set the environment variable :REGION="beijing", Then read the configuration file ,config/beijing.yaml Will be read .
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-boot/gf"
"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("my-region"))
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}4. Folder structure
$ tree . ├── boot.yaml ├── config │ ├── beijing.yaml │ ├── default.yaml │ └── shanghai.yaml ├── go.mod ├── go.sum └── main.go
5. verification
$ go run main.go
We will get the following output :
beijing
6. No matching environment variables found
If REGION="not-matched", That is, no matching environment variable was found , The default configuration file will be read (config/default.yaml). because config/default.yaml Of locale The attribute is *::*::*::*
// Application entrance.
func main() {
// Set REGION=not-matched
os.Setenv("REGION", "not-matched")
...
// Load config which is config/default.yaml
fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("my-region"))
...
}We will get the following output :
$ go run main.go default
7. Environment variable not configured
If we don't have configuration REGION environment variable , Will read config/default.yaml file .
// Application entrance.
func main() {
...
// Load config which is config/beijing.yaml
fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("my-region"))
...
}We will get the following output :
$ go run main.go default
Concept
rk-boot Use REALM,REGION,AZ,DOMAIN Four environment variables to distinguish configuration files .
These four environment variables can be arbitrary values .
Best practices
for instance , We have a 【 Cloud album 】 Business . This business is used in different environments MySQL Of IP Different addresses , You can configure it like this .
framework
Assume , Our business in 【 Beijing 】,【 Shanghai 】 All have servers , At the same time, in order to improve service availability , stay 【 Beijing 】 and 【 Shanghai 】 It's on again 2 Districts .
Now , We can configure the following environment variables on the machine , Can pass Ansible And other tools to set in batch .
Environmental Science | Corresponding environment variable |
|---|---|
Beijing , Area 1 , test | REALM="cloud-album",REGION="bj",AZ="bj-1",DOMAIN="test" |
Beijing , Area 1 , on-line | REALM="cloud-album",REGION="bj",AZ="bj-1",DOMAIN="prod" |
Beijing , Two district , test | REALM="cloud-album",REGION="bj",AZ="bj-2",DOMAIN="test" |
Beijing , Two district , on-line | REALM="cloud-album",REGION="bj",AZ="bj-2",DOMAIN="prod" |
Shanghai , Area 1 , test | REALM="cloud-album",REGION="sh",AZ="sh-1",DOMAIN="test" |
Shanghai , Area 1 , on-line | REALM="cloud-album",REGION="sh",AZ="sh-1",DOMAIN="prod" |
Shanghai , Two district , test | REALM="cloud-album",REGION="sh",AZ="sh-2",DOMAIN="test" |
Shanghai , Two district , on-line | REALM="cloud-album",REGION="sh",AZ="sh-2",DOMAIN="prod" |
meanwhile , If we don't use something like ETCD,Consul And other services pull configuration files remotely , You can add the following files directly to the machine . Each file has a different MySQL IP Address .
Folder structure
. ├── 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
Next , We are boot.yaml Add the following config entrance .
config:
# Default entry
- name: my-config
locale: "*::*::*::*"
path: config/default.yaml
# Beijing , Area 1 , Test environment
- name: my-config
locale: "cloud-album::bj::bj-1::test"
path: config/bj-1-test.yaml
# Beijing , Area 1 , The online environment
- name: my-config
locale: "cloud-album::bj::bj-1::prod"
path: config/bj-1-prod.yaml
# Beijing , Two district , Test environment
- name: my-config
locale: "cloud-album::bj::bj-2::test"
path: config/bj-2-test.yaml
# Beijing , Two district , The online environment
- name: my-config
locale: "cloud-album::bj::bj-2::prod"
path: config/bj-2-prod.yaml
# Shanghai , Area 1 , Test environment
- name: my-config
locale: "cloud-album::sh::sh-1::test"
path: config/sh-1-test.yaml
# Shanghai , Area 1 , The online environment
- name: my-config
locale: "cloud-album::sh::sh-1::prod"
path: config/sh-1-prod.yaml
# Shanghai , Two district , Test environment
- name: my-config
locale: "cloud-album::sh::sh-2::test"
path: config/sh-2-test.yaml
# Shanghai , Two district , The online environment
- name: my-config
locale: "cloud-album::sh::sh-2::prod"
path: config/sh-2-prod.yaml
gf:
- name: greeter
port: 8080
enabled: truemain.go Read configuration file from .
because , be-all Config They are all named my-config, stay main.go When reading from , We can use my-config obtain 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())
}Use environment variables to override
rk-boot Integrated viper To handle configuration files , therefore , Naturally integrated viper All the functions that come with it .
This includes passing 【 environment variable 】 Overwrite the existing configuration 【 value 】. Let's take an example .
1.config/default.yaml
stay config/default.yaml In file , Add one K/V.
--- endpoint: 8.8.8.8
2.main.go
Under normal circumstances , The following code will get 【8.8.8.8】, But we use environment variables , covers 【endpoint】 Value . Be careful , Use environment variables to override , Environment variable Key Need to use capital English .
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-boot/gf"
"os"
)
// Application entrance.
func main() {
// Set ENDPOINT=localhost
os.Setenv("ENDPOINT", "localhost")
// Create a new boot instance.
boot := rkboot.NewBoot()
// Load config which is config/default.yaml
fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("endpoint"))
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}3. verification
$ go run main.go localhost
4. Use the environment variable prefix
In the real world , There may be problems of environment variable conflicts . Now , We can do it in Viper There is a 【 Environment variable prefix 】 To mark our Config.
for instance , Assume that the system has HOSTNAME As an environment variable , Initialize to each machine . If we force this value to change , You will encounter unpredictable errors . Now , We can add a prefix .
Example :
- config/default.yaml
--- hostname: my-hostname
- boot.yaml stay config In the options , Add our ENV Prefix .
config:
- name: my-config
locale: "*::*::*::*"
path: config/default.yaml
envPrefix: rk
gf:
- name: greeter
port: 8080
enabled: true- main.go Now , We are overriding by environment variables HOSTNAME When , add to RK_ As a prefix .
Reference resources viper Official documents
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-boot/gf"
"os"
)
// Application entrance.
func main() {
// Set RK_HOSTNAME=override-hostname
os.Setenv("RK_HOSTNAME", "override-hostname")
// Create a new boot instance.
boot := rkboot.NewBoot()
// Load config which is config/default.yaml
fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("hostname"))
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}- verification
$ go run main.go override-hostname
边栏推荐
- Detailed explanation of online reputation management
- Canvas draw the clock
- Push RTMP stream using ffmpeg
- Application and challenge of ten billion level map data in Kwai security intelligence
- Xgboost Guide
- Spread spectrum and frequency hopping
- Performance test -- Jenkins environment construction for 15jmeter performance test
- Understand GB, gbdt and xgboost step by step
- Capture passwords of all chrome versions
- Log a log4j2 vulnerability handling
猜你喜欢

Deep learning environment configuration (I) installation of CUDA and cudnn

Interviewer: what is the difference between SSH and SSM frameworks? How to choose??

5. concept of ruler method

Vs Code inadvertently disable error waveform curve

Unity official case nightmare shooter development summary < I > realization of the role's attack function

Xgboost Guide

Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.

Evolution history of mobile communication

Li Mu's notes on machine learning -1.2

1.3-1.4 web page data capture
随机推荐
JS request path console reports an error failed to launch 'xxx' because the scheme does not have a registered handler
Deep learning environment configuration (III) pytorch GPU under Anaconda
8. greed
How does the easyplayer streaming video player set up tiling?
Ansible practice of Nepal graph
Summary of easy-to-use MySQL interview questions (Part 1)
Deep scan log4j2 vulnerability using codesec code audit platform
Aikuai multi dialing + load balancing overlay bandwidth
Quick sorting C language code + auxiliary diagram + Notes
2021-11-11
Schedule tasks to periodically restart remote services or restart machines
Source code analysis | activity setcontentview I don't flash
How to make a borrowing card
HTTP cache
[CodeWars]Matrix Determinant
Cmake configuration error, error configuration process, Preject files may be invalid
Call rest port to implement nailing notification
Spread spectrum and frequency hopping
How to set up an H5 demo of easyplayer locally to play h265 video streams?
5g access network and base station evolution