-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from sxllwx/develop
Ftr: etcdv3 for registry
- Loading branch information
Showing
11 changed files
with
1,982 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package etcdv3 | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
import ( | ||
perrors "github.com/pkg/errors" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common" | ||
"github.com/apache/dubbo-go/common/logger" | ||
"github.com/apache/dubbo-go/registry" | ||
"github.com/apache/dubbo-go/remoting" | ||
) | ||
|
||
type dataListener struct { | ||
interestedURL []*common.URL | ||
listener remoting.ConfigurationListener | ||
} | ||
|
||
func NewRegistryDataListener(listener remoting.ConfigurationListener) *dataListener { | ||
return &dataListener{listener: listener, interestedURL: []*common.URL{}} | ||
} | ||
|
||
func (l *dataListener) AddInterestedURL(url *common.URL) { | ||
l.interestedURL = append(l.interestedURL, url) | ||
} | ||
|
||
func (l *dataListener) DataChange(eventType remoting.Event) bool { | ||
|
||
url := eventType.Path[strings.Index(eventType.Path, "/providers/")+len("/providers/"):] | ||
serviceURL, err := common.NewURL(context.Background(), url) | ||
if err != nil { | ||
logger.Warnf("Listen NewURL(r{%s}) = error{%v}", eventType.Path, err) | ||
return false | ||
} | ||
|
||
for _, v := range l.interestedURL { | ||
if serviceURL.URLEqual(*v) { | ||
l.listener.Process(&remoting.ConfigChangeEvent{Key: eventType.Path, Value: serviceURL, ConfigType: eventType.Action}) | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
type configurationListener struct { | ||
registry *etcdV3Registry | ||
events chan *remoting.ConfigChangeEvent | ||
} | ||
|
||
func NewConfigurationListener(reg *etcdV3Registry) *configurationListener { | ||
// add a new waiter | ||
reg.wg.Add(1) | ||
return &configurationListener{registry: reg, events: make(chan *remoting.ConfigChangeEvent, 32)} | ||
} | ||
func (l *configurationListener) Process(configType *remoting.ConfigChangeEvent) { | ||
l.events <- configType | ||
} | ||
|
||
func (l *configurationListener) Next() (*registry.ServiceEvent, error) { | ||
for { | ||
select { | ||
case <-l.registry.done: | ||
logger.Warnf("listener's etcd client connection is broken, so etcd event listener exit now.") | ||
return nil, perrors.New("listener stopped") | ||
|
||
case e := <-l.events: | ||
logger.Infof("got etcd event %#v", e) | ||
if e.ConfigType == remoting.EventTypeDel { | ||
select { | ||
case <-l.registry.done: | ||
logger.Warnf("update @result{%s}. But its connection to registry is invalid", e.Value) | ||
default: | ||
} | ||
continue | ||
} | ||
return ®istry.ServiceEvent{Action: e.ConfigType, Service: e.Value.(common.URL)}, nil | ||
} | ||
} | ||
} | ||
func (l *configurationListener) Close() { | ||
l.registry.wg.Done() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package etcdv3 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
import ( | ||
"github.com/dubbogo/getty" | ||
"github.com/stretchr/testify/suite" | ||
"go.etcd.io/etcd/embed" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common" | ||
"github.com/apache/dubbo-go/remoting" | ||
) | ||
|
||
type RegistryTestSuite struct { | ||
suite.Suite | ||
etcd *embed.Etcd | ||
} | ||
|
||
// start etcd server | ||
func (suite *RegistryTestSuite) SetupSuite() { | ||
|
||
t := suite.T() | ||
|
||
cfg := embed.NewConfig() | ||
cfg.Dir = "/tmp/default.etcd" | ||
e, err := embed.StartEtcd(cfg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
select { | ||
case <-e.Server.ReadyNotify(): | ||
t.Log("Server is ready!") | ||
case <-getty.GetTimeWheel().After(60 * time.Second): | ||
e.Server.Stop() // trigger a shutdown | ||
t.Logf("Server took too long to start!") | ||
} | ||
|
||
suite.etcd = e | ||
return | ||
} | ||
|
||
// stop etcd server | ||
func (suite *RegistryTestSuite) TearDownSuite() { | ||
suite.etcd.Close() | ||
} | ||
|
||
func (suite *RegistryTestSuite) TestDataChange() { | ||
|
||
t := suite.T() | ||
|
||
listener := NewRegistryDataListener(&MockDataListener{}) | ||
url, _ := common.NewURL(context.Background(), "jsonrpc%3A%2F%2F127.0.0.1%3A20001%2Fcom.ikurento.user.UserProvider%3Fanyhost%3Dtrue%26app.version%3D0.0.1%26application%3DBDTService%26category%3Dproviders%26cluster%3Dfailover%26dubbo%3Ddubbo-provider-golang-2.6.0%26environment%3Ddev%26group%3D%26interface%3Dcom.ikurento.user.UserProvider%26ip%3D10.32.20.124%26loadbalance%3Drandom%26methods.GetUser.loadbalance%3Drandom%26methods.GetUser.retries%3D1%26methods.GetUser.weight%3D0%26module%3Ddubbogo%2Buser-info%2Bserver%26name%3DBDTService%26organization%3Dikurento.com%26owner%3DZX%26pid%3D74500%26retries%3D0%26service.filter%3Decho%26side%3Dprovider%26timestamp%3D1560155407%26version%3D%26warmup%3D100") | ||
listener.AddInterestedURL(&url) | ||
if !listener.DataChange(remoting.Event{Path: "/dubbo/com.ikurento.user.UserProvider/providers/jsonrpc%3A%2F%2F127.0.0.1%3A20001%2Fcom.ikurento.user.UserProvider%3Fanyhost%3Dtrue%26app.version%3D0.0.1%26application%3DBDTService%26category%3Dproviders%26cluster%3Dfailover%26dubbo%3Ddubbo-provider-golang-2.6.0%26environment%3Ddev%26group%3D%26interface%3Dcom.ikurento.user.UserProvider%26ip%3D10.32.20.124%26loadbalance%3Drandom%26methods.GetUser.loadbalance%3Drandom%26methods.GetUser.retries%3D1%26methods.GetUser.weight%3D0%26module%3Ddubbogo%2Buser-info%2Bserver%26name%3DBDTService%26organization%3Dikurento.com%26owner%3DZX%26pid%3D74500%26retries%3D0%26service.filter%3Decho%26side%3Dprovider%26timestamp%3D1560155407%26version%3D%26warmup%3D100"}) { | ||
t.Fatal("data change not ok") | ||
} | ||
} | ||
|
||
func TestRegistrySuite(t *testing.T) { | ||
suite.Run(t, &RegistryTestSuite{}) | ||
} | ||
|
||
type MockDataListener struct{} | ||
|
||
func (*MockDataListener) Process(configType *remoting.ConfigChangeEvent) {} |
Oops, something went wrong.