Kitex 提供的服务注册与发现 etcd 拓展

在这里插入图片描述

📕作者简介: 过去日记,致力于Java、GoLang,Rust等多种编程语言,热爱技术,喜欢游戏的博主。
📘相关专栏Rust初阶教程go语言基础系列、spring教程等,大家有兴趣的可以看一看
📙Java并发编程系列,设计模式系列、go web开发框架 系列正在发展中,喜欢Java,GoLang,Rust,的朋友们可以关注一下哦!


@[TOC] # Kitex 提供的服务注册与发现 etcd 拓展 ## etcd介绍 etcd 是一个高度可用的、分布式、一致性的键值存储系统,主要用于服务发现、配置共享、协调分布式系统状态等场景。它是使用 Go 语言编写的,并采用 Raft 算法保证数据的强一致性。etcd 在云原生生态系统中扮演着至关重要的角色,特别是在 Kubernetes(K8s)中作为其元数据存储和集群协调的核心组件。 ## docker 安装 ```yaml docker etcd: image: bitnami/etcd:3.5 container_name: etcd ports: - 2379:2379 - 2380:2380 volumes: - ./etcd/data:/bitnami/etcd-data environment: - TZ=Asia/Shanghai - ALLOW_NONE_AUTHENTICATION=yes - ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379 ``` ## 代码实现 ### 安装包 >go get github.com/kitex-contrib/registry-etcd

服务注册

注册函数

提供了三个创建 Registry 的函数

NewEtcdRegistry
NewEtcdRegistry 使用 etcd 创建一个新的服务注册中心,需要传入端点值。可自定义服务注册中心配置,配置详情见 Option。
函数签名:

func NewEtcdRegistry(endpoints []string, opts …Option) (registry.Registry, error)

NewEtcdRegistryWithAuth
NewEtcdRegistryWithAuth 创建服务注册中心需要传入 auth 参数。
函数签名:

func NewEtcdRegistryWithAuth(endpoints []string, username, password string) (registry.Registry, error)

NewEtcdRegistryWithRetry
NewEtcdRegistryWithRetry 创建服务注册中心传入自定义 Retry 配置。
函数签名:

func NewEtcdRegistryWithRetry(endpoints []string, retryConfig *retry.Config, opts …Option) (registry.Registry, error)

使用 NewRetryConfig(opts …Option) *Config 生成 Retry 配置,配置详情见 Option。

代码示例

package main

import (
	"github.com/cloudwego/kitex/pkg/rpcinfo"
	"github.com/cloudwego/kitex/server"
	etcd "github.com/kitex-contrib/registry-etcd"
	"log"
	"mykitex/kitex_gen/example/shop/item/itemservice"
	"net"
)

func main() {
	// 使用时请传入真实 etcd 的服务地址,本例中为 127.0.0.1:2379
	r, err := etcd.NewEtcdRegistry([]string{"127.0.0.1:2379"})
	if err != nil {
		log.Fatal(err)
	}

	addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8890")
	svr := itemservice.NewServer(new(ItemServiceImpl),
		server.WithServiceAddr(addr),
		// 指定 Registry 与服务基本信息
		server.WithRegistry(r),
		server.WithServerBasicInfo(
			&rpcinfo.EndpointBasicInfo{
				ServiceName: "example.shop.item",
			},
		),
	)

	err = svr.Run()

	if err != nil {
		log.Println(err.Error())
	}
}
Option

Etcd 拓展在服务注册部分中提供了 option 配置。

WithTLSOpt
Etcd 扩展提供了 WithTLSOpt 用于帮助用户配置 Etcd 中的 TLS 选项。
函数签名:

func WithTLSOpt(certFile, keyFile, caFile string) Option

WithAuthOpt
Etcd 扩展提供了 WithAuthOpt 用于帮助用户配置 Etcd 中的 Username 和 Password 选项。
函数签名:

func WithAuthOpt(username, password string) Option

WithDialTimeoutOpt
Etcd 扩展提供了 WithTimeoutOpt 用于帮助用户配置连接超时时间。

func WithDialTimeoutOpt(dialTimeout time.Duration) Option

Retry
在服务注册到 etcd 之后,它会定期检查服务的状态。如果发现任何异常状态,它将尝试重新注册服务。observeDelay 是正常情况下检查服务状态的延迟时间,而 retryDelay 是断开连接后尝试注册服务的延迟时间。

默认配置

配置名 默认值 描述
WithMaxAttemptTimes(maxAttemptTimes uint) Option 5 用于设置最大尝试次数,如果为 0,则表示无限尝试
WithObserveDelay(observeDelay time.Duration) Option 30 * time.Second 用于设置正常连接条件下检查服务状态的延迟时间
WithRetryDelay(t time.Duration) Option 10 * time.Second 用于设置断开连接后重试的延迟时间

服务发现

发现函数

NewEtcdResolver
NewEtcdResolver 使用 etcd 创建一个新的服务发现中心,需要传入端点值。可自定义服务发现中心配置,配置详情见 Option。
函数签名:

func NewEtcdResolver(endpoints []string, opts …Option) (discovery.Resolver, error)

NewEtcdResolverWithAuth
NewEtcdResolverWithAuth 服务发现中心,需要传入 Auth 参数。
函数签名:

func NewEtcdResolverWithAuth(endpoints []string, username, password string) (discovery.Resolver, error)

代码示例

package main

import (
	"context"
	"github.com/cloudwego/kitex/client"
	"github.com/cloudwego/kitex/pkg/rpcinfo"
	etcd "github.com/kitex-contrib/registry-etcd"
	"log"
	"mykitex/kitex_gen/example/shop/item"
	"mykitex/kitex_gen/example/shop/item/itemservice"
	"time"
)

func main() {
	// 使用时请传入真实 etcd 的服务地址,本例中为 127.0.0.1:2379
	r, err := etcd.NewEtcdResolver([]string{"127.0.0.1:2379"})
	if err != nil {
		log.Fatal(err)
	}
	// 指定 Resolve
	cl, err := itemservice.NewClient("example.shop.item",
		client.WithResolver(r),
		client.WithClientBasicInfo(
			&rpcinfo.EndpointBasicInfo{
				ServiceName: "example.shop.item",
			},
		),
	)
	for {
		if err != nil {
			log.Println("发现服务失败")
		}
		p, _ := cl.GetItem(context.Background(), &item.GetItemReq{Id: 1})
		log.Println(p)
		time.Sleep(time.Second)
	}

}
Option

Etcd 拓展在服务发现部分中提供了 option 配置。

WithTLSOpt
Etcd 扩展提供了 WithTLSOpt 用于帮助用户配置 Etcd 中的TLS选项。
函数签名:

func WithTLSOpt(certFile, keyFile, caFile string) Option

WithAuthOpt
Etcd 扩展提供了WithAuthOpt用于帮助用户配置 Etcd 中的Username和Password选项。
函数签名:

func WithAuthOpt(username, password string) Option

WithDialTimeoutOpt
Etcd 扩展提供了WithTimeoutOpt用于帮助用户配置连接超时时间。
func WithDialTimeoutOpt(dialTimeout time.Duration) Option

代码仓库

https://github.com/onenewcode/mykitex

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 14:46:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 14:46:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 14:46:04       82 阅读
  4. Python语言-面向对象

    2024-03-30 14:46:04       91 阅读

热门阅读

  1. Spring中 Bean生命周期总结

    2024-03-30 14:46:04       43 阅读
  2. baseDao增删改查.

    2024-03-30 14:46:04       41 阅读
  3. 探讨Spring Boot的自动配置原理

    2024-03-30 14:46:04       53 阅读