grpc_resolver_zk

package
v2.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2024 License: MIT, MIT Imports: 12 Imported by: 0

README

GRPC Resolver Nacos

GitHub go.mod Go version GitHub

本项目实现了Grpc中Nacos的服务发现

  • 支持负载均衡中Nacos权重选择器
  • 支持 grpc-gateway
  • 使用 Nacos-sdk-go 同结构配置方便

Installation

$ go get -u github.com/hang666/grpc_resolver_nacos

Client Example

// 创建NacosClientConfig
clientConfig := *constant.NewClientConfig(
	constant.WithNamespaceId(""),
	constant.WithTimeoutMs(5000),
	constant.WithNotLoadCacheAtStart(true),
	constant.WithLogDir("/tmp/nacos/log"),
	constant.WithCacheDir("/tmp/nacos/cache"),
	constant.WithLogLevel("debug"),
)
// 创建NacosServerConfigs (兼容多发现中心)
serverConfigs := []constant.ServerConfig{*constant.NewServerConfig("127.0.0.1", 8848, constant.WithContextPath("/nacos"))}
// 创建NacosNamingClient
client, err := clients.CreateNamingClient(map[string]interface{}{
	"serverConfigs": serverConfigs,
	"clientConfig":  &clientConfig,
})
// 添加Nacos配置 (支持多服务)
grpc_resolver_nacos.AddNacosConfig(grpc_resolver_nacos.NacosConfig{
	Label:              "user",			//Label与ServiceName一致即可
	ServiceName:        "user",			//Nacos内注册的服务名
	Clusters           	[]string{},
	GroupName          	"",
	NacosClientConfig:  clientConfig,
	NacosServerConfigs: serverConfigs,
	NacosClient:        client,
})
// target按照 nacos://ServiceName/ 填写即可,如上添加过的配置
// grpc-gateway中RegisterXXXHandlerFromEndpoint 如此target填写相同即可
conn, err := grpc.Dial("nacos://user/",
	grpc.WithTransportCredentials(insecure.NewCredentials()),
	grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "nacos_weight_balancer"}`))

Load Balancing

// 如下将 {"loadBalancingPolicy": "nacos_weight_balancer"} 添加DialOption即可
conn, err := grpc.Dial("nacos://user/",
	grpc.WithTransportCredentials(insecure.NewCredentials()),
	grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "nacos_weight_balancer"}`))

Documentation

Index

Constants

View Source
const Name = "zk_weight_balancer"

Variables

View Source
var ZkConfigs = make(map[string]*ZkConfig)

Functions

func AddZkConfig

func AddZkConfig(config ZkConfig)

AddZkConfig is not thread-safe

func DelZkConfig

func DelZkConfig(label string)

Types

type Chooser

type Chooser struct {
	// contains filtered or unexported fields
}

type Watcher

type Watcher interface {
	Endpoints() []string
	Event() <-chan struct{}
	IsClosed() bool
	Close()
}

A Watcher represents how a serverset.Watch is used so it can be stubbed out for tests.

type WeightAddrInfo

type WeightAddrInfo struct {
	Weight int
}

type WeightAttributeKey

type WeightAttributeKey struct{}

type ZkConfig

type ZkConfig struct {
	Label       string //用做寻找配置的标签
	ServiceName string //标记服务名称
	Watcher     Watcher
	Group       string
	Version     string
}

type ZkResolver

type ZkResolver struct {
	*ZkConfig
}

func (*ZkResolver) Build

func (*ZkResolver) Close

func (r *ZkResolver) Close()

func (*ZkResolver) ResolveNow

func (r *ZkResolver) ResolveNow(resolver.ResolveNowOptions)

func (*ZkResolver) Scheme

func (r *ZkResolver) Scheme() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL