-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
68 lines (56 loc) · 1.87 KB
/
grpc.go
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
package server
import (
"context"
"time"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/registry"
kratosGrpc "github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/lalifeier/vvgo-mall/gen/api/go/common/conf"
)
const defaultTimeout = 5 * time.Second
// CreateGrpcClient 创建GRPC客户端
func CreateGrpcClient(ctx context.Context, r registry.Discovery, serviceName string, timeoutDuration *durationpb.Duration) grpc.ClientConnInterface {
timeout := defaultTimeout
if timeoutDuration != nil {
timeout = timeoutDuration.AsDuration()
}
endpoint := "discovery:///" + serviceName
conn, err := kratosGrpc.DialInsecure(
ctx,
kratosGrpc.WithEndpoint(endpoint),
kratosGrpc.WithDiscovery(r),
kratosGrpc.WithTimeout(timeout),
kratosGrpc.WithMiddleware(
tracing.Client(),
recovery.Recovery(),
),
)
if err != nil {
log.Fatalf("dial grpc client [%s] failed: %s", serviceName, err.Error())
}
return conn
}
// CreateGrpcServer 创建GRPC服务端
func CreateGrpcServer(cfg *conf.Bootstrap, m ...middleware.Middleware) *kratosGrpc.Server {
var opts []kratosGrpc.ServerOption
var ms []middleware.Middleware
ms = append(ms, recovery.Recovery())
ms = append(ms, tracing.Server())
ms = append(ms, m...)
opts = append(opts, kratosGrpc.Middleware(ms...))
if cfg.Server.Grpc.Network != "" {
opts = append(opts, kratosGrpc.Network(cfg.Server.Grpc.Network))
}
if cfg.Server.Grpc.Addr != "" {
opts = append(opts, kratosGrpc.Address(cfg.Server.Grpc.Addr))
}
if cfg.Server.Grpc.Timeout != nil {
opts = append(opts, kratosGrpc.Timeout(cfg.Server.Grpc.Timeout.AsDuration()))
}
return kratosGrpc.NewServer(opts...)
}