Skip to content
Snippets Groups Projects
Commit 5952c59f authored by xg.gao's avatar xg.gao
Browse files

consul metadata report

parent 9a464273
No related branches found
No related tags found
No related merge requests found
/*
* 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"
)
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 {
k := metadataIdentifier.GetIdentifierKey()
kv, _, err := m.client.KV().Get(k, nil)
if err != nil {
panic(err)
}
if kv == nil {
return []string{}
}
return []string{string(kv.Value)}
}
// SaveSubscribedData saves the urls.
func (m *consulMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier, urlListStr string) error {
kv := &consul.KVPair{Key: subscriberMetadataIdentifier.GetIdentifierKey(), Value: []byte(urlListStr)}
_, err := m.client.KV().Put(kv, nil)
return err
}
// GetSubscribedURLs gets the urls.
func (m *consulMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier) []string {
k := subscriberMetadataIdentifier.GetIdentifierKey()
kv, _, err := m.client.KV().Get(k, nil)
if err != nil {
panic(err)
}
if kv == nil {
return []string{}
}
return []string{string(kv.Value)}
}
// GetServiceDefinition gets the service definition.
func (m *consulMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) string {
k := metadataIdentifier.GetIdentifierKey()
kv, _, err := m.client.KV().Get(k, nil)
if err != nil {
panic(err)
}
if kv == nil {
return ""
}
return string(kv.Value)
}
type consulMetadataReportFactory struct {
}
func (m *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}
}
\ No newline at end of file
/*
* 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
......@@ -38,19 +38,19 @@ import (
)
func init() {
ftry := &nacosMetadataReportFactory{}
mf := &nacosMetadataReportFactory{}
extension.SetMetadataReportFactory("nacos", func() factory.MetadataReportFactory {
return ftry
return mf
})
}
// nacosMetadataReport is the implementation of MetadataReport based Nacos
// nacosMetadataReport is the implementation
// of MetadataReport based on 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
// StoreProviderMetadata stores the metadata.
func (n *nacosMetadataReport) StoreProviderMetadata(providerIdentifier *identifier.MetadataIdentifier, serviceDefinitions string) error {
return n.storeMetadata(vo.ConfigParam{
DataId: providerIdentifier.GetIdentifierKey(),
......@@ -59,8 +59,7 @@ func (n *nacosMetadataReport) StoreProviderMetadata(providerIdentifier *identifi
})
}
// StoreConsumerMetadata will store the metadata
// metadata including the basic info of the server, consumer info, and other user custom info
// StoreConsumerMetadata stores the metadata.
func (n *nacosMetadataReport) StoreConsumerMetadata(consumerMetadataIdentifier *identifier.MetadataIdentifier, serviceParameterString string) error {
return n.storeMetadata(vo.ConfigParam{
DataId: consumerMetadataIdentifier.GetIdentifierKey(),
......@@ -69,8 +68,7 @@ func (n *nacosMetadataReport) StoreConsumerMetadata(consumerMetadataIdentifier *
})
}
// SaveServiceMetadata will store the metadata
// metadata including the basic info of the server, service info, and other user custom info
// SaveServiceMetadata saves the metadata.
func (n *nacosMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier, url common.URL) error {
return n.storeMetadata(vo.ConfigParam{
DataId: metadataIdentifier.GetIdentifierKey(),
......@@ -79,7 +77,7 @@ func (n *nacosMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier
})
}
// RemoveServiceMetadata will remove the service metadata
// RemoveServiceMetadata removes the metadata.
func (n *nacosMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier) error {
return n.deleteMetadata(vo.ConfigParam{
DataId: metadataIdentifier.GetIdentifierKey(),
......@@ -87,8 +85,7 @@ func (n *nacosMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifi
})
}
// GetExportedURLs will look up the exported urls.
// if not found, an empty list will be returned.
// GetExportedURLs gets the urls.
func (n *nacosMetadataReport) GetExportedURLs(metadataIdentifier *identifier.ServiceMetadataIdentifier) []string {
return n.getConfigAsArray(vo.ConfigParam{
DataId: metadataIdentifier.GetIdentifierKey(),
......@@ -96,7 +93,7 @@ func (n *nacosMetadataReport) GetExportedURLs(metadataIdentifier *identifier.Ser
})
}
// SaveSubscribedData will convert the urlList to json array and then store it
// SaveSubscribedData saves the urls.
func (n *nacosMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier, urlListStr string) error {
return n.storeMetadata(vo.ConfigParam{
DataId: subscriberMetadataIdentifier.GetIdentifierKey(),
......@@ -105,8 +102,7 @@ func (n *nacosMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *i
})
}
// GetSubscribedURLs will lookup the url
// if not found, an empty list will be returned
// GetSubscribedURLs gets the urls.
func (n *nacosMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier) []string {
return n.getConfigAsArray(vo.ConfigParam{
DataId: subscriberMetadataIdentifier.GetIdentifierKey(),
......@@ -114,7 +110,7 @@ func (n *nacosMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *id
})
}
// GetServiceDefinition will lookup the service definition
// GetServiceDefinition gets the service definition.
func (n *nacosMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) string {
return n.getConfig(vo.ConfigParam{
DataId: metadataIdentifier.GetIdentifierKey(),
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment