-
Notifications
You must be signed in to change notification settings - Fork 472
/
chassis_api.go
executable file
·197 lines (173 loc) · 5.8 KB
/
chassis_api.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
package chassis
import (
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/go-chassis/openlog"
//init logger first
_ "github.com/go-chassis/go-chassis/v2/initiator"
// transport handler
_ "github.com/go-chassis/go-chassis/v2/core/client"
//load balancing
_ "github.com/go-chassis/go-chassis/v2/pkg/loadbalancing"
//protocols
_ "github.com/go-chassis/go-chassis/v2/client/rest"
_ "github.com/go-chassis/go-chassis/v2/server/restful"
"github.com/go-chassis/go-chassis/v2/core/common"
"github.com/go-chassis/go-chassis/v2/core/config"
"github.com/go-chassis/go-chassis/v2/core/handler"
"github.com/go-chassis/go-chassis/v2/core/registry"
//router
_ "github.com/go-chassis/go-chassis/v2/core/router/servicecomb"
//control panel
_ "github.com/go-chassis/go-chassis/v2/control/servicecomb"
// registry
_ "github.com/go-chassis/go-chassis/v2/core/registry/servicecenter"
"github.com/go-chassis/go-chassis/v2/core/server"
// prometheus reporter for circuit breaker metrics
_ "github.com/go-chassis/go-chassis/v2/third_party/forked/afex/hystrix-go/hystrix/reporter"
// aes package handles security related plugins
_ "github.com/go-chassis/go-chassis/v2/security/cipher/plugins/aes"
_ "github.com/go-chassis/go-chassis/v2/security/cipher/plugins/plain"
//config servers
_ "github.com/go-chassis/go-archaius/source/remote"
_ "github.com/go-chassis/go-archaius/source/remote/kie"
"github.com/go-chassis/go-chassis/v2/core/metadata"
)
var goChassis *chassis
func init() {
goChassis = &chassis{}
}
// RegisterSchema Register a API service to specific server by name
// You must register API first before Call Init
func RegisterSchema(serverName string, structPtr interface{}, opts ...server.RegisterOption) {
goChassis.registerSchema(serverName, 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
}
// HijackSignal set signals that want to hijack.
func HijackSignal(sigs ...os.Signal) {
goChassis.sigs = sigs
}
// InstallPreShutdown instal what you want to achieve before graceful shutdown
func InstallPreShutdown(name string, f func(os.Signal)) {
// lazy init
if goChassis.preShutDownFuncs == nil {
goChassis.preShutDownFuncs = make(map[string]func(os.Signal))
}
goChassis.preShutDownFuncs[name] = f
}
// InstallPostShutdown instal what you want to achieve after graceful shutdown
func InstallPostShutdown(name string, f func(os.Signal)) {
// lazy init
if goChassis.postShutDownFuncs == nil {
goChassis.postShutDownFuncs = make(map[string]func(os.Signal))
}
goChassis.postShutDownFuncs[name] = f
}
// HijackGracefulShutdown reset GracefulShutdown
func HijackGracefulShutdown(f func(os.Signal)) {
goChassis.hijackGracefulShutdown = f
}
// Run bring up the service,it waits for os signal,and shutdown gracefully
// before all protocol server start successfully, it may return error.
func Run(options ...server.RunOption) error {
err := goChassis.start(options...)
if err != nil {
openlog.Error("run chassis failed:" + err.Error())
return err
}
if !config.GetRegistratorDisable() {
//Register instance after Server started
if err := registry.DoRegister(); err != nil {
openlog.Error("register instance failed:" + err.Error())
return err
}
}
waitingSignal()
return nil
}
func waitingSignal() {
c := make(chan os.Signal, 1)
if len(goChassis.sigs) > 0 {
signal.Notify(c, goChassis.sigs...)
} else {
signal.Notify(c, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM,
syscall.SIGQUIT, syscall.SIGILL, syscall.SIGTRAP, syscall.SIGABRT)
}
var s os.Signal
select {
case s = <-c:
openlog.Info("got os signal " + s.String())
case err := <-server.ErrRuntime:
openlog.Info("got server error " + err.Error())
}
if goChassis.preShutDownFuncs != nil {
for k, v := range goChassis.preShutDownFuncs {
openlog.Info(fmt.Sprintf("exec pre shutdown funcs %s", k))
v(s)
}
}
goChassis.hijackGracefulShutdown(s)
if goChassis.postShutDownFuncs != nil {
for k, v := range goChassis.postShutDownFuncs {
openlog.Info(fmt.Sprintf("exec post shutdown funcs %s", k))
v(s)
}
}
}
// GracefulShutdown graceful shut down api
func GracefulShutdown(s os.Signal) {
if !config.GetRegistratorDisable() {
registry.HBService.Stop()
openlog.Info("unregister servers ...")
if err := server.UnRegistrySelfInstances(); err != nil {
openlog.Warn("servers failed to unregister: " + err.Error())
}
}
for name, s := range server.GetServers() {
openlog.Info("stopping server " + name + "...")
err := s.Stop()
if err != nil {
openlog.Warn("servers failed to stop: " + err.Error())
}
openlog.Info(name + " server stop success")
}
openlog.Info("go chassis server gracefully shutdown")
}
// Init prepare the chassis framework runtime
func Init() error {
if goChassis.DefaultConsumerChainNames == nil {
defaultChain := strings.Join([]string{
handler.Router,
handler.LoadBalancing,
handler.TracingConsumer,
handler.Transport,
}, ",")
goChassis.DefaultConsumerChainNames = map[string]string{
common.DefaultKey: defaultChain,
}
}
if goChassis.DefaultProviderChainNames == nil {
defaultChain := strings.Join([]string{handler.TracingProvider}, ",")
goChassis.DefaultProviderChainNames = map[string]string{
common.DefaultKey: defaultChain,
}
}
goChassis.hijackGracefulShutdown = GracefulShutdown
if err := goChassis.initialize(); err != nil {
log.Println("init chassis fail:", err)
return err
}
openlog.Info("init chassis success, version is " + metadata.SdkVersion)
return nil
}