ggrpc

package module
v0.0.0-...-221c9aa Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2019 License: MIT Imports: 14 Imported by: 0

README

grpc封装库

一个简单对GPRC做封装的库

安装

go get github.com/Visolleon/ggrpc
生成proto
syntax = "proto3"; // 指定proto版本
package proto;     // 指定包名

option go_package = "proto";

// 玩家信息结构
message PlayerStruct {
    // 用户ID
    int64 Uid = 1;
    // 用户Token
    string Token = 2;
    // 用户昵称
    string Name = 3;
}
protoc --go_out=plugins=grpc:. *.proto
使用protoc命令编译.proto文件:
  • -I 参数:指定import路径,可以指定多个 -I参数,按顺序查找,默认只查找当前目录
  • --go_out :golang编译支持,支持以下参数:
    • plugins=plugin1+plugin2 - 指定插件,目前只有grpc,即:plugins=grpc
    • M 参数 - 指定导入的.proto文件路径编译后对应的golang包名(不指定本参数默认就是.proto文件中import语句的路径)
    • import_prefix=xxx - 为所有import路径添加前缀,主要用于编译子目录内的多个proto文件,这个参数按理说很有用,尤其适用替代hello_http.proto编译时的M参数,但是实际使用时有个蛋疼的问题,自己尝试看看吧
    • import_path=foo/bar - 用于指定未声明package或go_package的文件的包名,最右面的斜线前的字符会被忽略
    • :编译文件路径 .proto文件路径(支持通配符)
    • 同一个包内包含多个.proto文件时使用通配符同时编译所有文件,单独编译每个文件会存在变量命名冲突,例如编译hello_http.proto那里所示

完整示例:

protoc --go_out=plugins=grpc,Mfoo/bar.proto=bar,import_prefix=,import_path=foo/bar:. ./*.proto
新建RPC服务器

ini配置文件设置

[rpc]
; rpc服务的唯一标识
UUID=Service_001
; rpc服务端口号
PORT = 51001
; 启用TLS认证,详细可查询grpc的OpenTLS
IsOpenTLS = true
KEY = keys/server.key
PEM = keys/server.pem
; 启用SIGN认证
IsUseSign = true
APPID = b659e4f4cea1ad1f011e48355c14667d
APPKEY = 37e267fe-f348-3cc1-85ff-ee111cf6e2b7
// 新建rpc Server
var server = ggrpc.InitServerFromIniConfig(config.Cfg)
// 注册RPC服务提供调用的类和方法
proto.RegisterBattleServer(server.GRPCServer, &service.UserService{})
// 启动RPC服务
server.Start()
调用RPC服务

RPC调用配置文件,支持集群,或可以根据uuid有目标性的调用

[
    {
        "uuid": "PushServer_001",
        "ip": "127.0.0.1",
        "port": 40041,
        "isOpenTLS": true,
        "key": "keys/server.key",
        "pem": "keys/server.pem",
        "tlsName": "MLDD.RPC",
        "isUseSign": true,
        "appId": "b659e4f4cea1ad1f011e48355c14667d",
        "appKey": "37e267fe-f348-3cc1-85ff-ee111cf6e2b7",
        "websocketURL": "127.0.0.1:8002"
    },
    ...
]
// 新建RPC集群调用(集群用于负载或者有目的性的调用)
var PushServerGroup = &ggrpc.ServerGroup{}
// 读取配置文件
err := PushServerGroup.Init(config.PushServerConfigFile)
if err != nil {
    log.Panicf("Load push server error, %v", err)
}
// 监控配置文件,当配置文件发生变化后,程序自动更新
PushServerGroup.GoWatchFile()

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	RPCInfo *RPCServerInfo // RPC信息
	// contains filtered or unexported fields
}

Client RPC客户端

func NewClient

func NewClient(rpcInfo *RPCServerInfo) *Client

NewClient 新建Client对象

func (*Client) Init

func (c *Client) Init(rpcInfo *RPCServerInfo)

Init 初始化

func (*Client) Invoke

func (c *Client) Invoke(hander InvokeHandler) error

Invoke 调用

func (*Client) RegInterceptor

func (c *Client) RegInterceptor(hander grpc.UnaryClientInterceptor)

RegInterceptor 注册拦截器

type InvokeHandler

type InvokeHandler func(*grpc.ClientConn) error

InvokeHandler 调用方法

type RPCServerInfo

type RPCServerInfo struct {
	UUID string `json:"uuid"` // RPC服务UUID,用于服务器判别

	IP   string `json:"ip"`   // PRC IP地址
	Port int    `json:"port"` // RPC端口号

	IsOpenTLS bool   `json:"isOpenTLS"` // 是否启用TLS认证,如果启用,则必须要指明公钥Key和私钥Pem的文件地址
	Key       string `json:"key"`       // 公钥,供服务端使用
	Pem       string `json:"pem"`       // 私钥
	TLSName   string `json:"tlsName"`   // 加密名,供客户端使用

	IsUseSign bool   `json:"isUseSign"` // 是否启用MD5签名认证,如果启用,则必须要设置AppID和AppKey
	AppID     string `json:"appId"`     // 自定义验证APPID,暂时固定值,以后扩展使用
	AppKey    string `json:"appKey"`    // 自定义验证APPKEY
}

RPCServerInfo RPC服务配置实体类

var (
	// RPCServerSetting RPC Server Setting
	RPCServerSetting *RPCServerInfo
)

func LoadRPCServerConfig

func LoadRPCServerConfig(cfg *ini.File) *RPCServerInfo

LoadRPCServerConfig 设置RPC服务配置信息

type RegHandler

type RegHandler func(*grpc.Server)

RegHandler RPC注册方法

type Server

type Server struct {
	RPCInfo    *RPCServerInfo // RPC信息
	GRPCServer *grpc.Server
	Listener   net.Listener
}

Server RPC服务对象

func InitServerFromIniConfig

func InitServerFromIniConfig(cfg *ini.File) *Server

InitServerFromIniConfig 初始化RPC服务器

func NewGPRCServer

func NewGPRCServer(rpcInfo *RPCServerInfo) *Server

NewGPRCServer 新建RPC服务对象

func (*Server) GetUUID

func (s *Server) GetUUID() string

GetUUID 获取Server的UUID

func (*Server) Init

func (s *Server) Init(rpcInfo *RPCServerInfo)

Init 初始化RPC服务

func (*Server) Register

func (s *Server) Register(regFunc RegHandler)

Register 注册函数对象 @Example:

server.Register(func(grpcServer *grpc.Server) {
	pb.RegisterHelloServer(grpcServer, &service.HelloService{})
})

func (*Server) Start

func (s *Server) Start()

Start 启动RPC服务

type ServerGroup

type ServerGroup struct {
	InfoList []*RPCServerInfo // RPC信息库列表

	ServerList []*Client // RPC Server列表
	// contains filtered or unexported fields
}

ServerGroup RPC服务器群管理信息

func (*ServerGroup) GetByUUID

func (group *ServerGroup) GetByUUID(uuid string) (*Client, error)

GetByUUID 根据UIID来获取RPC服务器信息

func (*ServerGroup) GoWatchFile

func (group *ServerGroup) GoWatchFile()

GoWatchFile 监控配置文件

func (*ServerGroup) Init

func (group *ServerGroup) Init(configFile string) error

Init 初始化配置

func (*ServerGroup) InvokeAll

func (group *ServerGroup) InvokeAll(hander InvokeHandler) error

InvokeAll RPC集群群体调用

func (*ServerGroup) InvokeByUUID

func (group *ServerGroup) InvokeByUUID(uuid string, hander InvokeHandler) error

InvokeByUUID 调用

func (*ServerGroup) InvokeRand

func (group *ServerGroup) InvokeRand(hander InvokeHandler) error

InvokeRand 随机调用

func (*ServerGroup) RandOne

func (group *ServerGroup) RandOne() (*Client, error)

RandOne 随机选取一个服务

func (*ServerGroup) RegInterceptor

func (group *ServerGroup) RegInterceptor(hander grpc.UnaryClientInterceptor)

RegInterceptor 全体注册拦截器

Jump to

Keyboard shortcuts

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