grpcx

package
v0.0.0-...-00b4424 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

Server

服务端Grpc服务的实现,支持各种语言的grpc client访问。

1、 创建一个grpc server插件

创建一个grpc server插件,并把它加入到rpcx server的插件容器中。

	s := server.NewServer() // rpcx server
	gs := NewGrpcServer() // grpc server plugin
	s.Plugins.Add(gs) // add  grpc server plugin into plugins of rpcx

2、注册 rpcx 服务和 grpc 服务

在这个例子中,GreeterService既实现了rpcx的服务方法Greet,也实现了grpc服务helloworld.GreeterServer

  • 2.1 注册rpcx的代码如下:
err := s.Register(greetService, "")
  • 2.2 注册grpc服务如下:
gs.RegisterService(func(grpcServer *grpc.Server) {
		helloworld.RegisterGreeterServer(grpcServer, greetService)
})

3. 启动rpcx服务

go s.Serve("tcp", "127.0.0.1:0")

4. 启动grpc server 插件

err := gs.Start()

至此,服务端启动完成

grpc client

rpcx的客户端不变, grpc客户端可以使用任何你喜欢的语言去实现都可以。

比如使用Go语言访问这个服务:

    conn, err := grpc.Dial(s.Address().String(), grpc.WithInsecure(), grpc.WithTimeout(time.Second))
	
	defer conn.Close()
	c := helloworld.NewGreeterClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	r, err := c.SayHello(ctx, &helloworld.HelloRequest{Name: "smallnest"})

客户端

rpcx client支持访问各种编程语言实现的grpc服务,并能提供FailMode、熔断服务治理能力。

客户端注册Grpc支持能力

	// register CacheClientBuilder
	gcp := NewGrpcClientPlugin([]grpc.DialOption{grpc.WithInsecure()}, nil)
	client.RegisterCacheClientBuilder("grpc", gcp)

访问grpc服务

和访问rpcx服务一模一样,没有特殊的配置。

	// rpcx client
	d, _ := client.NewPeer2PeerDiscovery("grpc@"+lis.Addr().String(), "")
	opt := client.DefaultOption
	xclient := client.NewXClient("helloworld.Greeter", client.Failtry, client.RandomSelect, d, opt)
	defer xclient.Close()

	argv := &helloworld.HelloRequest{
		Name: "smallnest",
	}
	reply := &helloworld.HelloReply{}
	err = xclient.Call(context.Background(), "SayHello", argv, reply)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSupported              = errors.New("feature not supported")
	ErrClientNotRegistered       = errors.New("grpc client not registered")
	ErrGrpcMethodNotFound        = errors.New("grpc method not found")
	ErrReplyMustBePointer        = errors.New("reply must be pointer type")
	ErrGrpcClientBuilderNotFound = errors.New("grpc client builder not found")
	ErrGrpcReplyCannotSet        = errors.New("grpc reply can not be set")
)

Errors for grpc client.

Functions

This section is empty.

Types

type GrpcClient

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

GrpcClient is a grpc client wrapper and implements RPCClient interface.

func (*GrpcClient) Call

func (c *GrpcClient) Call(ctx context.Context, servicePath, serviceMethod string, argv interface{}, reply interface{}) error

Call invoke the grpc sevice.

func (*GrpcClient) Close

func (c *GrpcClient) Close() error

Close record this client closed.

func (*GrpcClient) Connect

func (c *GrpcClient) Connect(network, address string) error

Connect connects the server. not supported because the grpc.ClientConn has connected in case of init.

func (*GrpcClient) GetConn

func (c *GrpcClient) GetConn() net.Conn

GetConn returns underlying net.Conn. Always returns nil because grpc.ClientConn has not implement net.Conn.

func (*GrpcClient) Go

func (c *GrpcClient) Go(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) *client.Call

Go calls the grpc servics asynchronizously.

func (*GrpcClient) IsClosing

func (c *GrpcClient) IsClosing() bool

IsClosing return closed or not.

func (*GrpcClient) IsShutdown

func (c *GrpcClient) IsShutdown() bool

IsShutdown return closed or not.

func (*GrpcClient) RegisterServerMessageChan

func (c *GrpcClient) RegisterServerMessageChan(ch chan<- *protocol.Message)

RegisterServerMessageChan register stream chan. not supported.

func (*GrpcClient) RemoteAddr

func (c *GrpcClient) RemoteAddr() string

RemoteAddr returns the remote address.

func (*GrpcClient) SendRaw

func (c *GrpcClient) SendRaw(ctx context.Context, r *protocol.Message) (map[string]string, []byte, error)

SendRaw sends raw data. not supported.

func (*GrpcClient) UnregisterServerMessageChan

func (c *GrpcClient) UnregisterServerMessageChan()

UnregisterServerMessageChan unregister stream chan. not supported.

type GrpcClientPlugin

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

GrpcClientPlugin is used for managing rpcx clients for grpc protocol.

func NewGrpcClientPlugin

func NewGrpcClientPlugin(dialOpts []grpc.DialOption, callOpts []grpc.CallOption) *GrpcClientPlugin

NewGrpcClientPlugin creates a new GrpcClientPlugin.

func (*GrpcClientPlugin) DeleteCachedClient

func (c *GrpcClientPlugin) DeleteCachedClient(client client.RPCClient, k, servicePath, serviceMethod string)

DeleteCachedClient deletes an exited client.

func (*GrpcClientPlugin) FindCachedClient

func (c *GrpcClientPlugin) FindCachedClient(k, servicePath, serviceMethod string) client.RPCClient

FindCachedClient gets a cached client if exist.

func (*GrpcClientPlugin) GenerateClient

func (c *GrpcClientPlugin) GenerateClient(k, servicePath, serviceMethod string) (client client.RPCClient, err error)

GenerateClient generates an new grpc client.

func (*GrpcClientPlugin) SetCachedClient

func (c *GrpcClientPlugin) SetCachedClient(client client.RPCClient, k, servicePath, serviceMethod string)

SetCachedClient sets the cache client.

type GrpcServerPlugin

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

GrpcServerPlugin supports grpc services.

func NewGrpcServerPlugin

func NewGrpcServerPlugin() *GrpcServerPlugin

NewGrpcServerPlugin creates a new grpc server.

func (*GrpcServerPlugin) Close

func (s *GrpcServerPlugin) Close() error

Close closes the grpc server.

func (*GrpcServerPlugin) MuxMatch

func (s *GrpcServerPlugin) MuxMatch(m cmux.CMux)

MuxMatch splits grpc Listener.

func (*GrpcServerPlugin) RegisterService

func (s *GrpcServerPlugin) RegisterService(registerFunc func(grpcServer *grpc.Server))

RegisterService registers grpc service by this method.

func (*GrpcServerPlugin) Start

func (s *GrpcServerPlugin) Start() error

Start stats the grpc server.

Jump to

Keyboard shortcuts

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