-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnaming_server.go
192 lines (157 loc) · 3.74 KB
/
naming_server.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package server
import (
"fmt"
"net"
"sync"
"time"
"github.com/gmsec/micro/registry"
"github.com/gmsec/micro/tracer"
"github.com/xxjwxc/public/dev"
"github.com/xxjwxc/public/mylog"
"github.com/gmsec/micro/grpc_opentracing"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
)
// var (
// DefaultnamingResolver registry.RegistryNaming =
// )
/*
github.com/gmsec/micro/naming
grpc 默认支持项,多语言同时接入时推荐使用
*/
type namingResolver struct {
opts Options
sync.RWMutex
// marks the serve as started
started bool
//exit chan chan error
}
// NewNamingResolver new one
func NewNamingResolver(opts ...Option) *namingResolver {
resp := &namingResolver{}
resp.Init(opts...)
// if resp.opts.Registry == nil {
// resp.opts.Registry = ®istry.Registry{
// RegNaming: registry.NewDNSNamingRegistry(),
// }
// }
return resp
}
func (s *namingResolver) Options() Options {
s.RLock()
opts := s.opts
s.RUnlock()
return opts
}
func (s *namingResolver) Init(opts ...Option) error {
s.Lock()
defer s.Unlock()
for _, opt := range opts {
opt(&s.opts)
}
return nil
}
// Handle(Handler) error
// NewHandler(interface{}, ...HandlerOption) Handler
// NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
// Subscribe(Subscriber) error
func (s *namingResolver) Start() error {
gs := s.GetServer()
s.Lock()
defer s.Unlock()
lis := s.opts.getListener()
if s.started {
return nil
}
if s.opts.Registry == nil { // default naming register
s.opts.Registry = ®istry.Registry{
RegNaming: registry.NewDNSNamingRegistry(),
}
}
// init registry parms
s.opts.Registry.RegNaming.Init(registry.WithAddrs(s.opts.Address),
registry.WithNodeID(s.opts.ID),
registry.WithServiceName(s.opts.Name),
registry.WithTimeout(s.opts.RegisterTTL),
)
// 开始注册
reg := s.opts.Registry.RegNaming
if err := reg.Register(s.opts.Address, nil); err != nil {
mylog.ErrorString("gRPC Server register error:")
mylog.Error(err)
}
// 健康检查
hsrv := health.NewServer()
grpc_health_v1.RegisterHealthServer(gs, hsrv)
hsrv.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
// micro: go ts.Accept(s.accept)
go func() {
mylog.Info(fmt.Sprintf("grpc server in: %v", s.opts.Address))
if err := gs.Serve(lis); err != nil {
mylog.ErrorString("gRPC Server start error:")
mylog.Error(err)
}
}()
s.started = true
return nil
}
func (s *namingResolver) Stop() error {
s.RLock()
if !s.started {
s.RUnlock()
return nil
}
s.RUnlock()
gs := s.GetServer()
reg := s.opts.Registry.RegNaming
reg.Deregister()
tracer.CloseTracer() // 关闭链路追踪
// paus one second
select {
case <-time.After(time.Second):
gs.Stop()
}
s.Lock()
s.started = false
s.Unlock()
return nil
}
// String name string
func (s *namingResolver) String() string {
return "naming_resolver"
}
// GetServer get server
func (s *namingResolver) GetServer() *grpc.Server {
s.Lock()
defer s.Unlock()
if s.opts.Server == nil {
trace := tracer.GetTracer()
if trace != nil {
s.opts.Server = grpc.NewServer(grpc.UnaryInterceptor(
grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(trace),
grpc_opentracing.WithDev(dev.IsDev()))))
} else {
s.opts.Server = grpc.NewServer()
}
}
return s.opts.Server
}
// GetListener listener
func (s *namingResolver) GetListener() net.Listener {
s.Lock()
defer s.Unlock()
return s.opts.getListener()
}
// SetListener 设置listener
func (s *namingResolver) SetListener(l net.Listener) bool {
s.Lock()
defer s.Unlock()
return s.opts.setListener(l)
}
func (s *namingResolver) SetAddress(add string) {
s.opts.Address = add
}
func (s *namingResolver) GetAddress() string {
return s.opts.Address
}