1 Star 0 Fork 0

天雨流芳 / go-micro-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
registry.go 4.43 KB
一键复制 编辑 原始数据 按行查看 历史
天雨流芳 提交于 2024-03-28 16:17 . 基于nacos的服务发现
package nacos
import (
"context"
"errors"
"fmt"
"gitee.com/tylf2018/go-micro-framework/pkg/common/util/net"
log "gitee.com/tylf2018/go-micro-framework/pkg/logger"
"gitee.com/tylf2018/go-micro-framework/registry"
regOps "gitee.com/tylf2018/go-micro-framework/registry/registry"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"net/url"
)
var ErrServiceInstanceNameEmpty = errors.New("kratos/nacos: ServiceInstance.Name can not be empty")
type options struct {
prefix string
weight float64
cluster string
group string
kind string
}
// Option is nacos option.
type Option func(o *options)
// NacosRegistry is nacos registry.
type NacosRegistry struct {
opts *options
cli naming_client.INamingClient
}
func NewDefaultNacosRegistry(reg *regOps.RegistryOptions, opts ...Option) *NacosRegistry {
host, port, err := net.ExtractHostPort(reg.Address)
if err != nil {
log.ErrorF("split host and port error: %v", err)
panic(err)
}
sc := []constant.ServerConfig{
*constant.NewServerConfig(host, uint64(port)),
}
cc := constant.ClientConfig{
NamespaceId: "public", // namespace id
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
LogLevel: "debug",
}
// a more graceful way to create naming client
client, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
if err != nil {
log.ErrorF("create naming client failed.error=%v", err.Error())
panic(err)
}
return NewNacosRegistry(client, opts...)
}
// NewNacosRegistry new a nacos registry.
func NewNacosRegistry(cli naming_client.INamingClient, opts ...Option) *NacosRegistry {
op := &options{
prefix: "/microservices",
cluster: "DEFAULT",
group: constant.DEFAULT_GROUP,
weight: 100,
kind: "grpc",
}
if len(opts) > 0 {
for _, o := range opts {
o(op)
}
}
return &NacosRegistry{
opts: op,
cli: cli,
}
}
// Register the registration.
func (r *NacosRegistry) Register(_ context.Context, si *registry.ServiceInstance) error {
if si.Name == "" {
return ErrServiceInstanceNameEmpty
}
for _, endpoint := range si.Endpoints {
u, err := url.Parse(endpoint)
if err != nil {
return err
}
host, port, err := net.ExtractHostPort(u.Host)
if err != nil {
return err
}
var rmd map[string]string
if si.Metadata == nil {
rmd = map[string]string{
"kind": u.Scheme,
"version": si.Version,
}
} else {
rmd = make(map[string]string, len(si.Metadata)+2)
for k, v := range si.Metadata {
rmd[k] = v
}
rmd["kind"] = u.Scheme
rmd["version"] = si.Version
}
_, e := r.cli.RegisterInstance(vo.RegisterInstanceParam{
Ip: host,
Port: port,
ServiceName: si.Name + "." + u.Scheme,
Weight: r.opts.weight,
Enable: true,
Healthy: true,
Ephemeral: true,
Metadata: rmd,
ClusterName: r.opts.cluster,
GroupName: r.opts.group,
})
if e != nil {
return fmt.Errorf("RegisterInstance err %v,%v", e, endpoint)
}
}
return nil
}
// Deregister the registration.
func (r *NacosRegistry) Deregister(_ context.Context, service *registry.ServiceInstance) error {
for _, endpoint := range service.Endpoints {
u, err := url.Parse(endpoint)
if err != nil {
return err
}
host, port, err := net.ExtractHostPort(u.Host)
if err != nil {
return err
}
if _, err = r.cli.DeregisterInstance(vo.DeregisterInstanceParam{
Ip: host,
Port: port,
ServiceName: service.Name + "." + u.Scheme,
GroupName: r.opts.group,
Cluster: r.opts.cluster,
Ephemeral: true,
}); err != nil {
return err
}
}
return nil
}
// WithPrefix with prefix path.
func WithPrefix(prefix string) Option {
return func(o *options) { o.prefix = prefix }
}
// WithWeight with weight option.
func WithWeight(weight float64) Option {
return func(o *options) { o.weight = weight }
}
// WithCluster with cluster option.
func WithCluster(cluster string) Option {
return func(o *options) { o.cluster = cluster }
}
// WithGroup with group option.
func WithGroup(group string) Option {
return func(o *options) { o.group = group }
}
// WithDefaultKind with default kind option.
func WithDefaultKind(kind string) Option {
return func(o *options) { o.kind = kind }
}
1
https://gitee.com/tylf2018/go-micro-framework.git
git@gitee.com:tylf2018/go-micro-framework.git
tylf2018
go-micro-framework
go-micro-framework
e87e0c3d7074

搜索帮助