-
Notifications
You must be signed in to change notification settings - Fork 932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nacos MetadataReport implementation #522
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
/* | ||
* 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 nacos | ||
|
||
import ( | ||
"encoding/json" | ||
"net/url" | ||
) | ||
|
||
import ( | ||
"github.com/nacos-group/nacos-sdk-go/clients/config_client" | ||
"github.com/nacos-group/nacos-sdk-go/vo" | ||
perrors "github.com/pkg/errors" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common" | ||
"github.com/apache/dubbo-go/common/extension" | ||
"github.com/apache/dubbo-go/common/logger" | ||
"github.com/apache/dubbo-go/metadata/identifier" | ||
"github.com/apache/dubbo-go/metadata/report" | ||
"github.com/apache/dubbo-go/metadata/report/factory" | ||
"github.com/apache/dubbo-go/remoting/nacos" | ||
) | ||
|
||
func init() { | ||
ftry := &nacosMetadataReportFactory{} | ||
extension.SetMetadataReportFactory("nacos", func() factory.MetadataReportFactory { | ||
return ftry | ||
}) | ||
} | ||
|
||
// nacosMetadataReport is the implementation of MetadataReport based Nacos | ||
type nacosMetadataReport struct { | ||
client config_client.IConfigClient | ||
} | ||
|
||
// StoreProviderMetadata will store the metadata | ||
// metadata including the basic info of the server, provider info, and other user custom info | ||
func (n *nacosMetadataReport) StoreProviderMetadata(providerIdentifier *identifier.MetadataIdentifier, serviceDefinitions string) error { | ||
return n.storeMetadata(vo.ConfigParam{ | ||
DataId: providerIdentifier.GetIdentifierKey(), | ||
Group: providerIdentifier.Group, | ||
Content: serviceDefinitions, | ||
}) | ||
} | ||
|
||
// StoreConsumerMetadata will store the metadata | ||
flycash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// metadata including the basic info of the server, consumer info, and other user custom info | ||
func (n *nacosMetadataReport) StoreConsumerMetadata(consumerMetadataIdentifier *identifier.MetadataIdentifier, serviceParameterString string) error { | ||
return n.storeMetadata(vo.ConfigParam{ | ||
DataId: consumerMetadataIdentifier.GetIdentifierKey(), | ||
Group: consumerMetadataIdentifier.Group, | ||
Content: serviceParameterString, | ||
}) | ||
} | ||
|
||
// SaveServiceMetadata will store the metadata | ||
flycash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// metadata including the basic info of the server, service info, and other user custom info | ||
func (n *nacosMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier, url common.URL) error { | ||
return n.storeMetadata(vo.ConfigParam{ | ||
DataId: metadataIdentifier.GetIdentifierKey(), | ||
Group: metadataIdentifier.Group, | ||
Content: url.String(), | ||
}) | ||
} | ||
|
||
// RemoveServiceMetadata will remove the service metadata | ||
func (n *nacosMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier) error { | ||
return n.deleteMetadata(vo.ConfigParam{ | ||
DataId: metadataIdentifier.GetIdentifierKey(), | ||
Group: metadataIdentifier.Group, | ||
}) | ||
} | ||
|
||
// GetExportedURLs will look up the exported urls. | ||
// if not found, an empty list will be returned. | ||
func (n *nacosMetadataReport) GetExportedURLs(metadataIdentifier *identifier.ServiceMetadataIdentifier) []string { | ||
return n.getConfigAsArray(vo.ConfigParam{ | ||
DataId: metadataIdentifier.GetIdentifierKey(), | ||
Group: metadataIdentifier.Group, | ||
}) | ||
} | ||
|
||
// SaveSubscribedData will convert the urlList to json array and then store it | ||
func (n *nacosMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier, urlList []common.URL) error { | ||
if len(urlList) == 0 { | ||
logger.Warnf("The url list is empty") | ||
return nil | ||
} | ||
urlStrList := make([]string, 0, len(urlList)) | ||
|
||
for _, e := range urlList { | ||
urlStrList = append(urlStrList, e.String()) | ||
} | ||
|
||
bytes, err := json.Marshal(urlStrList) | ||
|
||
if err != nil { | ||
return perrors.WithMessage(err, "Could not convert the array to json") | ||
} | ||
return n.storeMetadata(vo.ConfigParam{ | ||
DataId: subscriberMetadataIdentifier.GetIdentifierKey(), | ||
Group: subscriberMetadataIdentifier.Group, | ||
Content: string(bytes), | ||
}) | ||
} | ||
|
||
// GetSubscribedURLs will lookup the url | ||
// if not found, an empty list will be returned | ||
func (n *nacosMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier) []string { | ||
return n.getConfigAsArray(vo.ConfigParam{ | ||
DataId: subscriberMetadataIdentifier.GetIdentifierKey(), | ||
Group: subscriberMetadataIdentifier.Group, | ||
}) | ||
} | ||
|
||
// GetServiceDefinition will lookup the service definition | ||
func (n *nacosMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) string { | ||
return n.getConfig(vo.ConfigParam{ | ||
DataId: metadataIdentifier.GetIdentifierKey(), | ||
Group: metadataIdentifier.Group, | ||
}) | ||
} | ||
|
||
// storeMetadata will publish the metadata to Nacos | ||
// if failed or error is not nil, error will be returned | ||
func (n *nacosMetadataReport) storeMetadata(param vo.ConfigParam) error { | ||
res, err := n.client.PublishConfig(param) | ||
if err != nil { | ||
return perrors.WithMessage(err, "Could not publish the metadata") | ||
} | ||
if !res { | ||
return perrors.New("Publish the metadata failed.") | ||
} | ||
return nil | ||
} | ||
|
||
// deleteMetadata will delete the metadata | ||
func (n *nacosMetadataReport) deleteMetadata(param vo.ConfigParam) error { | ||
res, err := n.client.DeleteConfig(param) | ||
if err != nil { | ||
return perrors.WithMessage(err, "Could not delete the metadata") | ||
} | ||
if !res { | ||
return perrors.New("Deleting the metadata failed.") | ||
} | ||
return nil | ||
} | ||
|
||
// getConfigAsArray will read the config and then convert it as an one-element array | ||
// error or config not found, an empty list will be returned. | ||
func (n *nacosMetadataReport) getConfigAsArray(param vo.ConfigParam) []string { | ||
cfg := n.getConfig(param) | ||
res := make([]string, 0, 1) | ||
if len(cfg) == 0 { | ||
return res | ||
} | ||
decodeCfg, err := url.QueryUnescape(cfg) | ||
if err != nil { | ||
logger.Errorf("The config is invalid: %s", cfg) | ||
return res | ||
} | ||
res = append(res, decodeCfg) | ||
return res | ||
} | ||
|
||
// getConfig will read the config | ||
func (n *nacosMetadataReport) getConfig(param vo.ConfigParam) string { | ||
cfg, err := n.client.GetConfig(param) | ||
if err != nil { | ||
logger.Errorf("Finding the configuration failed: %v", param) | ||
} | ||
return cfg | ||
} | ||
|
||
type nacosMetadataReportFactory struct { | ||
} | ||
|
||
func (n *nacosMetadataReportFactory) CreateMetadataReport(url *common.URL) report.MetadataReport { | ||
client, err := nacos.NewNacosConfigClient(url) | ||
if err != nil { | ||
logger.Errorf("Could not create nacos metadata report. URL: %s", url.String()) | ||
return nil | ||
} | ||
return &nacosMetadataReport{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,115 @@ | ||
/* | ||
* 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 nacos | ||
|
||
import ( | ||
"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" | ||
) | ||
|
||
func TestNacosMetadataReport_CRUD(t *testing.T) { | ||
rpt := newTestReport() | ||
assert.NotNil(t, rpt) | ||
|
||
providerMi := newMetadataIdentifier("server") | ||
providerMeta := "provider" | ||
err := rpt.StoreProviderMetadata(providerMi, providerMeta) | ||
|
||
consumerMi := newMetadataIdentifier("client") | ||
consumerMeta := "consumer" | ||
err = rpt.StoreConsumerMetadata(consumerMi, consumerMeta) | ||
assert.Nil(t, err) | ||
|
||
serviceMi := newServiceMetadataIdentifier() | ||
serviceUrl, _ := common.NewURL("registry://console.nacos.io:80", common.WithParamsValue(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER))) | ||
|
||
err = rpt.SaveServiceMetadata(serviceMi, serviceUrl) | ||
assert.Nil(t, err) | ||
|
||
exportedUrls := rpt.GetExportedURLs(serviceMi) | ||
assert.Equal(t, 1, len(exportedUrls)) | ||
|
||
subMi := newSubscribeMetadataIdentifier() | ||
urlList := make([]common.URL, 0, 1) | ||
urlList = append(urlList, serviceUrl) | ||
err = rpt.SaveSubscribedData(subMi, urlList) | ||
assert.Nil(t, err) | ||
|
||
subscribeUrl := rpt.GetSubscribedURLs(subMi) | ||
assert.Equal(t, 1, len(subscribeUrl)) | ||
|
||
err = rpt.RemoveServiceMetadata(serviceMi) | ||
assert.Nil(t, err) | ||
|
||
} | ||
|
||
func newSubscribeMetadataIdentifier() *identifier.SubscriberMetadataIdentifier { | ||
return &identifier.SubscriberMetadataIdentifier{ | ||
Revision: "subscribe", | ||
MetadataIdentifier: *newMetadataIdentifier("provider"), | ||
} | ||
|
||
} | ||
|
||
func newServiceMetadataIdentifier() *identifier.ServiceMetadataIdentifier { | ||
return &identifier.ServiceMetadataIdentifier{ | ||
Protocol: "nacos", | ||
Revision: "a", | ||
BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ | ||
ServiceInterface: "com.test.MyTest", | ||
Version: "1.0.0", | ||
Group: "test_group", | ||
Side: "service", | ||
}, | ||
} | ||
} | ||
|
||
func newMetadataIdentifier(side string) *identifier.MetadataIdentifier { | ||
return &identifier.MetadataIdentifier{ | ||
Application: "test", | ||
BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ | ||
ServiceInterface: "com.test.MyTest", | ||
Version: "1.0.0", | ||
Group: "test_group", | ||
Side: side, | ||
}, | ||
} | ||
} | ||
|
||
func TestNacosMetadataReportFactory_CreateMetadataReport(t *testing.T) { | ||
res := newTestReport() | ||
assert.NotNil(t, res) | ||
} | ||
|
||
func newTestReport() report.MetadataReport { | ||
regurl, _ := common.NewURL("registry://console.nacos.io:80", common.WithParamsValue(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER))) | ||
res := extension.GetMetadataReportFactory("nacos").CreateMetadataReport(®url) | ||
return res | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is recommended to provide a method named Newxxx?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we provide NewXXX method, the factory should be singleton and we need factoryInstance + factoryInitOnce, but using closure can simply the codes