forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchassis.go
executable file
·137 lines (127 loc) · 4.27 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
package chassis
import (
"log"
"os"
"os/signal"
"strings"
"syscall"
//init logger first
_ "github.com/go-chassis/go-chassis/initiator"
//load balancing
_ "github.com/go-chassis/go-chassis/pkg/loadbalancing"
//protocols
_ "github.com/go-chassis/go-chassis/client/rest"
_ "github.com/go-chassis/go-chassis/server/restful"
//routers
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/handler"
"github.com/go-chassis/go-chassis/core/registry"
//router
_ "github.com/go-chassis/go-chassis/core/router/servicecomb"
//control panel
_ "github.com/go-chassis/go-chassis/control/archaius"
// registry
_ "github.com/go-chassis/go-chassis/core/registry/file"
_ "github.com/go-chassis/go-chassis/core/registry/servicecenter"
"github.com/go-chassis/go-chassis/core/server"
// prometheus reporter for circuit breaker metrics
_ "github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/reporter"
// aes package handles security related plugins
_ "github.com/go-chassis/go-chassis/security/plugins/aes"
_ "github.com/go-chassis/go-chassis/security/plugins/plain"
//config centers
_ "github.com/go-chassis/go-chassis-config/configcenter"
"github.com/go-chassis/go-chassis/core/metadata"
"github.com/go-mesh/openlogging"
)
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
}
//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() error {
err := goChassis.start()
if err != nil {
openlogging.Error("run chassis failed:" + err.Error())
return err
}
if !config.GetRegistratorDisable() {
//Register instance after Server started
if err := registry.DoRegister(); err != nil {
openlogging.Error("register instance failed:" + err.Error())
return err
}
}
waitingSignal()
return nil
}
func waitingSignal() {
//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:
openlogging.Info("got os signal " + s.String())
case err := <-server.ErrRuntime:
openlogging.Info("got server error " + err.Error())
}
for name, s := range server.GetServers() {
openlogging.Info("stopping server " + name + "...")
err := s.Stop()
if err != nil {
openlogging.GetLogger().Warnf("servers failed to stop: %s", err)
}
openlogging.Info(name + " server stop success")
}
if !config.GetRegistratorDisable() {
if err := server.UnRegistrySelfInstances(); err != nil {
openlogging.GetLogger().Warnf("servers failed to unregister: %s", err)
}
}
openlogging.Info("go chassis server gracefully shutdown")
}
//Init prepare the chassis framework runtime
func Init() error {
if goChassis.DefaultConsumerChainNames == nil {
defaultChain := strings.Join([]string{
handler.RatelimiterConsumer,
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,
}, ",")
goChassis.DefaultProviderChainNames = map[string]string{
common.DefaultKey: defaultChain,
}
}
if err := goChassis.initialize(); err != nil {
log.Println("init chassis fail:", err)
return err
}
openlogging.GetLogger().Infof("init chassis success, version is %s", metadata.SdkVersion)
return nil
}