grpc

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 8 Imported by: 0

README

grpc

client

  1. client是对grpc client的封装, 主要关注于默认的参数的处理和拦截器的加载
  2. client默认使用p2c的负载均衡器, 同时根据环境变量选择是否从注册中心加载服务, 连接服务
  3. 使用
package main

import (
	"context"
	"log"
	"sync"

	"github.com/gowins/dionysus/example/grpc/hw"
	"github.com/gowins/dionysus/grpc/client"
	xlog "github.com/gowins/dionysus/log"
	"google.golang.org/grpc"
)

func main() {
	xlog.Setup(xlog.SetProjectName("grpc-client"))
	var wg sync.WaitGroup
	for i := 0; i < 3; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			conn, err := client.New(":8081")
			if err != nil {
				log.Fatalf("did not connect: %v", err)
			}
			defer conn.Close()
			c := hw.NewGreeterClient(conn)
			// Contact the server and print out its response.
			r, err := c.SayHello(context.Background(), &hw.HelloRequest{Name: "nameing"})
			if err != nil {
				log.Printf("could not greet: %v", err)
				return
			}
			log.Printf("Greeting: %s", r.GetMessage())
		}()
	}
	wg.Wait()
}
  1. 运行例子

go run example/grpc/client/main.go

  1. 熔断
clientinterceptors.BreakerUnary(hystrix.CommandConfig{})
  • 若传递空的hystrix.CommandConfig,则所有的熔断配置走hystrix默认的 配置,若有自定义配置,则传非空hystrix.CommandConfig
  • 可选参数,针对特定的rpc方法有自定义配置的,
ghystrix.HystrixCfg{Name: "/hw.Greeter/SayHello", Cfg: hystrix.CommandConfig{MaxConcurrentRequests: 3}},

Name字段必须是: /服务名/RPC方法名

server

  1. server 是对grpc server的封装
  2. server包含了默认参数处理, 拦截器加载, 服务注册逻辑和启动关闭流程
  3. 使用
package main

import (
	"context"
	"log"
	"time"

	"github.com/gowins/dionysus"
	"github.com/gowins/dionysus/cmd"
	"github.com/gowins/dionysus/example/grpc/hw"
	"github.com/gowins/dionysus/grpc/server"
	"github.com/gowins/dionysus/grpc/serverinterceptors"
)

type gserver struct {
	hw.UnimplementedGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *gserver) SayHello(ctx context.Context, in *hw.HelloRequest) (*hw.HelloReply, error) {
	log.Printf("Received: %v", in.GetName())
	return &hw.HelloReply{Message: "Hello " + in.GetName()}, nil
}

func main() {
	dio := dionysus.NewDio()
	cfg := server.DefaultCfg
	cfg.Address = ":8081"
	c := cmd.NewGrpcCmd(cmd.WithCfg(cfg))
	c.EnableDebug()
	c.AddUnaryServerInterceptors(serverinterceptors.TimeoutUnary(10 * time.Second))
	// recover interceptor
	c.AddUnaryServerInterceptors(serverinterceptors.RecoveryUnary(serverinterceptors.DefaultRecovery()))
	c.AddStreamServerInterceptors(serverinterceptors.RecoveryStream(serverinterceptors.DefaultRecovery()))
	// tacing interceptor
	c.AddUnaryServerInterceptors(serverinterceptors.OpenTracingUnary())
	c.AddStreamServerInterceptors(serverinterceptors.OpenTracingStream())
	// register grpc service
	c.RegisterGrpcService(hw.RegisterGreeterServer, &gserver{})
	dio.DioStart("grpc", c)
}
  1. 运行例子

go run example/grpc/main.go grpc

健康检查介绍

https://github.com/grpc/grpc/blob/master/doc/health-checking.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetClient

func GetClient(service string, opts ...grpc.DialOption) (*grpc.ClientConn, error)

func NewClient

func NewClient(service string, opts ...grpc.DialOption) (*grpc.ClientConn, error)

func SetLog

func SetLog(log logger.Logger)

func WithDefaultClientStreamInterceptors

func WithDefaultClientStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) grpc.DialOption

func WithDefaultClientUnaryInterceptors

func WithDefaultClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) grpc.DialOption

Types

type CallOption

type CallOption = grpc.CallOption

type ClientConn

type ClientConn = grpc.ClientConn

type ClientStream

type ClientStream = grpc.ClientStream

type EmptyServerOption

type EmptyServerOption = grpc.EmptyServerOption

type EndPoints

type EndPoints []string

type Server

type Server = server.GrpcServer

func NewServer

func NewServer() *Server

type ServerOption

type ServerOption = grpc.ServerOption

type StreamDesc

type StreamDesc = grpc.StreamDesc

type StreamServerInterceptor

type StreamServerInterceptor = grpc.StreamServerInterceptor

type Streamer

type Streamer = grpc.Streamer

type UnaryInvoker

type UnaryInvoker = grpc.UnaryInvoker

type UnaryServerInterceptor

type UnaryServerInterceptor = grpc.UnaryServerInterceptor

Directories

Path Synopsis
balancer
Package registry is an interface for service discovery
Package registry is an interface for service discovery
etcdv3
copy from go-plugins/registry/etcdv3 Package etcdv3 provides an etcd version 3 registry
copy from go-plugins/registry/etcdv3 Package etcdv3 provides an etcd version 3 registry

Jump to

Keyboard shortcuts

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