初始化项目
This commit is contained in:
2
trunk/goutil/grpc-util/.gitignore
vendored
Normal file
2
trunk/goutil/grpc-util/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
Log/*
|
||||
logs/*
|
||||
63
trunk/goutil/grpc-util/client/client.go
Normal file
63
trunk/goutil/grpc-util/client/client.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
var (
|
||||
// 缓存连接 key:rpc地址 value:连接指针对象
|
||||
rpcConMap sync.Map
|
||||
|
||||
// 连接超时时间
|
||||
defaultTimeOut = time.Second * 1
|
||||
)
|
||||
|
||||
// GetClientConn
|
||||
// @description: 获取grpc连接对象
|
||||
// parameter:
|
||||
// @rpcAddres:连接地址
|
||||
// return:
|
||||
// @*grpc.ClientConn:连接对象
|
||||
// @error:错误对象
|
||||
func GetClientConn(rpcAddres string) (*grpc.ClientConn, error) {
|
||||
// 从缓存获取
|
||||
if con1, isok := rpcConMap.Load(rpcAddres); isok {
|
||||
return con1.(*grpc.ClientConn), nil
|
||||
}
|
||||
|
||||
// 构造新对象
|
||||
//ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*1))
|
||||
var ctx, _ = context.WithTimeout(context.Background(), defaultTimeOut)
|
||||
con, err := grpc.DialContext(ctx, rpcAddres, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 加入缓存
|
||||
rpcConMap.Store(rpcAddres, con)
|
||||
|
||||
return con, nil
|
||||
}
|
||||
|
||||
// ClearClientConn
|
||||
// @description: 根据连接地址,删除缓存的连接对象
|
||||
// parameter:
|
||||
// @rpcAddres:连接地址
|
||||
// return:
|
||||
func ClearClientConn(rpcAddres string) {
|
||||
rpcConMap.Delete(rpcAddres)
|
||||
}
|
||||
|
||||
// SetDefaultTimeOut
|
||||
// @description: 设置连接的默认超时
|
||||
// parameter:
|
||||
// @outTime:超时时间
|
||||
// return:
|
||||
func SetDefaultTimeOut(outTime time.Duration) {
|
||||
defaultTimeOut = outTime
|
||||
}
|
||||
8
trunk/goutil/grpc-util/client/doc.go
Normal file
8
trunk/goutil/grpc-util/client/doc.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// ************************************
|
||||
// @package: client
|
||||
// @description: grpc客户端辅助包
|
||||
// @author: byron
|
||||
// @revision history:
|
||||
// @create date: 2022-01-19 16:50:24
|
||||
// ************************************
|
||||
package client
|
||||
29
trunk/goutil/grpc-util/client/readme.md
Normal file
29
trunk/goutil/grpc-util/client/readme.md
Normal file
@@ -0,0 +1,29 @@
|
||||
client简单封装了grpc相关调用方法,缓存了connection连接
|
||||
整体文档入口请参考:[传送门](../readme.md)
|
||||
|
||||
## 使用方式如下:
|
||||
|
||||
### 1.导入包
|
||||
```go
|
||||
import (
|
||||
"vast.com/goutil/grpc-util/client"
|
||||
)
|
||||
```
|
||||
|
||||
### 2.获取连接
|
||||
```go
|
||||
con, err := client.GetClientConn(host)
|
||||
```
|
||||
|
||||
### 3.清理某个连接
|
||||
```go
|
||||
client.ClearClientConn("ip:port")
|
||||
```
|
||||
|
||||
### 4.设置连接的默认超时时间
|
||||
```go
|
||||
client.SetDefaultTimeOut(time.Second * 3)
|
||||
```
|
||||
注意:
|
||||
1.设置后对后续创建的连接生效,已经创建的不生效
|
||||
2.默认的超时 时间为1s
|
||||
20
trunk/goutil/grpc-util/readme.md
Normal file
20
trunk/goutil/grpc-util/readme.md
Normal file
@@ -0,0 +1,20 @@
|
||||
grpc-util做了如下工作:
|
||||
|
||||
### server(废弃):
|
||||
|
||||
1.实现服务的启动和相关方法的注册
|
||||
2.提供了公共对象和接口的实现
|
||||
3.提供公共req对象到res对象build辅助方法
|
||||
具体参考server文档 [传送门](server/readme.md)
|
||||
|
||||
### client:
|
||||
|
||||
1.缓存了grpc连接对象,使其可复用(可单独使用)
|
||||
2.基于提供的公共对象和接口,提供了grpc的4种基本用法的调用(建议需要搭配server使用)
|
||||
具体参考client文档 [传送门](client/readme.md)
|
||||
|
||||
### util:
|
||||
|
||||
1.提供pb相关辅助方法
|
||||
具体请直接看代码
|
||||
|
||||
135
trunk/goutil/grpc-util/util/pbUtil.go
Normal file
135
trunk/goutil/grpc-util/util/pbUtil.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc/peer"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
moOpt protojson.MarshalOptions
|
||||
uoOpt protojson.UnmarshalOptions
|
||||
)
|
||||
|
||||
func init() {
|
||||
moOpt = protojson.MarshalOptions{
|
||||
// Multiline: true,
|
||||
AllowPartial: true,
|
||||
UseProtoNames: true,
|
||||
UseEnumNumbers: true,
|
||||
EmitUnpopulated: true,
|
||||
}
|
||||
uoOpt = protojson.UnmarshalOptions{
|
||||
DiscardUnknown: true,
|
||||
}
|
||||
}
|
||||
|
||||
// PbCopy
|
||||
// @description: 将from对象的内容copy给to对象
|
||||
// parameter:
|
||||
// @from:proto.Message
|
||||
// @to:proto.Message
|
||||
// return:
|
||||
// @error:如果失败,则返回错误,否则返回nil
|
||||
func PbCopy(from, to proto.Message) error {
|
||||
data, err := proto.Marshal(from)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return proto.Unmarshal(data, to)
|
||||
}
|
||||
|
||||
// Pb2Json
|
||||
// @description: 将pb对象转换为json字符串
|
||||
// parameter:
|
||||
// @m:
|
||||
// return:
|
||||
// @string:pb对象的json标识形式
|
||||
// @error:
|
||||
func Pb2Json(m proto.Message) (string, error) {
|
||||
str, err := moOpt.Marshal(m)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(str), nil
|
||||
}
|
||||
|
||||
// Json2Pb
|
||||
// @description: 将json字符串转换成对应pb对象
|
||||
// parameter:
|
||||
// @js:
|
||||
// @m:
|
||||
// return:
|
||||
// @error:
|
||||
func Json2Pb(js string, m proto.Message) error {
|
||||
return uoOpt.Unmarshal([]byte(js), m)
|
||||
}
|
||||
|
||||
// Marshal
|
||||
// @description: 序列化pb对象
|
||||
// parameter:
|
||||
// @m:
|
||||
// return:
|
||||
// @[]byte:
|
||||
// @error:
|
||||
func Marshal(m proto.Message) ([]byte, error) {
|
||||
data, err := proto.Marshal(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Marshal_Panic
|
||||
// @description: 序列化pb对象
|
||||
// parameter:
|
||||
// @m:
|
||||
// return:
|
||||
// @[]byte:
|
||||
func Marshal_Panic(m proto.Message) []byte {
|
||||
data, err := Marshal(m)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("pbUtil Marshal pb错误,err:%s", err))
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// Unmarshal
|
||||
// @description: 将数据转换为pb对象
|
||||
// parameter:
|
||||
// @data:
|
||||
// @m:
|
||||
// return:
|
||||
func Unmarshal(data []byte, m proto.Message) error {
|
||||
err := proto.Unmarshal(data, m)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetClientIP
|
||||
// @description: 获取客户端Ip
|
||||
// parameter:
|
||||
// @ctx:grpc底层传递过来的上下文对象
|
||||
// return:
|
||||
// @string:客户端ip
|
||||
// @error:错误对象
|
||||
func GetClientIP(ctx context.Context) (string, error) {
|
||||
pr, ok := peer.FromContext(ctx)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("GetClietIP未获取到客户端ip")
|
||||
}
|
||||
if pr.Addr == net.Addr(nil) {
|
||||
return "", fmt.Errorf("GetClientIP 获取到的peer.Addr=nil")
|
||||
}
|
||||
addSlice := strings.Split(pr.Addr.String(), ":")
|
||||
|
||||
return addSlice[0], nil
|
||||
}
|
||||
Reference in New Issue
Block a user