Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion consul/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consul
import (
"fmt"
"time"
"strings"

consul "github.com/hashicorp/consul/api"
. "github.com/wothing/wonaming/lib"
Expand All @@ -29,6 +30,28 @@ type ConsulWatcher struct {
func (cw *ConsulWatcher) Close() {
}

func (cw *ConsulWatcher) tag() string {
var tag string

split := strings.Split(cw.cr.ServiceName, ".")
if len(split) == 2 {
tag = split[0]
}

return tag
}

func (cw *ConsulWatcher) name() string {
name := cw.cr.ServiceName

split := strings.Split(cw.cr.ServiceName, ".")
if len(split) == 2 {
name = split[1]
}

return name
}

// Next to return the updates
func (cw *ConsulWatcher) Next() ([]*naming.Update, error) {
// Nil cw.addrs means it is initial called
Expand Down Expand Up @@ -73,7 +96,7 @@ func (cw *ConsulWatcher) Next() ([]*naming.Update, error) {
// queryConsul is helper function to query consul
func (cw *ConsulWatcher) queryConsul(q *consul.QueryOptions) ([]string, uint64, error) {
// query consul
cs, meta, err := cw.cc.Health().Service(cw.cr.ServiceName, "", true, q)
cs, meta, err := cw.cc.Health().Service(cw.name(), cw.tag(), true, q)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vectorhacker

If service is v1.hello_sever

It registered in consul as v1. hello_sever-ip-port

Your changes break the law. The service you found now becoming hello_sever not v1. hello_sever

It looks like you forgot to handle the consul Register.

https://github.com/wothing/wonaming/blob/master/consul/register.go#L22

if err != nil {
return nil, 0, err
}
Expand Down