/
init.go
137 lines (119 loc) · 4.3 KB
/
init.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
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
// Copyright 2022 NetEase Media Technology(Beijing)Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"github.com/NetEase-Media/easy-ngo/application/hooks"
"github.com/NetEase-Media/easy-ngo/application/r/rlog"
"github.com/NetEase-Media/easy-ngo/application/r/rmetrics"
"github.com/NetEase-Media/easy-ngo/application/r/rms"
"github.com/NetEase-Media/easy-ngo/application/r/rms/api"
"github.com/NetEase-Media/easy-ngo/application/r/rms/sd"
"github.com/NetEase-Media/easy-ngo/microservices/middleware"
"github.com/NetEase-Media/easy-ngo/microservices/middleware/metrics"
"github.com/NetEase-Media/easy-ngo/microservices/middleware/tracing"
"github.com/NetEase-Media/easy-ngo/microservices/transport/grpc/client"
tracer "github.com/NetEase-Media/easy-ngo/observability/tracing"
"github.com/NetEase-Media/easy-ngo/xlog"
"github.com/NetEase-Media/easy-ngo/xlog/xfmt"
)
var clients map[string]*client.Client
func init() {
hooks.Register(hooks.Initialize, initialize)
}
func initialize(ctx context.Context) error {
gconfig, err := rms.GetConfig()
if err != nil {
return fmt.Errorf("[microservices] load config failed: %v", err)
}
config := gconfig.GetGrpc().GetClients()
clients = make(map[string]*client.Client, len(config))
for i := range config {
if _, ok := clients[config[i].Name]; ok {
return fmt.Errorf("[microservices] duplicate client key: %s", config[i].Name)
}
c, err := newClient(ctx, config[i])
if err != nil {
return fmt.Errorf("[microservices] new client failed: %v", err)
}
clients[config[i].Name] = c
hooks.Register(hooks.Stop, func(ctx context.Context) error {
c.Close()
return nil
})
}
return nil
}
func newClient(ctx context.Context, config *api.GRPCClient) (*client.Client, error) {
target := config.Target
if target == "" {
return nil, fmt.Errorf("[microservices] client target must be set")
}
opts := make([]client.Option, 0, 10)
if config.Timeout != nil && config.Timeout.AsDuration() > 0 {
opts = append(opts, client.WithTimeout(config.Timeout.AsDuration()))
}
if config.DiscoveryRef != "" {
opts = append(opts, client.WithDiscovery(sd.GetDiscovery(config.DiscoveryRef)))
}
if config.BalancerName != "" {
opts = append(opts, client.WithBalancerName(config.BalancerName))
}
opts = append(opts, client.WithEnabledHealthCheck(config.EnabledHealthCheck))
var log xlog.Logger
log, _ = xfmt.Default()
if config.LoggerRef != "" {
log = rlog.GetLogger(config.LoggerRef)
if log == nil {
return nil, fmt.Errorf("[microservices] client logger not found: %s", config.LoggerRef)
}
}
opts = append(opts, client.WithLogger(log))
if config.EnableMetrics || config.EnableTracing || config.EnableLogging {
mws := make([]middleware.Middleware, 0, 3)
if config.EnableMetrics {
mws = append(mws, metrics.Client(rmetrics.GetProvider(), metrics.WithLogger(log)))
}
if config.EnableTracing {
mws = append(mws, tracing.Client(tracer.GetTracerProvider(), tracing.WithLogger(log)))
}
if config.EnableLogging {
return nil, fmt.Errorf("[microservices] client logging not supported")
}
opts = append(opts, client.WithMiddlewares(mws...))
}
if config.Tls != nil {
b, err := os.ReadFile(config.Tls.CertFile)
if err != nil {
return nil, fmt.Errorf("[microservices] client tls error: %s", err)
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
return nil, fmt.Errorf("[microservices] client tls error: %s", err)
}
tlsConf := &tls.Config{ServerName: config.Tls.ServerName, RootCAs: cp}
opts = append(opts, client.WithTLSConfig(tlsConf))
}
if config.GrpcOpts != nil {
opts = append(opts, client.WithGRPCOptions(getGRPCOptions(config.GrpcOpts)...))
}
return client.New(ctx, target, opts...)
}
func Get(name string) *client.Client {
return clients[name]
}