grpccli

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 12 Imported by: 0

README

grpccli

grpc client with support for service discovery, logging, load balancing, trace, metrics, retries, circuit breaker.

Example of use

    import "gitee.com/yzsunjianguo/common_pkg/grpc/grpccli"

func grpcClientExample() serverNameV1.UserExampleServiceClient {
	err := config.Init(third_party.Path("../config/conf.yml"))
	if err != nil {
		panic(err)
	}

	var discovery registry.Discovery
	var endpoint = fmt.Sprintf("127.0.0.1:%d", config.Get().Grpc.Port)
	ctx, _ := context.WithTimeout(context.Background(), time.Second*3)
	if config.Get().App.EnableRegistryDiscovery {
		endpoint = "discovery:///" + config.Get().App.Name
		discovery = discoveryETCD(config.Get().Etcd.Addrs)
	}
	conn, err := grpccli.DialInsecure(ctx, endpoint,
		grpccli.WithEnableLog(logger.Get()),
		grpccli.WithDiscovery(discovery),
        //grpccli.WithEnableCircuitBreaker(),		
		//grpccli.WithEnableTrace(),
		//grpccli.WithEnableLoadBalance(),
		//grpccli.WithEnableRetry(),
		//grpccli.WithEnableMetrics(),
	)
	if err != nil {
		panic(err)
	}

	return serverNameV1.NewUserExampleServiceClient(conn)
}

func discoveryETCD(endpoints []string) registry.Discovery {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints,
		DialTimeout: 10 * time.Second,
		DialOptions: []grpc.DialOption{
			grpc.WithBlock(),
			grpc.WithTransportCredentials(insecure.NewCredentials()),
		},
	})
	if err != nil {
		panic(err)
	}

	return etcd.New(cli)
}

Documentation

Overview

Package grpccli is grpc client with support for service discovery, logging, load balancing, trace, metrics, retries, circuit breaker.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(ctx context.Context, endpoint string, opts ...Option) (*grpc.ClientConn, error)

Dial to grpc server

Types

type Option

type Option func(*options)

Option grpc dial options

func WithDialOptions

func WithDialOptions(dialOptions ...grpc.DialOption) Option

WithDialOptions set dial options

func WithDiscovery

func WithDiscovery(discovery registry.Discovery) Option

WithDiscovery set dial discovery

func WithEnableCircuitBreaker

func WithEnableCircuitBreaker() Option

WithEnableCircuitBreaker enable circuit breaker

func WithEnableLoadBalance

func WithEnableLoadBalance() Option

WithEnableLoadBalance enable load balance

func WithEnableLog

func WithEnableLog(log *zap.Logger) Option

WithEnableLog enable log

func WithEnableMetrics

func WithEnableMetrics() Option

WithEnableMetrics enable metrics

func WithEnableRequestID

func WithEnableRequestID() Option

WithEnableRequestID enable request id

func WithEnableRetry

func WithEnableRetry() Option

WithEnableRetry enable registry

func WithEnableTrace

func WithEnableTrace() Option

WithEnableTrace enable trace

func WithOneWaySecure

func WithOneWaySecure(serverName string, certFile string) Option

WithOneWaySecure set one-way secure

func WithSecure

func WithSecure(t string, serverName string, caFile string, certFile string, keyFile string) Option

WithSecure support setting one-way or two-way secure

func WithStreamInterceptors

func WithStreamInterceptors(streamInterceptors ...grpc.StreamClientInterceptor) Option

WithStreamInterceptors set dial streamInterceptors

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout set dial timeout

func WithToken

func WithToken(enable bool, appID string, appKey string) Option

WithToken set token

func WithTwoWaySecure

func WithTwoWaySecure(serverName string, caFile string, certFile string, keyFile string) Option

WithTwoWaySecure set two-way secure

func WithUnaryInterceptors

func WithUnaryInterceptors(unaryInterceptors ...grpc.UnaryClientInterceptor) Option

WithUnaryInterceptors set dial unaryInterceptors

Jump to

Keyboard shortcuts

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