forked from apache/dubbo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdubbo.go
158 lines (140 loc) · 4.79 KB
/
dubbo.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 dubbo
import (
"github.com/pkg/errors"
)
import (
"dubbo.apache.org/dubbo-go/v3/client"
"dubbo.apache.org/dubbo-go/v3/server"
)
// Instance is the highest layer conception that user could touch. It is mapped from RootConfig.
// When users want to inject global configurations and configure common modules for client layer
// and server layer, user-side code would be like this:
//
// ins, err := NewInstance()
// cli, err := ins.NewClient()
type Instance struct {
insOpts *InstanceOptions
}
// NewInstance receives InstanceOption and initializes RootConfig. There are some processing
// tasks during initialization.
func NewInstance(opts ...InstanceOption) (*Instance, error) {
newInsOpts := defaultInstanceOptions()
if err := newInsOpts.init(opts...); err != nil {
return nil, err
}
return &Instance{insOpts: newInsOpts}, nil
}
// NewClient is like client.NewClient, but inject configurations from RootConfig and
// ConsumerConfig
func (ins *Instance) NewClient(opts ...client.ClientOption) (*client.Client, error) {
if ins == nil || ins.insOpts == nil {
return nil, errors.New("Instance has not been initialized")
}
var cliOpts []client.ClientOption
conCfg := ins.insOpts.Consumer
appCfg := ins.insOpts.Application
regsCfg := ins.insOpts.Registries
sdCfg := ins.insOpts.Shutdown
metricsCfg := ins.insOpts.Metrics
otelCfg := ins.insOpts.Otel
if conCfg != nil {
if conCfg.Check {
cliOpts = append(cliOpts, client.WithClientCheck())
}
// these options come from Consumer and Root.
// for dubbo-go developers, referring config/ConsumerConfig.Init and config/ReferenceConfig
cliOpts = append(cliOpts,
client.WithClientFilter(conCfg.Filter),
// todo(DMwangnima): deal with Protocol
client.WithClientRegistryIDs(conCfg.RegistryIDs),
// todo(DMwangnima): deal with TracingKey
client.SetClientConsumer(conCfg),
)
}
if appCfg != nil {
cliOpts = append(cliOpts, client.SetApplication(appCfg))
}
if regsCfg != nil {
cliOpts = append(cliOpts, client.SetClientRegistries(regsCfg))
}
if sdCfg != nil {
cliOpts = append(cliOpts, client.SetClientShutdown(sdCfg))
}
if metricsCfg != nil {
cliOpts = append(cliOpts, client.SetClientMetrics(metricsCfg))
}
if otelCfg != nil {
cliOpts = append(cliOpts, client.SetClientOtel(otelCfg))
}
// options passed by users has higher priority
cliOpts = append(cliOpts, opts...)
cli, err := client.NewClient(cliOpts...)
if err != nil {
return nil, err
}
return cli, nil
}
// NewServer is like server.NewServer, but inject configurations from RootConfig.
func (ins *Instance) NewServer(opts ...server.ServerOption) (*server.Server, error) {
if ins == nil || ins.insOpts == nil {
return nil, errors.New("Instance has not been initialized")
}
var srvOpts []server.ServerOption
appCfg := ins.insOpts.Application
regsCfg := ins.insOpts.Registries
prosCfg := ins.insOpts.Protocols
sdCfg := ins.insOpts.Shutdown
metricsCfg := ins.insOpts.Metrics
otelCfg := ins.insOpts.Otel
if appCfg != nil {
srvOpts = append(srvOpts,
server.SetServerApplication(appCfg),
//server.WithServer_ApplicationConfig(
// global.WithApplication_Name(appCfg.Name),
// global.WithApplication_Organization(appCfg.Organization),
// global.WithApplication_Module(appCfg.Module),
// global.WithApplication_Version(appCfg.Version),
// global.WithApplication_Owner(appCfg.Owner),
// global.WithApplication_Environment(appCfg.Environment),
//),
)
}
if regsCfg != nil {
srvOpts = append(srvOpts, server.SetServerRegistries(regsCfg))
}
if prosCfg != nil {
srvOpts = append(srvOpts, server.SetServerProtocols(prosCfg))
}
if sdCfg != nil {
srvOpts = append(srvOpts, server.SetServerShutdown(sdCfg))
}
if metricsCfg != nil {
srvOpts = append(srvOpts, server.SetServerMetrics(metricsCfg))
}
if otelCfg != nil {
srvOpts = append(srvOpts, server.SetServerOtel(otelCfg))
}
// options passed by users have higher priority
srvOpts = append(srvOpts, opts...)
srv, err := server.NewServer(srvOpts...)
if err != nil {
return nil, err
}
return srv, nil
}