当前位置:   article > 正文

gin框架结合grpc_gin grpc

gin grpc

一、创建

mkdir grpc-go-client
cd grpc-go-client
go mod grpc-go-client
# 下载并安装gin
go get -u github.com/gin-gonic/gin
  • 1
  • 2
  • 3
  • 4
  • 5

二、创建main.go文件

Main.go

package main

import (
	"fmt"
	"net/http"
	"github.com/gin-gonic/gin"
)

func main(){
  r := gin.Default()
  // 测试一个get请求
  r.GET("/rest/n/:name", func(c *gin.Context) {
		name := c.Param("name")
    c.JSON(http.StatusOK, gin.H{
			"result": fmt.Sprint(name),
		})
	})
  	// Run http server
	if err := r.Run(":8052"); err != nil {
		fmt.Printf("could not run server: %v", err)
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

运行http服务

go run main.go
  • 1

三、创建protobuf连接

1、创建pb文件夹和protos文件夹

mkdir pb
mkdir protos
  • 1
  • 2

2、创建传输图片的proto文件

photo.proto

syntax = "proto3";

package pb;

option go_package = ".;pb";

message PhotoRuquest{
    string name = 1;
    bytes databytes = 2;
    
}

message PhotoResponse{
    string res = 1;
}

service Photoer {
    rpc SendPhoto (PhotoRuquest) returns (PhotoResponse){}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3、编译生成

go get google.golang.org/grpc
protoc --proto_path=./protos/ ./protos/*.proto --go_out=plugins=grpc:./pb
  • 1
  • 2

4、在启动文件中引入连接grpc服务器

import (
	"grpc-go-client/pb"
  "google.golang.org/grpc"
)
const port = ":50051" // 服务器端口
conn,err :=grpc.Dial("localhost"+port,grpc.WithInsecure())
	if err !=nil{
		log.Fatal(err.Error())
	}
	defer conn.Close()
// conn 调用相关服务的“通道”
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

四、grpc调用服务

// 用于main函数中调用
func sendImg(client pb.PhotoerClient,data []byte,imgName string){
	res,err :=client.SendPhoto(context.Background(),&pb.PhotoRuquest{Databytes:data,Name:imgName})

	
	if err !=nil{
		log.Fatal(err.Error())
	}
	fmt.Println(res.Res)
}
	
}
//main函数中调用
sendImg(pb.NewPhotoerClient(conn),data,imgFile.Filename)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

五、http的post请求调用服务

完整main函数代码

package main

import (
	"fmt"
	"grpc-go-client/pb"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"context"
	"github.com/gin-gonic/gin"
  "google.golang.org/grpc"
)

const port = ":50051"

func main(){
	conn,err :=grpc.Dial("localhost"+port,grpc.WithInsecure())
	if err !=nil{
		log.Fatal(err.Error())
	}
	defer conn.Close()

	r := gin.Default()
	// 上传文件
	r.POST("/upload", func(c *gin.Context) {
		// 单个文件
		imgFile, err := c.FormFile("img")
    
		f,_:=imgFile.Open()
    
		data,_ :=ioutil.ReadAll(f)
    
		sendImg(pb.NewPhotoerClient(conn),data,imgFile.Filename)
    
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{
				"message": err.Error(),
			})
			return
		}

		log.Println(imgFile.Filename)
		c.JSON(http.StatusOK, gin.H{
			"message": fmt.Sprintf("'%s' uploaded!", imgFile.Filename),
		})
	})

	// Run http server
	if err := r.Run(":8052"); err != nil {
		log.Fatalf("could not run server: %v", err)
	}


}
// 功能函数
func sendImg(client pb.PhotoerClient,data []byte,imgName string){
	res,err :=client.SendPhoto(context.Background(),&pb.PhotoRuquest{Databytes:data,Name:imgName})

	
	if err !=nil{
		log.Fatal(err.Error())
	}
	fmt.Println(res.Res)
}
	
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

六、日志记录中间件

1、通过viper管理项目的日志配置及后续其他的配置

安装viper

go get -u github.com/spf13/viper
  • 1

安装zap记录日志

// 安装 lumberjack,zap
go get -u github.com/natefinch/lumberjack
go get -u go.uber.org/zap
  • 1
  • 2
  • 3

2、创建日志相关的文件夹和配置文件

# 创建logs文件夹和config配置文件夹
mkdir logs # 存放日志
mkdir config
  • 1
  • 2
  • 3

3、编写日志的配置文件

logconf.json

{
  "level": "info",
  "filename": "logs/viper_zap_gin.log",
  "maxsize": "1",
  "max_age": "30",
  "max_backups": "5",
  "version": "v1.8"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4、使用zap日志库

创建logger文件夹,编写zap库的设置logger.go

package logger

import (
	// "grpc-go-client/config" // 配置文件
	"github.com/gin-gonic/gin"
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"net"
	"net/http"
	"net/http/httputil"
	"os"
	"runtime/debug"
	"strings"
	"time"
)


var Logger *zap.Logger

type logConfig struct {
	Level      string `json:"level"`
	Filename   string `json:"filename"`
	MaxSize    int    `json:"maxsize"`
	MaxAge     int    `json:"max_age"`
	MaxBackups int    `json:"max_backups"`
	Version    string `json:"version"`
}

// InitLogger 初始化Logger
func InitLogger(cfg *logConfig) (err error) {
	writeSyncer := getLogWriter(cfg.Filename, cfg.MaxSize, cfg.MaxBackups, cfg.MaxAge)
	encoder := getEncoder()
	// for human operators.
	var l = new(zapcore.Level)
	err = l.UnmarshalText([]byte(cfg.Level))
	if err != nil {
		return
	}
	// 打印到控制台和日志文件
	core := zapcore.NewCore(encoder,
		zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stderr), writeSyncer),
		l)

	Logger = zap.New(core, zap.AddCaller())
	return
}

func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.TimeKey = "time"
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	encoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
	encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
	return zapcore.NewJSONEncoder(encoderConfig)
}

func getLogWriter(filename string, maxSize, maxBackup, maxAge int) zapcore.WriteSyncer {
	lumberJackLogger := &lumberjack.Logger{
		Filename:   filename,
		MaxSize:    maxSize,
		MaxBackups: maxBackup,
		MaxAge:     maxAge,
	}
	return zapcore.AddSync(lumberJackLogger)
}

// GinLogger 接收gin框架默认的日志
func GinLogger(logger *zap.Logger) gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()
		path := c.Request.URL.Path
		query := c.Request.URL.RawQuery
		c.Next()

		cost := time.Since(start)
		logger.Info(path,
			zap.Int("status", c.Writer.Status()),
			zap.String("method", c.Request.Method),
			zap.String("path", path),
			zap.String("query", query),
			zap.String("ip", c.ClientIP()),
			zap.String("user-agent", c.Request.UserAgent()),
			zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
			zap.Duration("cost", cost),
		)
	}
}

// GinRecovery recover掉项目可能出现的panic,并使用zap记录相关日志
func GinRecovery(logger *zap.Logger, stack bool) gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				// Check for a broken connection, as it is not really a
				// condition that warrants a panic stack trace.
				var brokenPipe bool
				if ne, ok := err.(*net.OpError); ok {
					if se, ok := ne.Err.(*os.SyscallError); ok {
						if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
							brokenPipe = true
						}
					}
				}

				httpRequest, _ := httputil.DumpRequest(c.Request, false)
				if brokenPipe {
					logger.Error(c.Request.URL.Path,
						zap.Any("error", err),
						zap.String("request", string(httpRequest)),
					)
					// If the connection is dead, we can't write a status to it.
					c.Error(err.(error)) // nolint: errcheck
					c.Abort()
					return
				}

				if stack {
					logger.Error("[Recovery from panic]",
						zap.Any("error", err),
						zap.String("request", string(httpRequest)),
						zap.String("stack", string(debug.Stack())),
					)
				} else {
					logger.Error("[Recovery from panic]",
						zap.Any("error", err),
						zap.String("request", string(httpRequest)),
					)
				}
				c.AbortWithStatus(http.StatusInternalServerError)
			}
		}()
		c.Next()
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137

main.go文件中启用viper和zap

	viper.SetConfigFile("./config/conf.json") //指定json配置文件的路径
	// viper.SetConfigFile("./config/conf.yaml") //指定yaml配置文件的路径
	err := viper.ReadInConfig() // 读取配置信息
	if err != nil {             // 配置失败
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}

	if err = viper.Unmarshal(logConf); err != nil {
		panic(fmt.Errorf("unmarshal conf failed, err:%s \n", err))
	}
	// 监控配置文件变化
	viper.WatchConfig()
 // 在gin中使用日志中间件
	r.Use(logger.GinLogger(logger.Logger), logger.GinRecovery(logger.Logger, true))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

七、划分不同的APP和路由分组

1、创建APP文件夹,用来放置不同的AI应用

mkdir app
# 在app下创建ping文件夹用于测试
mkdir app/ping
  • 1
  • 2
  • 3

2、路由分组

#在ping下创建hander.go和router.go
touch hander.go router.go
  • 1
  • 2

3、创建routers文件夹初始化路由

mkdir routers
touch routers/routers.go
  • 1
  • 2

routers.go

package routers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"grpc-go-client/logger"
)

type Option func(*gin.Engine)

var options = []Option{}

func Include(opts ...Option) {
	options = append(options, opts...)
}

func cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method

		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")

		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}

// 初始化 gin的引擎
func RouterInit() *gin.Engine {
	r := gin.New()
	// 跨域
	r.Use(cors())
	// 使用zap日志
	r.Use(logger.GinLogger(logger.Logger), logger.GinRecovery(logger.Logger, true))

	for _, opt := range options {
		opt(r)
	}
	return r
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

调整后的main.go文件内容

package main

import (
	"fmt"
	"grpc-go-client/app/ping"
	"grpc-go-client/logger"
	"grpc-go-client/routers"
	"log"
	"github.com/spf13/viper"
)


var logConf = new(logger.LogConfig)

func main() {
	// viper
	viper.SetConfigFile("./config/logconf.json") //指定json配置文件的路径
	err := viper.ReadInConfig()                  // 读取配置信息
	if err != nil {                              // 配置失败
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}

	if err = viper.Unmarshal(logConf); err != nil {
		panic(fmt.Errorf("unmarshal conf failed, err:%s \n", err))
	}
	// 监控配置文件变化
	viper.WatchConfig()

	// zap 初始化
	if err := logger.InitLogger(logConf); err != nil {
		fmt.Printf("init logger failed, err:%v\n", err)
		return
	}
	// 加载多个APP的路由配置
	routers.Include(ping.Routers)

	// 初始化所有的app的路由
	r := routers.RouterInit()


	// Run http server
	if err := r.Run(":8052"); err != nil {
		log.Fatalf("could not run server: %v", err)
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

八、对接ocr

项目代码略

2、创建grpc服务端

python编译

# 在protos文件夹中
python -m grpc_tools.protoc --python_out=../pb --grpc_python_out=../pb -I . ocrPic.proto
  • 1
  • 2

golang编译

protoc --proto_path=./ ocrPic.proto --go_out=plugins=grpc:../pb
  • 1

报错:


AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'

  • 1
  • 2
  • 3

解决

pip install --upgrade protobuf
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/89122
推荐阅读
相关标签
  

闽ICP备14008679号