go-zero consul 集成

go-zero consul 集成

  • consul 安装
  • 集成 cousul
    • 创建rpc 和api服务
    • rpc 注册 consul
    • api发现rpc
    • consul 配置文件 替换本地yaml配置

consul 安装

参考 https://developer.hashicorp.com/consul/downloads

集成 cousul

创建rpc 和api服务

参见 go-zero 官网 https://go-zero.dev/cn/docs/quick-start/micro-service/

rpc 注册 consul

  • 引入依赖
go get -u github.com/zeromicro/zero-contrib/zrpc/registry/consul
  • 修改 rpc/**/etc/*.yaml
Name: mall.rpc
ListenOn: 0.0.0.0:8080
Consul:
  Host: 127.0.0.1:8500
  Key: card.rpc
  Meta:
    Protocol: grpc
  Tag:
    - card
    - rpc
  • 修改 rpc/**/internal/config/config.go
import (
	"github.com/zeromicro/go-zero/zrpc"
	"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
)

type Config struct {
	zrpc.RpcServerConf
	Consul consul.Conf
}

  • 修改 启动文件
import (
	"flag"
	"fmt"

	"wlwkgo/card/rpc/internal/config"
	"wlwkgo/card/rpc/internal/server"
	"wlwkgo/card/rpc/internal/svc"
	"wlwkgo/card/rpc/types/card"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
	_ "github.com/zeromicro/zero-contrib/zrpc/registry/consul"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

var configFile = flag.String("f", "etc/card.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)

	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		card.RegisterCardServer(grpcServer, server.NewCardServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	_ = consul.RegisterService(c.ListenOn, c.Consul) //注册consul
	defer s.Stop()

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start()
}

api发现rpc

  • main.go
    引入依赖
import _ "github.com/zeromicro/zero-contrib/zrpc/registry/consul"
  • etc/*.yaml
MallRpc:
  Target: consul://127.0.0.1:8500/mall.rpc?wait=14s

consul 配置文件 替换本地yaml配置

这里以rpc配置示例

  • 在consul 中创建配置文件 格式为 yaml
    go-zero consul 集成_第1张图片
Name: card.rpc
ListenOn: 0.0.0.0:8080
Consul:
  Host: 127.0.0.1
  Key: card.rpc
  Meta:
    Protocol: grpc
  Tag:
    - card
    - rpc
Mysql:
  DataSource: $user:$password@tcp($IP)/$DB?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
CacheRedis:
  - Host: 127.0.0.1
    Pass: 
    Type: node
  • config.go
import (
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/zrpc"
	"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
)

type Config struct {
	zrpc.RpcServerConf
	Consul consul.Conf
	Mysql  struct {
		DataSource string
	}

	CacheRedis cache.CacheConf
}
  • 启动文件
    以官方KV读取配置文件,然后以LoadConfigFromYamlBytes方法替换MustLoad
import (
	"flag"
	"fmt"
	"log"

	"wlwkgo/card/rpc/internal/config"
	"wlwkgo/card/rpc/internal/server"
	"wlwkgo/card/rpc/internal/svc"
	"wlwkgo/card/rpc/types/card"
	"github.com/hashicorp/consul/api"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
	_ "github.com/zeromicro/zero-contrib/zrpc/registry/consul"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)
var configFile = flag.String("f", "etc/card.yaml", "the config file")

func main() {
	flag.Parse()
	var c config.Config
	//conf.MustLoad(*configFile, &c)
	LoadConsulConf("mall", &c)
	log.Print(c)
	ctx := svc.NewServiceContext(c)
	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		card.RegisterCardServer(grpcServer, server.NewCardServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	_ = consul.RegisterService(c.ListenOn, c.Consul)
	defer s.Stop()
	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start()
}

func LoadConsulConf(key string, v interface{}) {
	client, _ := api.NewClient(&api.Config{Address: "http://127.0.0.1:8500"})
	kv := client.KV()
	data, _, err := kv.Get(key, nil)
	log.Print(string(data.Value))
	conf.LoadConfigFromYamlBytes(data.Value, v)
	logx.Must(err)
}

你可能感兴趣的:(golang,consul)