grpccli

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 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 "github.com/zhufuyi/sponge/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 WithDiscoveryInsecure added in v1.8.0

func WithDiscoveryInsecure(b bool) Option

WithDiscoveryInsecure setting discovery insecure

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 added in v1.3.2

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 added in v1.3.20

func WithOneWaySecure(serverName string, certFile string) Option

WithOneWaySecure set one-way secure

func WithSecure added in v1.3.20

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(d time.Duration) Option

WithTimeout set dial timeout

func WithToken added in v1.3.20

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

WithToken set token

func WithTwoWaySecure added in v1.3.20

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