grpccli

package
v1.3.18 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 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

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

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 secure connections

func DialInsecure

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

DialInsecure unsafe connections

Types

type Option

type Option func(*options)

Option grpc dial options

func WithCredentials

func WithCredentials(credentials credentials.TransportCredentials) Option

WithCredentials set dial credentials

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 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 WithStreamInterceptors

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

WithStreamInterceptors set dial streamInterceptors

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout set dial timeout

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