通用包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Wang Yunxiao aa9a815a2a 错误拦截新增全局 glog 记录服务错误 3 months ago
..
grpc 错误拦截新增全局 glog 记录服务错误 3 months ago
README.md http编解码和grpc拦截器 2 years ago

README.md

interceptor-通用grpcInterceptor

说明

grpc请求和响应拦截器

  • server
    • server原样返回业务错误,正式服其它错误统一返回InternalServerError
  • client
    • client默认转换server的所有error为kratos的error,方便bff原样返回

使用示例

client

package data

import (
	"context"
	grpc2 "gitea.drugeyes.vip/pharnexbase/utils/interceptor/v1/grpc"
	"github.com/go-kratos/kratos/v2/middleware/circuitbreaker"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/google/wire"
	user "user-center-front/api/user/v2"
	"user-center-front/internal/conf"
)

// ProviderSet is data providers.
var ProviderSet = wire.NewSet(
	NewData,
	NewUserService,
)

// Data .
type Data struct {
	User user.UserCenterClient
}

// NewData .
func NewData(userServer user.UserCenterClient) *Data {
	return &Data{
		User: userServer,
	}
}

func NewUserService(c *conf.Bootstrap) user.UserCenterClient {
	grpcInterceptor := grpc2.NewInterceptor(c.Env, nil)
	conn, err := grpc.DialInsecure(
		context.Background(),
		grpc.WithEndpoint(c.Service.GetUser()),
		grpc.WithTimeout(0),
		grpc.WithMiddleware(
			tracing.Client(),        // 链路追踪
			circuitbreaker.Client(), // 熔断器
		),
		// 拦截器
		grpc.WithUnaryInterceptor(grpcInterceptor.UnaryClientInterceptor()),
	)
	if err != nil {
		panic(err)
	}

	return user.NewUserCenterClient(conn)
}

server

package server

import (
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/logging"
	"github.com/go-kratos/kratos/v2/middleware/ratelimit"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/middleware/validate"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	grpc2 "gitea.drugeyes.vip/pharnexbase/utils/interceptor/v1/grpc"
	v2 "user-center-v2/api/user/v2"
	"user-center-v2/internal/conf"
	pkgLog "user-center-v2/internal/pkg/log"
	"user-center-v2/internal/service"
)

// NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Bootstrap, userService *service.UserCenterService, logger log.Logger) *grpc.Server {
	grpcInterceptor := grpc2.NewInterceptor(c.Env, v2.ErrorReason_value)
	var opts = []grpc.ServerOption{
		grpc.Middleware(
			recovery.Recovery(),
			pkgLog.Trace(),
			ratelimit.Server(),
			logging.Server(logger),
			validate.Validator(),
		),
		// 拦截器
		grpc.UnaryInterceptor(grpcInterceptor.UnaryServerInterceptor()),
	}
	if c.Server.Grpc.Network != "" {
		opts = append(opts, grpc.Network(c.Server.Grpc.Network))
	}
	if c.Server.Grpc.Addr != "" {
		opts = append(opts, grpc.Address(c.Server.Grpc.Addr))
	}
	if c.Server.Grpc.Timeout != nil {
		opts = append(opts, grpc.Timeout(c.Server.Grpc.Timeout.AsDuration()))
	}

	srv := grpc.NewServer(opts...)
	v2.RegisterUserCenterServer(srv, userService)
	return srv
}