1 Star 0 Fork 0

东海苍月/traefik

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
eureka.go 2.71 KB
一键复制 编辑 原始数据 按行查看 历史
Romain 提交于 2020-09-21 10:20 . chore: move to Traefik organization.
package eureka
import (
"io/ioutil"
"time"
"github.com/ArthurHlt/go-eureka-client/eureka"
"github.com/cenk/backoff"
"github.com/containous/flaeg"
"github.com/traefik/traefik/job"
"github.com/traefik/traefik/log"
"github.com/traefik/traefik/provider"
"github.com/traefik/traefik/safe"
"github.com/traefik/traefik/types"
)
// Provider holds configuration of the Provider provider.
type Provider struct {
provider.BaseProvider `mapstructure:",squash" export:"true"`
Endpoint string `description:"Eureka server endpoint"`
Delay flaeg.Duration `description:"Override default configuration time between refresh (Deprecated)" export:"true"` // Deprecated
RefreshSeconds flaeg.Duration `description:"Override default configuration time between refresh" export:"true"`
}
// Init the provider
func (p *Provider) Init(constraints types.Constraints) error {
return p.BaseProvider.Init(constraints)
}
// Provide allows the eureka provider to provide configurations to traefik
// using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool) error {
eureka.GetLogger().SetOutput(ioutil.Discard)
operation := func() error {
client := eureka.NewClient([]string{p.Endpoint})
applications, err := client.GetApplications()
if err != nil {
log.Errorf("Failed to retrieve applications, error: %s", err)
return err
}
configuration, err := p.buildConfiguration(applications)
if err != nil {
log.Errorf("Failed to build configuration for Provider, error: %s", err)
return err
}
configurationChan <- types.ConfigMessage{
ProviderName: "eureka",
Configuration: configuration,
}
ticker := time.NewTicker(time.Duration(p.RefreshSeconds))
pool.Go(func(stop chan bool) {
for {
select {
case t := <-ticker.C:
log.Debugf("Refreshing Provider %s", t.String())
applications, err := client.GetApplications()
if err != nil {
log.Errorf("Failed to retrieve applications, error: %s", err)
continue
}
configuration, err := p.buildConfiguration(applications)
if err != nil {
log.Errorf("Failed to refresh Provider configuration, error: %s", err)
continue
}
configurationChan <- types.ConfigMessage{
ProviderName: "eureka",
Configuration: configuration,
}
case <-stop:
return
}
}
})
return nil
}
err := backoff.RetryNotify(operation, job.NewBackOff(backoff.NewExponentialBackOff()), notify)
if err != nil {
log.Errorf("Cannot connect to Provider server %+v", err)
return err
}
return nil
}
func notify(err error, time time.Duration) {
log.Errorf("Provider connection error %+v, retrying in %s", err, time)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dhcy/traefik.git
git@gitee.com:dhcy/traefik.git
dhcy
traefik
traefik
v1.7.27

搜索帮助