当前位置:网站首页>Grpc: based on cloud native environment, distinguish configuration files
Grpc: based on cloud native environment, distinguish configuration files
2022-06-24 02:42:00 【Trespass 】
Introduce
This article will show you how to gRPC Distinguish configuration files according to environment in microservice . 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 gRPC service .
Please visit the following address for a complete tutorial :https://rkdev.info/cnhttps://rkdocs.netlify.app/cn ( spare )
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-grpc
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
--- region: beijing
- config/shanghai.yaml
--- region: shanghai
- config/default.yaml
--- region: default
1. establish boot.yaml
We use config As the entry of the configuration file , You can offer multiple Config File path .
locale representative Config Of Environmental Science , We use locale Come on distinguish Different 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
grpc:
- name: greeter
port: 8080
enabled: true2. establish main.go
Set the environment variable :REGION="beijing", Then read the configuration file ,config/beijing.yaml Will be read .
package main
import (
"context"
"fmt"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-grpc/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())
}3. 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/beijing.yaml
fmt.Println(boot.GetConfigEntry("my-config").GetViper().GetString("region"))
...
}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("region"))
...
}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 , If we want to 【 Cloud album 】 Business , Used in different environments MySQL Of IP Different addresses , You can configure it like this .
framework
Assume , Our business is 【 Cloud album 】, And in 【 Beijing 】,【 Shanghai 】 All have servers , At the same time, for service availability , stay 【 Beijing 】 and 【 Shanghai 】 It's on again 2 Districts .
Now , We can work on machines in every different environment , Configure the following environment variables , 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 the profile management service , It can be directly in micro services , Add the following file . 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
grpc:
- 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"
_ "github.com/rookie-ninja/rk-grpc/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())
}边栏推荐
- The technical route is based on UE4 for secondary development
- What does operation and maintenance audit fortress mean? How to select the operation and maintenance audit fortress machine?
- CPS November additional incentive rules
- What is the meaning of scdo? Is it comparable to bGH
- What are the conditions for trademark registration? How long does it take to register a trademark?
- Do you still understand the deadlock handling methods in MySQL performance testing and tuning?
- Some tips for using uitextview
- Create and mount large files
- Operation and maintenance platform tcapulusdb transaction management
- Code 128 barcode details
随机推荐
Deep and shallow copy
How to build video websites? What are the types of video websites?
Official spoilers! Figure 1 understand Tencent security @2021 Tencent digital ecology Conference
Start tcapulusdb process
How to build a website? These things should be paid attention to
Pan micro reached cooperation with Tencent to help enterprises connect with banking services and support enterprise digital upgrading
Iranian gas station paralyzed by cyber attack, babuk blackmail software source code leaked | global network security hotspot
Gin framework: add Prometheus monitoring
VNC enters the password and goes around for a long time before entering the desktop. Use procmon to locate the reason
What are the general contents of the enterprise website construction scheme
Is the IP of the fortress machine the IP of the server? How to deploy the fortress machine
Afnetworking server client
MySQL case deep excavation information_ Root causes of slow schema view query (Part 2)
Cloud function pressure measurement based on wechat applet
How does easydss handle the problem that the sharing page cannot be opened due to cache problems?
Grpc: adjust data transfer size limit
How does [lightweight application server] build a cross-border e-commerce management environment?
Case of data recovery by misoperation under NTFS file system
Flink practice tutorial: getting started 1- zero basic users realize simple Flink tasks
How long can the trademark registration be completed? How to improve the speed of trademark registration?
