mrpc

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: Apache-2.0 Imports: 7 Imported by: 1

README

mRPC

gRPC 的封装集合 大量参考 zRPC

运行

type depositServer struct{}

func newDepositServer() *depositServer {
	return &depositServer{}
}

func (s *depositServer) Deposit(ctx context.Context, in *mock.DepositRequest) (*mock.DepositResponse, error) {
	fmt.Printf("mrpc client in %+v \n", in)
	return &mock.DepositResponse{
		Ok: true,
	}, nil
}

func TestMrpc(t *testing.T) {
	// start server
	srv := newDepositServer()

	sc := RpcServerConf{
		Addr:    "localhost:8081",
		Timeout: 60,
	}
	s := MustNewServer(sc, func(server *grpc.Server) {
		mock.RegisterDepositServiceServer(server, srv)
	})

	defer s.Stop()

	fmt.Printf("starting rpc server at %s...\n", sc.Addr)

	go s.Start()

	// start client
	cc := RpcClientConf{
		Target:  "localhost:8081",
		Timeout: 60,
	}
	deposit := mock.NewDepositServiceClient(MustNewClient(cc).Conn())

	fmt.Printf("connection mrpc server %s... \n", cc.Target)

	resp, err := deposit.Deposit(context.Background(), &mock.DepositRequest{
		Amount: 10,
	})
	if err != nil {
		fmt.Printf("mrpc deposit err %v \n", err)
		return
	}

	fmt.Printf("mrpc deposit succ %+v \n", resp)
}

案例

License

Apache License Version 2.0, http://www.apache.org/licenses/

Documentation

Index

Constants

View Source
const (
	DevMode     = "dev"
	TestMode    = "test"
	RtMode      = "rt"
	PreMode     = "pre"
	ProMode     = "pro"
	ReleaseMode = "release"
)

Variables

View Source
var (
	WithDialOption = internal.WithDialOption

	WithNonBlock = internal.WithNonBlock

	WithStreamClientInterceptor = internal.WithStreamClientInterceptor

	WithTimeout = internal.WithTimeout

	WithTransportCredentials = internal.WithTransportCredentials

	WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
)

Functions

func DontLogContextForMethod

func DontLogContextForMethod(method string)

func SetClientSlowThreshold

func SetClientSlowThreshold(threshold time.Duration)

func SetServerSlowThreshold

func SetServerSlowThreshold(threshold time.Duration)

Types

type Client

type Client internal.Client

func MustNewClient

func MustNewClient(c RpcClientConf, options ...ClientOption) Client

func NewClient

func NewClient(c RpcClientConf, options ...ClientOption) (Client, error)

func NewClientWithTarget

func NewClientWithTarget(target string, opts ...ClientOption) (Client, error)

type ClientOption

type ClientOption = internal.ClientOption

type RpcClient

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

func (*RpcClient) Conn

func (rc *RpcClient) Conn() *grpc.ClientConn

type RpcClientConf

type RpcClientConf struct {
	Target   string `json:"Target"`
	NonBlock bool   `json:"NonBlock"`
	Timeout  int64  `json:"Timeout"`
}

type RpcServer

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

func MustNewServer

func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer

func NewServer

func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error)

func (*RpcServer) AddOptions

func (rs *RpcServer) AddOptions(options ...grpc.ServerOption)

func (*RpcServer) AddStreamInterceptors

func (rs *RpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor)

func (*RpcServer) AddUnaryInterceptors

func (rs *RpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor)

func (*RpcServer) Start

func (rs *RpcServer) Start()

Start starts the RpcServer. Graceful shutdown is enabled by default. Use proc.SetTimeToForceQuit to customize the graceful shutdown period.

func (*RpcServer) Stop

func (rs *RpcServer) Stop()

type RpcServerConf

type RpcServerConf struct {
	ServiceConf `mapstructure:",squash"`
	Addr        string `json:"Addr"`
	Timeout     int64  `json:"Timeout"`
}

type ServiceConf

type ServiceConf struct {
	Name       string `json:"Name"`
	Mode       string `json:"Mode"`
	MetricsUrl string `json:"MetricsUrl"`
	Prometheus string `json:"Prometheus"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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