You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2020/06/29 02:25:34 UTC
[GitHub] [dubbo-go] hxmhlt commented on a change in pull request #633: Ftr: zk/consul Metadata
hxmhlt commented on a change in pull request #633:
URL: https://github.com/apache/dubbo-go/pull/633#discussion_r446736827
##########
File path: metadata/report/consul/report.go
##########
@@ -0,0 +1,136 @@
+/*
+ * 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)}
Review comment:
go fmt not work ?
##########
File path: metadata/report/consul/report.go
##########
@@ -0,0 +1,136 @@
+/*
+ * 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 {
+ k := metadataIdentifier.GetIdentifierKey()
+ kv, _, err := m.client.KV().Get(k, nil)
+ if err != nil {
+ panic(err)
+ }
+
+ if kv == nil {
+ return emptyStrSlice
+ }
+ return []string{string(kv.Value)}
+}
+
+// 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 {
+ k := subscriberMetadataIdentifier.GetIdentifierKey()
+ kv, _, err := m.client.KV().Get(k, nil)
+ if err != nil {
+ panic(err)
+ }
+
+ if kv == nil {
+ return emptyStrSlice
+ }
+ 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)
Review comment:
don't panic
##########
File path: remoting/consul/agent.go
##########
@@ -0,0 +1,70 @@
+/*
Review comment:
If used for unit test, it's better to move to xxx_test file.
##########
File path: metadata/report/consul/report.go
##########
@@ -0,0 +1,136 @@
+/*
+ * 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
+ })
Review comment:
combine two lines.
##########
File path: metadata/report/delegate/delegate_report.go
##########
@@ -241,11 +242,20 @@ func (mr *MetadataReport) GetExportedURLs(identifier *identifier.ServiceMetadata
// SaveSubscribedData will delegate to call remote metadata's sdk to save subscribed data
func (mr *MetadataReport) SaveSubscribedData(identifier *identifier.SubscriberMetadataIdentifier, urls []common.URL) error {
+ urlStrList := make([]string, 0, len(urls))
+ for _, url := range urls {
+ urlStrList = append(urlStrList, url.String())
+ }
+ bytes, err := json.Marshal(urlStrList)
+ if err != nil {
+ return perrors.WithMessage(err, "Could not convert the array to json")
+ }
+
report := instance.GetMetadataReportInstance()
Review comment:
May create influence to nacos's implement.
##########
File path: metadata/report/zookeeper/report.go
##########
@@ -0,0 +1,143 @@
+/*
+ * 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 zookeeper
+
+import (
+ "strings"
+ "time"
+)
+
+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/metadata/report/factory"
+ "github.com/apache/dubbo-go/remoting/zookeeper"
+)
+
+var (
+ emptyStrSlice = make([]string, 0)
+)
+
+func init() {
+ mf := &zookeeperMetadataReportFactory{}
+ extension.SetMetadataReportFactory("zookeeper", func() factory.MetadataReportFactory {
+ return mf
+ })
Review comment:
combine
##########
File path: metadata/report/delegate/delegate_report.go
##########
@@ -241,11 +242,20 @@ func (mr *MetadataReport) GetExportedURLs(identifier *identifier.ServiceMetadata
// SaveSubscribedData will delegate to call remote metadata's sdk to save subscribed data
func (mr *MetadataReport) SaveSubscribedData(identifier *identifier.SubscriberMetadataIdentifier, urls []common.URL) error {
+ urlStrList := make([]string, 0, len(urls))
+ for _, url := range urls {
+ urlStrList = append(urlStrList, url.String())
+ }
+ bytes, err := json.Marshal(urlStrList)
+ if err != nil {
+ return perrors.WithMessage(err, "Could not convert the array to json")
+ }
+
report := instance.GetMetadataReportInstance()
Review comment:
This common logic may be confirmed by @flycash
##########
File path: metadata/report/nacos/report.go
##########
@@ -39,19 +38,19 @@ import (
)
func init() {
- ftry := &nacosMetadataReportFactory{}
+ mf := &nacosMetadataReportFactory{}
extension.SetMetadataReportFactory("nacos", func() factory.MetadataReportFactory {
- return ftry
+ return mf
})
Review comment:
Combine
##########
File path: remoting/consul/agent.go
##########
@@ -0,0 +1,70 @@
+/*
Review comment:
What the consultAgent used for ?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org