非Spring Boot、非Java语言项目如何接入Spring Cloud方案
之前用Go写过一个接入eureka的,其他语言应该差不多的
http 请求
package eureka
import (
"attachment/config"
"fmt"
"attachment/util"
"strings"
"os"
"io/ioutil"
"time"
)
var instanceID string
var basicUrl string
var statusUrl string
// Register ...
func Register() {
instanceID = util.GetUUID()
basicUrl = strings.Join([]string{config.GetEurekaServer(), "apps/", config.GetApplicationName()}, "")
statusUrl = strings.Join([]string{fmt.Sprintf("%s/%s", basicUrl, util.GetLocalIP()), config.GetApplicationName(), instanceID}, ":")
dir, _ := os.Getwd()
data, _ := ioutil.ReadFile(dir + "/resources/regtpl.json")
tpl := string(data)
tpl = strings.Replace(tpl, "${ip.address}", util.GetLocalIP(), -1)
tpl = strings.Replace(tpl, "${port}", config.GetPort(), -1)
tpl = strings.Replace(tpl, "${instanceID}", instanceID, -1)
tpl = strings.Replace(tpl, "${application.name}", config.GetApplicationName(), -1)
registerAction := HttpAction{
URL: basicUrl,
Method: "POST",
ContentType: "application/json",
Body: tpl,
}
var result bool
for {
result = DoHttpRequest(registerAction)
if result {
break
} else {
time.Sleep(time.Second * 5)
}
}
}
// StartHeartbeat ...
func StartHeartbeat() {
for {
time.Sleep(time.Second * 30)
heartbeat()
}
}
func heartbeat() {
heartbeatAction := HttpAction{
URL: statusUrl,
Method: "PUT",
}
DoHttpRequest(heartbeatAction)
}
// Deregister ...
func Deregister() {
fmt.Println("Trying to deregister application...")
deregisterAction := HttpAction{
URL: statusUrl,
Method: "DELETE",
}
DoHttpRequest(deregisterAction)
fmt.Println("Deregistered application, exiting. Check Eureka...")
}
regtpl.json
{
"instance": {
"hostName": "${ip.address}",
"app": "${application.name}",
"ipAddr": "${ip.address}",
"vipAddress": "${application.name}",
"status": "UP",
"port": {
"$": "${port}",
"@enabled": true
},
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"metadata": {
"instanceId": "${application.name}:${instanceID}"
}
}
}
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4.1k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
3 回答1.7k 阅读✓ 已解决
spring cloud consul
consul 支持php等 其他语言