DMwangnima commented on code in PR #2534:
URL: https://github.com/apache/dubbo-go/pull/2534#discussion_r1439131790


##########
metadata/metadata_service.go:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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 metadata
+
+import (
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       "dubbo.apache.org/dubbo-go/v3/metadata/info"
+       "dubbo.apache.org/dubbo-go/v3/registry"
+)
+
+// version will be used by Version func
+const (
+       version              = "1.0.0"
+       allServiceInterfaces = "*"
+)
+
+// MetadataService is used to define meta data related behaviors
+// usually the implementation should be singleton
+type MetadataService interface {
+       // GetExportedURLs will get the target exported url in metadata, the 
url should be unique
+       GetExportedURLs(serviceInterface string, group string, version string, 
protocol string) ([]*common.URL, error)
+       // GetExportedServiceURLs will return exported service urls
+       GetExportedServiceURLs() ([]*common.URL, error)
+       // GetSubscribedURLs will get the exported urls in metadata
+       GetSubscribedURLs() ([]*common.URL, error)
+       Version() (string, error)
+       // GetMetadataInfo will return metadata info
+       GetMetadataInfo(revision string) (*info.MetadataInfo, error)
+       // GetMetadataServiceURL will return the url of metadata service
+       GetMetadataServiceURL() (*common.URL, error)
+       // SetMetadataServiceURL exporter to set url of metadata service, will 
not be exported by exporter,cause no error return
+       SetMetadataServiceURL(*common.URL)
+}
+
+type ServiceExporter interface {
+       Export() error
+       UnExport()
+}
+
+// DefaultMetadataService is store and query the metadata info in memory when 
each service registry
+type DefaultMetadataService struct {
+       url *common.URL
+}
+
+func (mts *DefaultMetadataService) SetMetadataServiceURL(url *common.URL) {
+       mts.url = url
+}
+
+// GetExportedURLs get all exported urls
+func (mts *DefaultMetadataService) GetExportedURLs(serviceInterface string, 
group string, version string, protocol string) ([]*common.URL, error) {
+       if allServiceInterfaces == serviceInterface {
+               return mts.GetExportedServiceURLs()
+       }
+       all, err := mts.GetExportedServiceURLs()
+       if err != nil {
+               return nil, err
+       }
+       urls := make([]*common.URL, 0)
+       for _, url := range all {
+               if url.GetParam(constant.InterfaceKey, "") == serviceInterface 
&&
+                       url.GetParam(constant.GroupKey, "") == group &&
+                       url.GetParam(constant.ProtocolKey, "") == protocol &&
+                       url.GetParam(constant.VersionKey, "") == version {
+                       urls = append(urls, url)
+               }
+       }
+       return urls, nil
+}
+
+// GetMetadataInfo can get metadata in memory
+func (mts *DefaultMetadataService) GetMetadataInfo(revision string) 
(*info.MetadataInfo, error) {
+       if revision == "" {
+               return nil, nil
+       }
+       for _, metadataInfo := range mts.getAllMetadata() {
+               if metadataInfo.Revision == revision {
+                       return metadataInfo, nil
+               }
+       }
+       logger.Warnf("metadata not found for revision: %s", revision)
+       return nil, nil
+}
+
+func (mts *DefaultMetadataService) getAllMetadata() []*info.MetadataInfo {

Review Comment:
   Need to be considered well.



##########
metadata/client.go:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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 metadata
+
+import (
+       "context"
+       "encoding/json"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       "dubbo.apache.org/dubbo-go/v3/metadata/info"
+       reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance"
+       "dubbo.apache.org/dubbo-go/v3/registry"
+)
+
+const metadataProxyDefaultTimeout = 5000
+
+// GetMetadataFromMetadataReport test depends on dubbo protocol, if dubbo not 
dependent on config package, can move to metadata dir
+func GetMetadataFromMetadataReport(revision string, instance 
registry.ServiceInstance) (*info.MetadataInfo, error) {
+       report := reportInstance.GetMetadataReport()
+       return report.GetAppMetadata(instance.GetServiceName(), revision)
+}
+
+func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) 
(*info.MetadataInfo, error) {
+       service, destroy := createRpcClient(instance)
+       ctx, cancel := context.WithTimeout(context.Background(), 
time.Duration(metadataProxyDefaultTimeout))
+       defer cancel()
+       defer destroy()
+       return service.GetMetadataInfo(ctx, revision)
+}
+
+type remoteMetadataService struct {
+       //GetExportedURLs       func(context context.Context, serviceInterface 
string, group string, version string, protocol string) ([]*common.URL, error) 
`dubbo:"getExportedURLs"`

Review Comment:
   The commented functions means they would be introduced in the future?



##########
metadata/info/metadata_info_test.go:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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 info
+
+import (
+       "testing"
+)
+
+import (
+       hessian "github.com/apache/dubbo-go-hessian2"
+
+       "github.com/stretchr/testify/assert"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+)
+
+func TestMetadataInfoAddService(t *testing.T) {
+       metadataInfo := &MetadataInfo{
+               Services:              make(map[string]*ServiceInfo),
+               exportedServiceURLs:   make(map[string][]*common.URL),
+               subscribedServiceURLs: make(map[string][]*common.URL),
+       }
+
+       url, _ := 
common.NewURL("dubbo://127.0.0.1:20000?application=foo&category=providers&check=false&dubbo=dubbo-go+v1.5.0&interface=com.foo.Bar&methods=GetPetByID%2CGetPetTypes&organization=Apache&owner=foo&revision=1.0.0&side=provider&version=1.0.0")
+       metadataInfo.AddService(url)
+       assert.True(t, len(metadataInfo.Services) > 0)
+       assert.True(t, len(metadataInfo.exportedServiceURLs) > 0)
+
+       metadataInfo.RemoveService(url)
+       assert.True(t, len(metadataInfo.Services) == 0)
+       assert.True(t, len(metadataInfo.exportedServiceURLs) == 0)
+}
+
+func TestHessian(t *testing.T) {
+       metadataInfo := &MetadataInfo{
+               App:                   "test",
+               Revision:              "1",
+               Services:              make(map[string]*ServiceInfo),
+               exportedServiceURLs:   make(map[string][]*common.URL),
+               subscribedServiceURLs: make(map[string][]*common.URL),
+       }
+       metadataInfo.Services["1"] = NewServiceInfo("dubbo.io", "default", 
"1.0.0", "dubbo", "", make(map[string]string))
+       e := hessian.NewEncoder()
+       err := e.Encode(metadataInfo)
+       if err != nil {
+               panic(err)
+       }
+       obj, err := hessian.NewDecoder(e.Buffer()).Decode()
+       if err != nil {
+               panic(err)
+       }
+       t.Log(obj)

Review Comment:
   Maybe using assert.Equal to test is a better choice?



##########
server/server.go:
##########
@@ -187,8 +187,11 @@ func (s *Server) Serve() error {
        if err := s.exportServices(); err != nil {
                return err
        }
-       metadata.ExportMetadataService()
-       registry_exposed.RegisterServiceInstance(s.cfg.Application.Name, 
s.cfg.Application.Tag, s.cfg.Application.MetadataType)
+       metadata.ExportMetadataService(s.cfg.Application.Name, 
s.cfg.Application.MetadataType)
+       err := exposed_tmp.RegisterServiceInstance()

Review Comment:
   ```if err != nil```
   Unified coding style is better.



##########
metadata/client.go:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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 metadata
+
+import (
+       "context"
+       "encoding/json"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       "dubbo.apache.org/dubbo-go/v3/metadata/info"
+       reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance"
+       "dubbo.apache.org/dubbo-go/v3/registry"
+)
+
+const metadataProxyDefaultTimeout = 5000
+
+// GetMetadataFromMetadataReport test depends on dubbo protocol, if dubbo not 
dependent on config package, can move to metadata dir
+func GetMetadataFromMetadataReport(revision string, instance 
registry.ServiceInstance) (*info.MetadataInfo, error) {
+       report := reportInstance.GetMetadataReport()
+       return report.GetAppMetadata(instance.GetServiceName(), revision)
+}
+
+func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) 
(*info.MetadataInfo, error) {
+       service, destroy := createRpcClient(instance)
+       ctx, cancel := context.WithTimeout(context.Background(), 
time.Duration(metadataProxyDefaultTimeout))
+       defer cancel()
+       defer destroy()
+       return service.GetMetadataInfo(ctx, revision)
+}
+
+type remoteMetadataService struct {
+       //GetExportedURLs       func(context context.Context, serviceInterface 
string, group string, version string, protocol string) ([]*common.URL, error) 
`dubbo:"getExportedURLs"`
+       GetMetadataInfo func(context context.Context, revision string) 
(*info.MetadataInfo, error) `dubbo:"getMetadataInfo"`
+       //GetMetadataServiceURL func(context context.Context) (*common.URL, 
error)
+       //GetSubscribedURLs     func(context context.Context) ([]*common.URL, 
error)
+       //Version               func(context context.Context) (string, error)
+}
+
+func createRpcClient(instance registry.ServiceInstance) 
(*remoteMetadataService, func()) {
+       params := 
getMetadataServiceUrlParams(instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName])
+       url := buildMetadataServiceURL(instance.GetServiceName(), 
instance.GetHost(), params)
+       return createRpcClientByUrl(url)
+}
+
+func createRpcClientByUrl(url *common.URL) (*remoteMetadataService, func()) {
+       rpcService := &remoteMetadataService{}
+       invoker := extension.GetProtocol(constant.Dubbo).Refer(url)
+       proxy := extension.GetProxyFactory("").GetProxy(invoker, url)

Review Comment:
   ```extension.GetProxyFactory("")``` works relying on that GetProxyFactory 
would treat empty string as "default". Maybe using "default" directly is better.



##########
metadata/client.go:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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 metadata
+
+import (
+       "context"
+       "encoding/json"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       "dubbo.apache.org/dubbo-go/v3/metadata/info"
+       reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance"
+       "dubbo.apache.org/dubbo-go/v3/registry"
+)
+
+const metadataProxyDefaultTimeout = 5000
+
+// GetMetadataFromMetadataReport test depends on dubbo protocol, if dubbo not 
dependent on config package, can move to metadata dir
+func GetMetadataFromMetadataReport(revision string, instance 
registry.ServiceInstance) (*info.MetadataInfo, error) {
+       report := reportInstance.GetMetadataReport()
+       return report.GetAppMetadata(instance.GetServiceName(), revision)
+}
+
+func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) 
(*info.MetadataInfo, error) {
+       service, destroy := createRpcClient(instance)
+       ctx, cancel := context.WithTimeout(context.Background(), 
time.Duration(metadataProxyDefaultTimeout))
+       defer cancel()
+       defer destroy()
+       return service.GetMetadataInfo(ctx, revision)
+}
+
+type remoteMetadataService struct {
+       //GetExportedURLs       func(context context.Context, serviceInterface 
string, group string, version string, protocol string) ([]*common.URL, error) 
`dubbo:"getExportedURLs"`
+       GetMetadataInfo func(context context.Context, revision string) 
(*info.MetadataInfo, error) `dubbo:"getMetadataInfo"`
+       //GetMetadataServiceURL func(context context.Context) (*common.URL, 
error)
+       //GetSubscribedURLs     func(context context.Context) ([]*common.URL, 
error)
+       //Version               func(context context.Context) (string, error)
+}
+
+func createRpcClient(instance registry.ServiceInstance) 
(*remoteMetadataService, func()) {
+       params := 
getMetadataServiceUrlParams(instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName])
+       url := buildMetadataServiceURL(instance.GetServiceName(), 
instance.GetHost(), params)
+       return createRpcClientByUrl(url)
+}
+
+func createRpcClientByUrl(url *common.URL) (*remoteMetadataService, func()) {
+       rpcService := &remoteMetadataService{}
+       invoker := extension.GetProtocol(constant.Dubbo).Refer(url)

Review Comment:
   invoker may be nil (Please check protocol/dubbo/dubbo_protocol.go for 
details).



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to