srd

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: MIT Imports: 17 Imported by: 0

README

SRD

基于etcd的注册与发现

注册
package main

import (
	"flag"
	"github.com/zituocn/srd"
	"log"
)

func main() {
	flag.Parse()
	_, err := srd.NewRegister(&srd.RegisterOption{
		EtcdEndpoints: []string{"192.168.0.101:2379"},
		Lease:         3,
		Schema:        "gk100-cache",
		Key:           "pod-ip",
		Val:           "10.10.10.2:10003",
	})
	if err != nil {
		log.Fatal(err)
	}
	select {}
}

发现
package main

import (
	"fmt"
	"github.com/zituocn/srd"
	"log"
)

func main() {
	dis, err := srd.NewDiscovery("gk100-cache", "pod-ip", []string{"192.168.0.101:2379"}, 3)
	if err != nil {
		log.Fatal(err)
	}
	err = dis.Builder()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(dis.GetValues())
}

GRPC服务端
package main

import (
	"context"
	"github.com/zituocn/logx"
	"github.com/zituocn/srd"
	"github.com/zituocn/srd/demo/pb"
	"log"
)

var (
	etcdEndPoints = []string{"192.168.0.101:2379"}
	ServerName    = "user-service"
	Port          = 1111
)

func init() {
	gs, err := srd.NewGrpcServer(&srd.GrpcServerOption{
		ServerName:    ServerName,    //服务名
		Port:          Port,          //端口
		EtcdEndpoints: etcdEndPoints, //etcd连接配置
		Lease:         5,             //etcd lease时间
		IsRegister:    true,          //是否注册到etcd
	})

	if err != nil {
		log.Fatal(err)
	}

	pb.RegisterUserServer(gs.Server, &UserService{})

	gs.Run()
}

func main() {
	select {}
}

type UserService struct {
}

func (s *UserService) GetUser(ctx context.Context, req *pb.UserRequest) (*pb.UserResponse, error) {
	logx.Debugf("from %d", req.Uid)
	return &pb.UserResponse{
		Uid:      req.Uid,
		Username: "zituocn",
		Phone:    "13888889999",
		Address:  "chengdu in china",
	}, nil
}

GRPC客户端
package main

import (
	"github.com/zituocn/gow"
	"github.com/zituocn/srd"
	"github.com/zituocn/srd/demo/pb"
	"log"
)

var (
	etcdEndPoints = []string{"127.0.0.1:2379"}
	serverName    = "user-service"
)

func main() {
	r := gow.Default()
	r.GET("/user/{id}", GetUser)
	r.Run()
}

func GetUser(c *gow.Context) {
	id, _ := c.ParamInt("id")
	client, err := srd.NewGrpcClient(&srd.GrpcClientOption{
		ServerName:   serverName,
		EtcdEndpoint: etcdEndPoints,
		ClientName:   "test",
		ConnType:     srd.BalanceType,
	})

	if err != nil {
		log.Fatal(err)
	}

	conn, ctx, err := client.GetConn()
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	userClient := pb.NewUserClient(conn)

	req := &pb.UserRequest{
		Uid: int32(id),
	}
	resp, err := userClient.GetUser(ctx, req)
	if err != nil {
		c.DataJSON(1, err.Error())
		return
	}
	c.DataJSON(resp)
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocalIP

func GetLocalIP() (string, error)

GetLocalIP get the IP address of the current system

physical or virtual machine

Types

type ConnType

type ConnType uint
const (

	// DefaultType 传统模式的grpc连接方式
	DefaultType ConnType = iota + 1

	// BalanceType 负载均衡的grpc连接方式
	//	需要使用etcd做为服务注册与发现
	BalanceType
)

type Discovery

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

func NewDiscovery

func NewDiscovery(schema, key string, etcdEndpoints []string, dialTimeout int) (*Discovery, error)

func (*Discovery) Build

func (*Discovery) Builder

func (d *Discovery) Builder() error

func (*Discovery) Close

func (d *Discovery) Close()

func (*Discovery) DelDiscoveryServiceList

func (d *Discovery) DelDiscoveryServiceList(key string)

func (*Discovery) DelServiceList

func (d *Discovery) DelServiceList(key string)

func (*Discovery) GetValues

func (d *Discovery) GetValues() []string

GetValues returns Values

func (*Discovery) ResolveNow

func (d *Discovery) ResolveNow(options resolver.ResolveNowOptions)

func (*Discovery) Scheme

func (d *Discovery) Scheme() string

func (*Discovery) SetDiscoveryServiceList

func (d *Discovery) SetDiscoveryServiceList(key, val string)

func (*Discovery) SetServiceList

func (d *Discovery) SetServiceList(key, val string)

type GrpcClient

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

func NewGrpcClient

func NewGrpcClient(opt *GrpcClientOption) (*GrpcClient, error)

func (*GrpcClient) GetConn

func (c *GrpcClient) GetConn() (*grpc.ClientConn, context.Context, error)

GetConn returns grpc clientConn,ctx and error

type GrpcClientOption

type GrpcClientOption struct {

	// SererAddr grpc服务的地址
	ServerAddr string

	// EtcdEndpoint etcd地址
	EtcdEndpoint []string

	// ServerName 服务名
	ServerName string

	// ClientName 客户端名称
	ClientName string

	// ConnType 连接类型
	ConnType ConnType
}

type GrpcServer

type GrpcServer struct {
	Listener net.Listener

	Server *grpc.Server

	ServerName string
	// contains filtered or unexported fields
}

func NewGrpcServer

func NewGrpcServer(opt *GrpcServerOption) (*GrpcServer, error)

NewGrpcServer 返回新的grpc register

func (*GrpcServer) Run

func (s *GrpcServer) Run()

Run 运行

type GrpcServerOption

type GrpcServerOption struct {

	// grpc ServerName 服务名
	ServerName string

	// 端口
	Port int

	// IP地址
	IP string

	// EtcdEndpoints  etcd地址,如果为多个地址时,需要集群部署etcd
	EtcdEndpoints []string

	// Lease etcd 租约续期时间
	Lease int64

	// IsRegister 是否注册到etcd
	//	为true时,可不填写传入ip地址
	IsRegister bool
}

GrpcServerOption grpc register 参数

type Register

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

func NewRegister

func NewRegister(opt *RegisterOption) (*Register, error)

func (*Register) Close

func (s *Register) Close() error

func (*Register) ListenLeaseRespChan

func (s *Register) ListenLeaseRespChan()

type RegisterOption

type RegisterOption struct {

	// EtcdEndpoints  etcd hosts
	EtcdEndpoints []string

	//Lease etcd lease
	Lease int64

	// DialTimeout etcd dialTimeout second
	DialTimeout time.Duration

	// Schema  etd key prefix
	Schema string

	Key string
	Val string
}

Jump to

Keyboard shortcuts

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