forked from xuanbo/eureka-client
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
144 lines (133 loc) · 6.23 KB
/
config.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
package eureka_client
import (
"fmt"
)
// Config eureka 客户端配置
type Config struct {
// eureka 服务端地址
DefaultZone string
// 心跳间隔,默认 30s
RenewalIntervalInSecs int
// 获取服务列表间隔,默认 15s
RegistryFetchIntervalSeconds int
// 过期间隔,默认 90s
DurationInSecs int
// 实例ID,默认 app:ip:port
InstanceID string
// 应用名称
App string
// 应用版本
Version string
// Host,为空则取 IP
HostName string
// IP,为空则取本地 IP
IP string
// 端口,默认 80
Port int
// 元数据
Metadata map[string]interface{}
}
// Applications eureka 服务端注册的 apps
type Applications struct {
VersionsDelta string `xml:"versions__delta,omitempty" json:"versions__delta,omitempty"`
AppsHashcode string `xml:"apps__hashcode,omitempty" json:"apps__hashcode,omitempty"`
Applications []Application `xml:"application,omitempty" json:"application,omitempty"`
}
// Application eureka 服务端注册的 app
type Application struct {
Name string `xml:"name" json:"name"`
Instances []Instance `xml:"instance" json:"instance"`
}
// Instance 服务实例
type Instance struct {
HostName string `xml:"hostName" json:"hostName"`
HomePageURL string `xml:"homePageUrl,omitempty" json:"homePageUrl,omitempty"`
StatusPageURL string `xml:"statusPageUrl" json:"statusPageUrl"`
HealthCheckURL string `xml:"healthCheckUrl,omitempty" json:"healthCheckUrl,omitempty"`
App string `xml:"app" json:"app"`
Version string `xml:"version" json:"version"`
IPAddr string `xml:"ipAddr" json:"ipAddr"`
VipAddress string `xml:"vipAddress" json:"vipAddress"`
SecureVipAddress string `xml:"secureVipAddress,omitempty" json:"secureVipAddress,omitempty"`
Status string `xml:"status" json:"status"`
Port *Port `xml:"port,omitempty" json:"port,omitempty"`
SecurePort *Port `xml:"securePort,omitempty" json:"securePort,omitempty"`
DataCenterInfo *DataCenterInfo `xml:"dataCenterInfo" json:"dataCenterInfo"`
LeaseInfo *LeaseInfo `xml:"leaseInfo,omitempty" json:"leaseInfo,omitempty"`
Metadata map[string]interface{} `xml:"metadata,omitempty" json:"metadata,omitempty"`
IsCoordinatingDiscoveryServer string `xml:"isCoordinatingDiscoveryServer,omitempty" json:"isCoordinatingDiscoveryServer,omitempty"`
LastUpdatedTimestamp string `xml:"lastUpdatedTimestamp,omitempty" json:"lastUpdatedTimestamp,omitempty"`
LastDirtyTimestamp string `xml:"lastDirtyTimestamp,omitempty" json:"lastDirtyTimestamp,omitempty"`
ActionType string `xml:"actionType,omitempty" json:"actionType,omitempty"`
OverriddenStatus string `xml:"overriddenstatus,omitempty" json:"overriddenstatus,omitempty"`
CountryID int `xml:"countryId,omitempty" json:"countryId,omitempty"`
InstanceID string `xml:"instanceId,omitempty" json:"instanceId,omitempty"`
EurekaConfig *Config
Beater *BeatReactor
}
// Port 端口
type Port struct {
Port int `xml:",chardata" json:"$"`
Enabled string `xml:"enabled,attr" json:"@enabled"`
}
// DataCenterInfo 数据中心信息
type DataCenterInfo struct {
Name string `xml:"name" json:"name"`
Class string `xml:"class,attr" json:"@class"`
Metadata *DataCenterMetadata `xml:"metadata,omitempty" json:"metadata,omitempty"`
}
// DataCenterMetadata 数据中心信息元数据
type DataCenterMetadata struct {
AmiLaunchIndex string `xml:"ami-launch-index,omitempty" json:"ami-launch-index,omitempty"`
LocalHostname string `xml:"local-hostname,omitempty" json:"local-hostname,omitempty"`
AvailabilityZone string `xml:"availability-zone,omitempty" json:"availability-zone,omitempty"`
InstanceID string `xml:"instance-id,omitempty" json:"instance-id,omitempty"`
PublicIpv4 string `xml:"public-ipv4,omitempty" json:"public-ipv4,omitempty"`
PublicHostname string `xml:"public-hostname,omitempty" json:"public-hostname,omitempty"`
AmiManifestPath string `xml:"ami-manifest-path,omitempty" json:"ami-manifest-path,omitempty"`
LocalIpv4 string `xml:"local-ipv4,omitempty" json:"local-ipv4,omitempty"`
Hostname string `xml:"hostname,omitempty" json:"hostname,omitempty"`
AmiID string `xml:"ami-id,omitempty" json:"ami-id,omitempty"`
InstanceType string `xml:"instance-type,omitempty" json:"instance-type,omitempty"`
}
// LeaseInfo 续约信息
type LeaseInfo struct {
RenewalIntervalInSecs int `xml:"renewalIntervalInSecs,omitempty" json:"renewalIntervalInSecs,omitempty"`
DurationInSecs int `xml:"durationInSecs,omitempty" json:"durationInSecs,omitempty"`
}
// NewInstance 创建服务实例
func NewInstance(config *Config) *Instance {
instance := &Instance{
InstanceID: config.InstanceID,
HostName: config.HostName,
App: config.App,
Version: config.Version,
IPAddr: config.IP,
Port: &Port{
Port: config.Port,
Enabled: "true",
},
VipAddress: config.App,
SecureVipAddress: config.App,
// 续约信息
LeaseInfo: &LeaseInfo{
RenewalIntervalInSecs: config.RenewalIntervalInSecs,
DurationInSecs: config.DurationInSecs,
},
Status: "UP",
OverriddenStatus: "UNKNOWN",
// 数据中心
DataCenterInfo: &DataCenterInfo{
Name: "MyOwn",
Class: "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
},
// 元数据
Metadata: config.Metadata,
}
instance.HomePageURL = fmt.Sprintf("%s://%s:%d", "http", config.IP, config.Port)
instance.StatusPageURL = fmt.Sprintf("%s://%s:%d/info", "http", config.IP, config.Port)
instance.EurekaConfig = config
beater := NewBeatReactor(config, int64(config.RenewalIntervalInSecs))
instance.Beater = &beater
return instance
}