-
Notifications
You must be signed in to change notification settings - Fork 917
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 #633 from dubbo-x/metadata
Ftr: zk/consul Metadata
- Loading branch information
Showing
20 changed files
with
822 additions
and
145 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
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
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,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} | ||
} |
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,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) | ||
} |
Oops, something went wrong.