Skip to content

Commit

Permalink
Issue #48: Allow configuration of serviceip used during consul regist…
Browse files Browse the repository at this point in the history
…ration

Add parameter 'registry.consul.register.addr' to
allow overriding the consul registration address
for container environments.
  • Loading branch information
magiconair committed Feb 9, 2016
1 parent 5250bac commit 7280a78
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Issue #41](https://github.com/eBay/fabio/issues/41): Cleanup metrics for deleted routes
* [Issue #43](https://github.com/eBay/fabio/issues/43): Allow configuration via env vars
* [Issue #47](https://github.com/eBay/fabio/issues/47): Move dependencies to vendor path
* [Issue #48](https://github.com/eBay/fabio/issues/48): Allow configuration of serviceip used during consul registration

### [v1.0.8](https://github.com/eBay/fabio/releases/tag/v1.0.8) - 14 Jan 2015

Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Consul struct {
Token string
KVPath string
TagPrefix string
ServiceAddr string
ServiceName string
CheckInterval time.Duration
CheckTimeout time.Duration
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Default = &Config{
Addr: "localhost:8500",
KVPath: "/fabio/config",
TagPrefix: "urlprefix-",
ServiceAddr: ":9998",
ServiceName: "fabio",
CheckInterval: time.Second,
CheckTimeout: 3 * time.Second,
Expand Down
1 change: 1 addition & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func fromProperties(p *properties.Properties) (cfg *Config, err error) {
Token: stringVal(p, Default.Registry.Consul.Token, "registry.consul.token", "consul.token"),
KVPath: stringVal(p, Default.Registry.Consul.KVPath, "registry.consul.kvpath", "consul.kvpath"),
TagPrefix: stringVal(p, Default.Registry.Consul.TagPrefix, "registry.consul.tagprefix", "consul.tagprefix"),
ServiceAddr: stringVal(p, Default.Registry.Consul.ServiceAddr, "registry.consul.register.addr"),
ServiceName: stringVal(p, Default.Registry.Consul.ServiceName, "registry.consul.register.name", "consul.register.name"),
CheckInterval: durationVal(p, Default.Registry.Consul.CheckInterval, "registry.consul.register.checkInterval", "consul.register.checkInterval"),
CheckTimeout: durationVal(p, Default.Registry.Consul.CheckTimeout, "registry.consul.register.checkTimeout", "consul.register.checkTimeout"),
Expand Down
2 changes: 2 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ registry.consul.addr = 1.2.3.4:5678
registry.consul.token = consul-token
registry.consul.kvpath = /some/path
registry.consul.tagprefix = p-
registry.consul.register.addr = 6.6.6.6:7777
registry.consul.register.name = fab
registry.consul.register.checkInterval = 5s
registry.consul.register.checkTimeout = 10s
Expand Down Expand Up @@ -69,6 +70,7 @@ ui.title = fabfab
Token: "consul-token",
KVPath: "/some/path",
TagPrefix: "p-",
ServiceAddr: "6.6.6.6:7777",
ServiceName: "fab",
CheckInterval: 5 * time.Second,
CheckTimeout: 10 * time.Second,
Expand Down
7 changes: 7 additions & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@
# registry.file.path =


# registry.consul.addr configures the address of the consul agent to connect to.
#
# The default is
#
# registry.consul.addr = localhost:8500


# registry.consul.token configures the acl token for consul.
#
# The default is
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func initBackend(cfg *config.Config) {
case "static":
registry.Default, err = static.NewBackend(cfg.Registry.Static.Routes)
case "consul":
registry.Default, err = consul.NewBackend(&cfg.Registry.Consul, cfg.UI.Addr)
registry.Default, err = consul.NewBackend(&cfg.Registry.Consul)
default:
log.Fatal("[FATAL] Unknown registry backend ", cfg.Registry.Backend)
}
Expand Down
9 changes: 4 additions & 5 deletions registry/consul/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ type be struct {
c *api.Client
dc string
cfg *config.Consul
apiAddr string
serviceID string
}

func NewBackend(cfg *config.Consul, apiAddr string) (registry.Backend, error) {
func NewBackend(cfg *config.Consul) (registry.Backend, error) {
// create a reusable client
c, err := api.NewClient(&api.Config{Address: cfg.Addr, Scheme: "http", Token: cfg.Token})
if err != nil {
Expand All @@ -34,19 +33,19 @@ func NewBackend(cfg *config.Consul, apiAddr string) (registry.Backend, error) {

// we're good
log.Printf("[INFO] consul: Connecting to %q in datacenter %q", cfg.Addr, dc)
return &be{c: c, dc: dc, cfg: cfg, apiAddr: apiAddr}, nil
return &be{c: c, dc: dc, cfg: cfg}, nil
}

func (b *be) Register() error {
service, err := serviceRegistration(b.apiAddr, b.cfg.ServiceName, b.cfg.CheckInterval, b.cfg.CheckTimeout)
service, err := serviceRegistration(b.cfg.ServiceAddr, b.cfg.ServiceName, b.cfg.CheckInterval, b.cfg.CheckTimeout)
if err != nil {
return err
}
if err := b.c.Agent().ServiceRegister(service); err != nil {
return err
}

log.Printf("[INFO] consul: Registered fabio as %q", service.ID)
log.Printf("[INFO] consul: Registered fabio as %q with address %q", service.ID, b.cfg.ServiceAddr)
b.serviceID = service.ID
return nil
}
Expand Down

0 comments on commit 7280a78

Please sign in to comment.