目录
1.简介
提供Go微服务客户端注册到Eureka中心。点击:github地址,欢迎各位多多star!
(已通过测试验证,用于正式生产部署)
2.原理
goeureka主要是通过REST请求来与server进行通信。
com.netflix.discovery.DiscoveryClient
3.注册信息
注册eureka服务端的配置信息如下:
{
"instance": {
"instanceId" : "${ipAddress}:${appName}:${port}",
"hostName":"${ipAddress}",
"app":"${appName}",
"ipAddr":"${ipAddress}",
"vipAddress":"${appName}",
"overriddenstatus": "UNKNOWN",
"status":"UP",
"port": {
"$":${port},
"@enabled": true
},
"securePort": {
"$":${securePort},
"@enabled": false
},
"homePageUrl" : "http://${ipAddress}:${port}/",
"statusPageUrl": "http://${ipAddress}:${port}/info",
"healthCheckUrl": "http://${ipAddress}:${port}/health",
"dataCenterInfo" : {
"@class":"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"metadata": {
"management.port" : "${port}"
}
}
}
4.功能特性
本项目封装了Eureka rest操作,实现了以下功能:
-
register
-
sendheartbeat
-
deregister
-
instances
调用前三个特性register 、heartbeat 和deregister基本上可以实现微服务注册到Eureka中心,其中appid实例是客户端的名称。
5.使用方法
工具包的导入方法:
import "github.com/SimonWang00/goeureka"
如果您默认使用本地启动的Eureka Server,注册代码如下:
goeureka.RegisterClient("http://127.0.0.1:8761","my-goserver", "8000", "43")
Notes: RegisterClient 为web server对应的端口
6.接入示例
在http中使用:
import (
"fmt"
"github.com/SimonWang00/goeureka"
"net/http"
)
func main() {
goeureka.RegisterClient("http://127.0.0.1:8761","myapp", "8000", "43")
http.HandleFunc("/hello", func(responseWriter http.ResponseWriter, request *http.Request) {
resp := "hello goeureka!"
_, _ = responseWriter.Write([]byte(resp))
})
// start server
if err := http.ListenAndServe("127.0.0.1:8000", nil); err != nil {
fmt.Println(err)
}
}
在gin框架中使用:
import (
"github.com/SimonWang00/goeureka"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("hello", func(c *gin.Context) {
c.String(200, "hello goeureka")
})
goeureka.RegisterClient("http://127.0.0.1:8761","myapp", "8000", "43")
r.Run("127.0.0.1:8000")
}
7.测试
java端测试代码,内涵gateway-eureka.jar