forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chassis.go
executable file
·261 lines (238 loc) · 7.19 KB
/
chassis.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package chassis
import (
"fmt"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/ServiceComb/go-chassis/auth"
"github.com/ServiceComb/go-chassis/bootstrap"
// highway package handles remote procedure calls
_ "github.com/ServiceComb/go-chassis/client/highway"
// rest package handle rest apis
_ "github.com/ServiceComb/go-chassis/client/rest"
// archaius package to get the conguration info fron diffent configuration sources
"github.com/ServiceComb/go-chassis/core/archaius"
"github.com/ServiceComb/go-chassis/core/common"
"github.com/ServiceComb/go-chassis/core/config"
"github.com/ServiceComb/go-chassis/core/handler"
"github.com/ServiceComb/go-chassis/core/lager"
"github.com/ServiceComb/go-chassis/core/loadbalance"
"github.com/ServiceComb/go-chassis/core/registry"
// file package for file based registration
_ "github.com/ServiceComb/go-chassis/core/registry/file"
// servicecenter package handles service center api calls
_ "github.com/ServiceComb/go-chassis/core/registry/servicecenter"
"github.com/ServiceComb/go-chassis/core/route"
"github.com/ServiceComb/go-chassis/core/server"
"github.com/ServiceComb/go-chassis/core/tracing"
"github.com/ServiceComb/go-chassis/eventlistener"
// aes package handles security related plugins
_ "github.com/ServiceComb/go-chassis/security/plugins/aes"
_ "github.com/ServiceComb/go-chassis/security/plugins/plain"
_ "github.com/ServiceComb/go-chassis/server/highway"
_ "github.com/ServiceComb/go-chassis/server/restful"
serverOption "github.com/ServiceComb/go-chassis/third_party/forked/go-micro/server"
// tcp package handles transport related things
_ "github.com/ServiceComb/go-chassis/transport/tcp"
)
var goChassis *chassis
func init() {
goChassis = &chassis{}
}
type chassis struct {
version string
schemas []*Schema
mu sync.Mutex
Initialized bool
DefaultConsumerChainNames map[string]string
DefaultProviderChainNames map[string]string
}
// Schema struct for to represent schema info
type Schema struct {
protocol string
schema interface{}
opts []serverOption.RegisterOption
}
func (c *chassis) initChains(chainType string) error {
var defaultChainName = "default"
var handlerNameMap = map[string]string{defaultChainName: ""}
switch chainType {
case common.Provider:
if len(config.GlobalDefinition.Cse.Handler.Chain.Provider) != 0 {
handlerNameMap = config.GlobalDefinition.Cse.Handler.Chain.Provider
} else {
handlerNameMap = c.DefaultProviderChainNames
}
case common.Consumer:
if len(config.GlobalDefinition.Cse.Handler.Chain.Consumer) != 0 {
handlerNameMap = config.GlobalDefinition.Cse.Handler.Chain.Consumer
} else {
handlerNameMap = c.DefaultConsumerChainNames
}
}
lager.Logger.Debugf("Init %s's handlermap", chainType)
return handler.CreateChains(chainType, handlerNameMap)
}
func (c *chassis) initHandler() error {
if err := c.initChains(common.Provider); err != nil {
lager.Logger.Errorf(err, "chain int failed")
return err
}
if err := c.initChains(common.Consumer); err != nil {
lager.Logger.Errorf(err, "chain int failed")
return err
}
lager.Logger.Info("Chain init success")
return nil
}
//Init
func (c *chassis) initialize() error {
if c.Initialized {
return nil
}
err := config.Init()
if err != nil {
lager.Logger.Error("Failed to initialize conf,", err)
return err
}
router.Init(config.GetRouterConfig().Destinations, config.GetRouterConfig().SourceTemplates)
auth.Init()
err = c.initHandler()
if err != nil {
lager.Logger.Errorf(err, "Handler init failed")
return err
}
bootstrap.Bootstrap()
err = server.Init()
if err != nil {
return err
}
if archaius.GetBool("cse.service.registry.disabled", false) != true {
err := registry.Enable()
if err != nil {
return err
}
if err := loadbalance.Enable(); err != nil {
return err
}
}
err = tracing.Init()
if err != nil {
return err
}
eventlistener.Init()
c.Initialized = true
return nil
}
func (c *chassis) registerSchema(protocol string, structPtr interface{}, opts ...serverOption.RegisterOption) {
schema := &Schema{
protocol: protocol,
schema: structPtr,
opts: opts,
}
c.mu.Lock()
c.schemas = append(c.schemas, schema)
c.mu.Unlock()
}
func (c *chassis) start() error {
if !c.Initialized {
return fmt.Errorf("the chassis do not init. please run chassis.Init() first")
}
for _, v := range c.schemas {
if v == nil {
continue
}
s, err := server.GetServer(v.protocol)
if err != nil {
return err
}
_, err = s.Register(v.schema, v.opts...)
if err != nil {
return err
}
}
err := server.StartServer()
if err != nil {
return err
}
return nil
}
//RegisterSchema Register a API service to specific protocol
//You must register API first before Call Init
func RegisterSchema(protocol string, structPtr interface{}, opts ...serverOption.RegisterOption) {
goChassis.registerSchema(protocol, structPtr, opts...)
}
//SetDefaultConsumerChains your custom chain map for Consumer,if there is no config, this default chain will take affect
func SetDefaultConsumerChains(c map[string]string) {
goChassis.DefaultConsumerChainNames = c
}
//SetDefaultProviderChains set your custom chain map for Provider,if there is no config, this default chain will take affect
func SetDefaultProviderChains(c map[string]string) {
goChassis.DefaultProviderChainNames = c
}
//Run bring up the service,it will not return error,instead just waiting for os signal,and shutdown gracefully
func Run() {
err := goChassis.start()
if err != nil {
lager.Logger.Error("run chassis fail:", err)
}
//Register instance after Server started
if err := registry.DoRegister(); err != nil {
lager.Logger.Error("register instance fail:", err)
}
//Graceful shutdown
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, syscall.SIGABRT)
select {
case s := <-c:
lager.Logger.Info("got os signal " + s.String())
case err := <-server.ServerErr:
lager.Logger.Info("got Server Error " + err.Error())
}
for name, s := range server.GetServers() {
lager.Logger.Info("stopping server " + name + "...")
err := s.Stop()
if err != nil {
lager.Logger.Errorf(err, "servers failed to stop")
}
lager.Logger.Info(name + " server stop success")
}
if err = server.UnRegistrySelfInstances(); err != nil {
lager.Logger.Errorf(err, "servers failed to unregister")
}
}
//Init prepare the chassis framework runtime
func Init() error {
if goChassis.DefaultConsumerChainNames == nil {
defaultChain := strings.Join([]string{
handler.RatelimiterConsumer,
handler.BizkeeperConsumer,
handler.Router,
handler.Loadbalance,
handler.TracingConsumer,
handler.Transport,
}, ",")
goChassis.DefaultConsumerChainNames = map[string]string{
common.DefaultKey: defaultChain,
}
}
if goChassis.DefaultProviderChainNames == nil {
defaultChain := strings.Join([]string{
handler.RatelimiterProvider,
handler.TracingProvider,
handler.BizkeeperProvider,
}, ",")
goChassis.DefaultProviderChainNames = map[string]string{
common.DefaultKey: defaultChain,
}
}
if err := goChassis.initialize(); err != nil {
log.Println("Init chassis fail:", err)
return err
}
lager.Logger.Infof("Init chassis success")
return nil
}