-
Notifications
You must be signed in to change notification settings - Fork 337
/
microservice.go
142 lines (125 loc) · 3.77 KB
/
microservice.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
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed 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 core
import (
"github.com/ServiceComb/service-center/pkg/util"
pb "github.com/ServiceComb/service-center/server/core/proto"
"github.com/ServiceComb/service-center/version"
"golang.org/x/net/context"
)
var Service *pb.MicroService
var Instance *pb.MicroServiceInstance
const (
REGISTRY_DOMAIN = "default"
REGISTRY_PROJECT = "default"
registry_app_id = "default"
registry_service_name = "SERVICECENTER"
registry_default_instance_env = "production"
REGISTRY_DEFAULT_LEASE_RENEWALINTERVAL int32 = 30
REGISTRY_DEFAULT_LEASE_RETRYTIMES int32 = 3
IS_SC_SELF = "sc_self"
DEFAULT_REGION = "default"
DEFAULT_AVAILABLEZONE = "default"
)
func init() {
Service = &pb.MicroService{
AppId: registry_app_id,
ServiceName: registry_service_name,
Version: version.Ver().Version,
Status: pb.MS_UP,
Level: "BACK",
Schemas: []string{
"servicecenter.grpc.api.ServiceCtrl",
"servicecenter.grpc.api.ServiceInstanceCtrl",
},
Properties: map[string]string{
pb.PROP_ALLOW_CROSS_APP: "true",
},
}
Instance = &pb.MicroServiceInstance{
Environment: registry_default_instance_env,
Status: pb.MSI_UP,
HealthCheck: &pb.HealthCheck{
Mode: pb.CHECK_BY_HEARTBEAT,
Interval: REGISTRY_DEFAULT_LEASE_RENEWALINTERVAL,
Times: REGISTRY_DEFAULT_LEASE_RETRYTIMES,
},
}
}
func AddDefaultContextValue(ctx context.Context) context.Context {
ctx = util.NewContext(ctx, "domain", REGISTRY_DOMAIN)
ctx = util.NewContext(ctx, "project", REGISTRY_PROJECT)
ctx = util.NewContext(ctx, IS_SC_SELF, true)
return ctx
}
func IsDefaultDomainProject(domainProject string) bool {
return domainProject == util.StringJoin([]string{REGISTRY_DOMAIN, REGISTRY_PROJECT}, "/")
}
func ISSCSelf(ctx context.Context) bool {
if ctx.Value(IS_SC_SELF) != nil && ctx.Value(IS_SC_SELF).(bool) {
return true
}
return false
}
func GetExistenceRequest() *pb.GetExistenceRequest {
return &pb.GetExistenceRequest{
Type: pb.EXISTENCE_MS,
AppId: registry_app_id,
ServiceName: registry_service_name,
Version: version.Ver().Version,
}
}
func GetServiceRequest(serviceId string) *pb.GetServiceRequest {
return &pb.GetServiceRequest{
ServiceId: serviceId,
}
}
func CreateServiceRequest() *pb.CreateServiceRequest {
return &pb.CreateServiceRequest{
Service: Service,
}
}
func RegisterInstanceRequest(hostName string, endpoints []string) *pb.RegisterInstanceRequest {
Instance.HostName = hostName
Instance.Endpoints = endpoints
return &pb.RegisterInstanceRequest{
Instance: Instance,
}
}
func UnregisterInstanceRequest() *pb.UnregisterInstanceRequest {
return &pb.UnregisterInstanceRequest{
ServiceId: Instance.ServiceId,
InstanceId: Instance.InstanceId,
}
}
func HeartbeatRequest() *pb.HeartbeatRequest {
return &pb.HeartbeatRequest{
ServiceId: Instance.ServiceId,
InstanceId: Instance.InstanceId,
}
}
func GetRegionAndAvailableZone(in *pb.DataCenterInfo) (region string, availableZone string){
if in == nil {
return DEFAULT_REGION, DEFAULT_AVAILABLEZONE
}
region = in.Region
if region == "" {
region = DEFAULT_REGION
}
availableZone = in.AvailableZone
if availableZone == "" {
availableZone = DEFAULT_AVAILABLEZONE
}
return
}