1 Star 1 Fork 0

flash520/utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
eureka.go 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* @Author: koulei
* @Description: TODO
* @File: eureka
* @Version: 1.0.0
* @Date: 2021/7/3 18:28
*/
package discovery
import (
"errors"
"strings"
"sync"
eureka "github.com/xuanbo/eureka-client"
)
type Eureka struct {
}
var eurekaClient *eureka.Client
func CreateEureka(username, password, node, app string, port int) *Eureka {
var once sync.Once
once.Do(func() {
eurekaClient = eureka.NewClient(&eureka.Config{
DefaultZone: "http://" + username + ":" + password + "@" + node,
App: app,
Port: port,
RenewalIntervalInSecs: 15,
DurationInSecs: 30,
Metadata: map[string]interface{}{
"VERSION": "0.1.0",
"NODE_GROUP_ID": 0,
"PRODUCT_CODE": "DEFAULT",
"PRODUCT_VERSION_CODE": "DEFAULT",
"PRODUCT_ENV_CODE": "DEFAULT",
"SERVICE_VERSION_CODE": "DEFAULT",
},
})
})
go eurekaClient.Start()
return &Eureka{}
}
func (e *Eureka) GetInstance(serviceName string) (instance string, err error) {
apps := eurekaClient.Applications
for _, v := range apps.Applications {
if v.Name == strings.ToUpper(serviceName) {
for i := 0; i < len(v.Instances); i++ {
instance = v.Instances[i].HomePageURL
return instance, nil
}
break
}
}
err = errors.New(strings.ToUpper(instance + " " + "找不到该服务"))
return instance, err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/flash520/utils.git
git@gitee.com:flash520/utils.git
flash520
utils
utils
v1.6.5

搜索帮助