grpc_resolver_nacos

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: 18 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 = "nacos_weight_balancer"

Variables

View Source
var NacosConfigs = make(map[string]*NacosConfig)

Functions

func AddNacosConfig

func AddNacosConfig(config NacosConfig)

AddNacosConfig is not thread-safe

func DelNacosConfig

func DelNacosConfig(label string)

DelNacosConfig is not thread-safe

func RegisterInstance

func RegisterInstance(ip string, port uint64, serviceName string, weight float64, enable bool, healthy bool,
	metadata map[string]string, clusterName string, groupName string, ephemeral bool, nacosClient NacosClient) (bool, error)

Types

type Chooser

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

Chooser from naming_client package in nacos-sdk-go

type NacosClient

type NacosClient = naming_client.INamingClient

type NacosConfig

type NacosConfig struct {
	Label       string //用做寻找配置的标签
	ServiceName string //标记服务名称
	Clusters    []string
	GroupName   string
	NacosClient NacosClient
}

type NacosInstance

type NacosInstance = model.Instance

func GetHealthyInstances

func GetHealthyInstances(serviceName string, clusters []string, groupName string, nacosClient NacosClient) ([]NacosInstance, error)

func GetOneHealthyInstance

func GetOneHealthyInstance(serviceName string, clusters []string, groupName string, nacosClient NacosClient) (*NacosInstance, error)

type NacosResolver

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

func (*NacosResolver) Build

func (*NacosResolver) Close

func (r *NacosResolver) Close()

func (*NacosResolver) ResolveNow

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

func (*NacosResolver) Scheme

func (r *NacosResolver) Scheme() string

type NacosService

type NacosService = model.Service

func GetService

func GetService(serviceName string, clusters []string, groupName string, nacosClient NacosClient) (NacosService, error)

type WeightAddrInfo

type WeightAddrInfo struct {
	Weight int
}

type WeightAttributeKey

type WeightAttributeKey struct{}

Jump to

Keyboard shortcuts

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