非Spring Boot、非Java语言项目如何接入Spring Cloud方案

非Spring Boot、非Java语言项目如何接入Spring Cloud方案

阅读 4.4k
2 个回答

spring cloud consul
consul 支持php等 其他语言

之前用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}"
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题