当前位置:网站首页>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: true

2. 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: true

main.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())
}
原网站

版权声明
本文为[Trespass ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/10/20211025184747415u.html

随机推荐