当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
eureka.go 2.82 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2017-01-01 20:29 . Add package comments
// Package eureka provides a Eureka registry
package eureka
/*
Eureka is a plugin for Netflix Eureka service discovery
*/
import (
"net/http"
"time"
"golang.org/x/net/context"
"github.com/hudl/fargo"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/registry"
"github.com/op/go-logging"
)
type fargoConnection interface {
RegisterInstance(*fargo.Instance) error
DeregisterInstance(*fargo.Instance) error
HeartBeatInstance(*fargo.Instance) error
GetInstance(string, string) (*fargo.Instance, error)
GetApp(string) (*fargo.Application, error)
GetApps() (map[string]*fargo.Application, error)
ScheduleAppUpdates(string, bool, <-chan struct{}) <-chan fargo.AppUpdate
}
type eurekaRegistry struct {
conn fargoConnection
opts registry.Options
}
func init() {
cmd.DefaultRegistries["eureka"] = NewRegistry
logging.SetLevel(logging.ERROR, "fargo")
}
func newRegistry(opts ...registry.Option) registry.Registry {
options := registry.Options{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
var cAddrs []string
for _, addr := range options.Addrs {
if len(addr) == 0 {
continue
}
cAddrs = append(cAddrs, addr)
}
if len(cAddrs) == 0 {
cAddrs = []string{"http://localhost:8080/eureka/v2"}
}
if c, ok := options.Context.Value(contextHttpClient{}).(*http.Client); ok {
fargo.HttpClient = c
}
conn := fargo.NewConn(cAddrs...)
conn.PollInterval = time.Second * 5
return &eurekaRegistry{
conn: &conn,
opts: options,
}
}
func (e *eurekaRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
instance, err := serviceToInstance(s)
if err != nil {
return err
}
if e.instanceRegistered(instance) {
return e.conn.HeartBeatInstance(instance)
}
return e.conn.RegisterInstance(instance)
}
func (e *eurekaRegistry) Deregister(s *registry.Service) error {
instance, err := serviceToInstance(s)
if err != nil {
return err
}
return e.conn.DeregisterInstance(instance)
}
func (e *eurekaRegistry) GetService(name string) ([]*registry.Service, error) {
app, err := e.conn.GetApp(name)
if err != nil {
return nil, err
}
return appToService(app), nil
}
func (e *eurekaRegistry) ListServices() ([]*registry.Service, error) {
var services []*registry.Service
apps, err := e.conn.GetApps()
if err != nil {
return nil, err
}
for _, app := range apps {
services = append(services, appToService(app)...)
}
return services, nil
}
func (e *eurekaRegistry) Watch() (registry.Watcher, error) {
return newWatcher(e.conn), nil
}
func (e *eurekaRegistry) String() string {
return "eureka"
}
func (e *eurekaRegistry) instanceRegistered(instance *fargo.Instance) bool {
_, err := e.conn.GetInstance(instance.App, instance.UniqueID(*instance))
return err == nil
}
func NewRegistry(opts ...registry.Option) registry.Registry {
return newRegistry(opts...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.3.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385