-
Notifications
You must be signed in to change notification settings - Fork 7
/
option.go
97 lines (84 loc) · 2.31 KB
/
option.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
package micro
import (
"context"
"time"
"github.com/gmsec/micro/client"
"github.com/gmsec/micro/registry"
"github.com/gmsec/micro/server"
"github.com/gmsec/micro/tracer"
)
// Options ...
type Options struct {
// Broker broker.Broker
// Cmd cmd.Cmd
Client client.Client
Server server.Server
Registry *registry.Registry
// Registry registry.Registry
// Transport transport.Transport
// // Before and After funcs
// BeforeStart []func() error
// BeforeStop []func() error
// AfterStart []func() error
// AfterStop []func() error
// // Other options for implementations of the interface
// // can be stored in a context
Context context.Context
}
// newOptions default of option define
func newOptions(opts ...Option) Options {
// reg := registry.NewDNSNamingRegistry()
opt := Options{
Client: client.NewClient(),
Server: server.NewServer(),
// Registry: ®istry.Registry{
// RegNaming: reg,
// },
Context: context.Background(),
}
for _, o := range opts {
o(&opt)
}
if opt.Registry == nil { // 默认注册 服务发现
opt.Registry = ®istry.Registry{
RegNaming: registry.NewDNSNamingRegistry(),
}
opt.Client.Init(client.WithRegistryNaming(opt.Registry.RegNaming))
opt.Server.Init(server.WithRegistryNaming(opt.Registry.RegNaming))
}
return opt
}
// WithName of the service . Specify service name (Group)
func WithName(n string) Option {
tracer.SetServiceName(n)
return func(o *Options) {
o.Server.Init(server.Name(n))
o.Client.Init(client.WithServiceName(n))
//o.Server.Name = n
}
}
// WithRegisterTTL the service with a TTL
func WithRegisterTTL(t time.Duration) Option {
return func(o *Options) {
o.Server.Init(server.RegisterTTL(t))
o.Client.Init(client.RegisterTTL(t))
}
}
// WithRegisterInterval the service with at interval.
func WithRegisterInterval(t time.Duration) Option {
return func(o *Options) {
o.Server.Init(server.RegisterInterval(t))
}
}
// WithRegistryNaming Register for naming service discovery
func WithRegistryNaming(reg registry.RegNaming) Option {
return func(o *Options) {
// o.Server = server.NewServer()
// o.Client = client.NewClient()
o.Registry = ®istry.Registry{
RegNaming: reg,
}
o.Server.Init(server.WithRegistryNaming(o.Registry.RegNaming))
o.Client.Init(client.WithRegistryNaming(o.Registry.RegNaming))
}
}