Skip to content

Commit

Permalink
Merge pull request #633 from dubbo-x/metadata
Browse files Browse the repository at this point in the history
Ftr:  zk/consul Metadata
  • Loading branch information
pantianying committed Jul 4, 2020
2 parents 024f7b2 + 302dbc2 commit 5a12137
Show file tree
Hide file tree
Showing 20 changed files with 822 additions and 145 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ classes

# go mod, go test
vendor/
coverage.txt

logs/
.vscode/
coverage.txt

# unit test
remoting/zookeeper/zookeeper-4unittest/
config_center/zookeeper/zookeeper-4unittest/
registry/zookeeper/zookeeper-4unittest/
metadata/report/zookeeper/zookeeper-4unittest/
registry/consul/agent*
config_center/apollo/mockDubbog.properties.json
5 changes: 4 additions & 1 deletion before_ut.bat
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ md cluster\router\chain\zookeeper-4unittest\contrib\fatjar
xcopy /f "%zkJar%" "cluster/router/chain/zookeeper-4unittest/contrib/fatjar/"

md cluster\router\condition\zookeeper-4unittest\contrib\fatjar
xcopy /f "%zkJar%" "cluster/router/condition/zookeeper-4unittest/contrib/fatjar/"
xcopy /f "%zkJar%" "cluster/router/condition/zookeeper-4unittest/contrib/fatjar/"

md metadata\report\zookeeper\zookeeper-4unittest\contrib\fatjar
xcopy /f "%zkJar%" "metadata/report/zookeeper/zookeeper-4unittest/contrib/fatjar/"
9 changes: 6 additions & 3 deletions before_ut.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ if [ ! -f "${zkJar}" ]; then
fi

mkdir -p config_center/zookeeper/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} config_center/zookeeper/zookeeper-4unittest/contrib/fatjar/
cp ${zkJar} config_center/zookeeper/zookeeper-4unittest/contrib/fatjar

mkdir -p registry/zookeeper/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} registry/zookeeper/zookeeper-4unittest/contrib/fatjar/
cp ${zkJar} registry/zookeeper/zookeeper-4unittest/contrib/fatjar

mkdir -p cluster/router/chain/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} cluster/router/chain/zookeeper-4unittest/contrib/fatjar

mkdir -p cluster/router/condition/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} cluster/router/condition/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} cluster/router/condition/zookeeper-4unittest/contrib/fatjar

mkdir -p metadata/report/zookeeper/zookeeper-4unittest/contrib/fatjar
cp ${zkJar} metadata/report/zookeeper/zookeeper-4unittest/contrib/fatjar
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/creasty/defaults v1.3.0
github.com/dubbogo/getty v1.3.5
github.com/dubbogo/go-zookeeper v1.0.0
github.com/dubbogo/go-zookeeper v1.0.1
github.com/dubbogo/gost v1.9.0
github.com/emicklei/go-restful/v3 v3.0.0
github.com/go-co-op/gocron v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dubbogo/getty v1.3.5 h1:xJxdDj9jm7wlrRSsVZSk2TDNxJbbac5GpxV0QpjO+Tw=
github.com/dubbogo/getty v1.3.5/go.mod h1:T55vN8Q6tZjf2AQZiGmkujneD3LfqYbv2b3QjacwYOY=
github.com/dubbogo/go-zookeeper v1.0.0 h1:RsYdlGwhDW+iKXM3eIIcvt34P2swLdmQfuIJxsHlGoM=
github.com/dubbogo/go-zookeeper v1.0.0/go.mod h1:fn6n2CAEer3novYgk9ULLwAjuV8/g4DdC2ENwRb6E+c=
github.com/dubbogo/go-zookeeper v1.0.1 h1:irLzvOsDOTNsN8Sv9tvYYxVu6DCQfLtziZQtUHmZgz8=
github.com/dubbogo/go-zookeeper v1.0.1/go.mod h1:fn6n2CAEer3novYgk9ULLwAjuV8/g4DdC2ENwRb6E+c=
github.com/dubbogo/gost v1.5.1/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8=
github.com/dubbogo/gost v1.9.0 h1:UT+dWwvLyJiDotxJERO75jB3Yxgsdy10KztR5ycxRAk=
github.com/dubbogo/gost v1.9.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8=
Expand Down
125 changes: 125 additions & 0 deletions metadata/report/consul/report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package consul

import (
consul "github.com/hashicorp/consul/api"
)

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/metadata/identifier"
"github.com/apache/dubbo-go/metadata/report"
"github.com/apache/dubbo-go/metadata/report/factory"
)

var (
emptyStrSlice = make([]string, 0)
)

func init() {
mf := &consulMetadataReportFactory{}
extension.SetMetadataReportFactory("consul", func() factory.MetadataReportFactory {
return mf
})
}

// consulMetadataReport is the implementation of
// MetadataReport based on consul.
type consulMetadataReport struct {
client *consul.Client
}

// StoreProviderMetadata stores the metadata.
func (m *consulMetadataReport) StoreProviderMetadata(providerIdentifier *identifier.MetadataIdentifier, serviceDefinitions string) error {
kv := &consul.KVPair{Key: providerIdentifier.GetIdentifierKey(), Value: []byte(serviceDefinitions)}
_, err := m.client.KV().Put(kv, nil)
return err
}

// StoreConsumerMetadata stores the metadata.
func (m *consulMetadataReport) StoreConsumerMetadata(consumerMetadataIdentifier *identifier.MetadataIdentifier, serviceParameterString string) error {
kv := &consul.KVPair{Key: consumerMetadataIdentifier.GetIdentifierKey(), Value: []byte(serviceParameterString)}
_, err := m.client.KV().Put(kv, nil)
return err
}

// SaveServiceMetadata saves the metadata.
func (m *consulMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier, url common.URL) error {
kv := &consul.KVPair{Key: metadataIdentifier.GetIdentifierKey(), Value: []byte(url.String())}
_, err := m.client.KV().Put(kv, nil)
return err
}

// RemoveServiceMetadata removes the metadata.
func (m *consulMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier) error {
k := metadataIdentifier.GetIdentifierKey()
_, err := m.client.KV().Delete(k, nil)
return err
}

// GetExportedURLs gets the urls.
func (m *consulMetadataReport) GetExportedURLs(metadataIdentifier *identifier.ServiceMetadataIdentifier) ([]string, error) {
k := metadataIdentifier.GetIdentifierKey()
kv, _, err := m.client.KV().Get(k, nil)
if err != nil || kv == nil {
return emptyStrSlice, err
}
return []string{string(kv.Value)}, nil
}

// SaveSubscribedData saves the urls.
func (m *consulMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier, urls string) error {
kv := &consul.KVPair{Key: subscriberMetadataIdentifier.GetIdentifierKey(), Value: []byte(urls)}
_, err := m.client.KV().Put(kv, nil)
return err
}

// GetSubscribedURLs gets the urls.
func (m *consulMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier) ([]string, error) {
k := subscriberMetadataIdentifier.GetIdentifierKey()
kv, _, err := m.client.KV().Get(k, nil)
if err != nil || kv == nil {
return emptyStrSlice, err
}
return []string{string(kv.Value)}, nil
}

// GetServiceDefinition gets the service definition.
func (m *consulMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) (string, error) {
k := metadataIdentifier.GetIdentifierKey()
kv, _, err := m.client.KV().Get(k, nil)
if err != nil || kv == nil {
return "", err
}
return string(kv.Value), nil
}

type consulMetadataReportFactory struct {
}

// nolint
func (mf *consulMetadataReportFactory) CreateMetadataReport(url *common.URL) report.MetadataReport {
config := &consul.Config{Address: url.Location}
client, err := consul.NewClient(config)
if err != nil {
panic(err)
}
return &consulMetadataReport{client: client}
}
165 changes: 165 additions & 0 deletions metadata/report/consul/report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package consul

import (
"encoding/json"
"net/url"
"strconv"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/metadata/identifier"
"github.com/apache/dubbo-go/metadata/report"
"github.com/apache/dubbo-go/remoting/consul"
)

func newProviderRegistryUrl(host string, port int) *common.URL {
return common.NewURLWithOptions(
common.WithIp(host),
common.WithPort(strconv.Itoa(port)),
common.WithParams(url.Values{}),
common.WithParamsValue(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER)),
)
}

func newBaseMetadataIdentifier(side string) *identifier.BaseMetadataIdentifier {
return &identifier.BaseMetadataIdentifier{
ServiceInterface: "org.apache.HelloWorld",
Version: "1.0.0",
Group: "group",
Side: side,
}
}

func newMetadataIdentifier(side string) *identifier.MetadataIdentifier {
return &identifier.MetadataIdentifier{
Application: "application",
BaseMetadataIdentifier: *newBaseMetadataIdentifier(side),
}
}

func newServiceMetadataIdentifier(side string) *identifier.ServiceMetadataIdentifier {
return &identifier.ServiceMetadataIdentifier{
Revision: "1.0",
Protocol: "dubbo",
BaseMetadataIdentifier: *newBaseMetadataIdentifier(side),
}
}

func newSubscribeMetadataIdentifier(side string) *identifier.SubscriberMetadataIdentifier {
return &identifier.SubscriberMetadataIdentifier{
Revision: "1.0",
MetadataIdentifier: *newMetadataIdentifier(side),
}
}

type consulMetadataReportTestSuite struct {
t *testing.T
m report.MetadataReport
}

func newConsulMetadataReportTestSuite(t *testing.T, m report.MetadataReport) *consulMetadataReportTestSuite {
return &consulMetadataReportTestSuite{t: t, m: m}
}

func (suite *consulMetadataReportTestSuite) testStoreProviderMetadata() {
providerMi := newMetadataIdentifier("provider")
providerMeta := "provider"
err := suite.m.StoreProviderMetadata(providerMi, providerMeta)
assert.NoError(suite.t, err)
}

func (suite *consulMetadataReportTestSuite) testStoreConsumerMetadata() {
consumerMi := newMetadataIdentifier("consumer")
consumerMeta := "consumer"
err := suite.m.StoreProviderMetadata(consumerMi, consumerMeta)
assert.NoError(suite.t, err)
}

func (suite *consulMetadataReportTestSuite) testSaveServiceMetadata(url common.URL) {
serviceMi := newServiceMetadataIdentifier("provider")
err := suite.m.SaveServiceMetadata(serviceMi, url)
assert.NoError(suite.t, err)
}

func (suite *consulMetadataReportTestSuite) testRemoveServiceMetadata() {
serviceMi := newServiceMetadataIdentifier("provider")
err := suite.m.RemoveServiceMetadata(serviceMi)
assert.NoError(suite.t, err)
}

func (suite *consulMetadataReportTestSuite) testGetExportedURLs() {
serviceMi := newServiceMetadataIdentifier("provider")
urls, err := suite.m.GetExportedURLs(serviceMi)
assert.Equal(suite.t, 1, len(urls))
assert.NoError(suite.t, err)
}

func (suite *consulMetadataReportTestSuite) testSaveSubscribedData(url common.URL) {
subscribeMi := newSubscribeMetadataIdentifier("provider")
urls := []string{url.String()}
bytes, _ := json.Marshal(urls)
err := suite.m.SaveSubscribedData(subscribeMi, string(bytes))
assert.Nil(suite.t, err)
}

func (suite *consulMetadataReportTestSuite) testGetSubscribedURLs() {
subscribeMi := newSubscribeMetadataIdentifier("provider")
urls, err := suite.m.GetSubscribedURLs(subscribeMi)
assert.Equal(suite.t, 1, len(urls))
assert.NoError(suite.t, err)
}

func (suite *consulMetadataReportTestSuite) testGetServiceDefinition() {
providerMi := newMetadataIdentifier("provider")
providerMeta, err := suite.m.GetServiceDefinition(providerMi)
assert.Equal(suite.t, "provider", providerMeta)
assert.NoError(suite.t, err)
}

func test1(t *testing.T) {
consulAgent := consul.NewConsulAgent(t, 8500)
defer consulAgent.Close()

url := newProviderRegistryUrl("localhost", 8500)
mf := extension.GetMetadataReportFactory("consul")
m := mf.CreateMetadataReport(url)

suite := newConsulMetadataReportTestSuite(t, m)
suite.testStoreProviderMetadata()
suite.testStoreConsumerMetadata()
suite.testSaveServiceMetadata(*url)
suite.testGetExportedURLs()
suite.testRemoveServiceMetadata()
suite.testSaveSubscribedData(*url)
suite.testGetSubscribedURLs()
suite.testGetServiceDefinition()
}

func TestConsulMetadataReport(t *testing.T) {
t.Run("test1", test1)
}
Loading

0 comments on commit 5a12137

Please sign in to comment.