From 29144da97faeadae85b0dd8ac6cdbdb019115ea0 Mon Sep 17 00:00:00 2001 From: lizhipeng <lizhipeng1@huya.com> Date: Fri, 24 Apr 2020 23:30:38 +0800 Subject: [PATCH] fix synthesizer bug --- registry/service/service_discovery_registry.go | 2 +- .../rest/rest_subscribed_urls_synthesizer.go | 8 +++++--- .../rest_subscribed_urls_synthesizer_test.go | 17 ++++++++++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/registry/service/service_discovery_registry.go b/registry/service/service_discovery_registry.go index c946b9890..b3c9e6a63 100644 --- a/registry/service/service_discovery_registry.go +++ b/registry/service/service_discovery_registry.go @@ -276,7 +276,7 @@ func (s *serviceDiscoveryRegistry) findMappedServices(url common.URL) *gxset.Has } func (s *serviceDiscoveryRegistry) getExportedUrls(subscribedURL common.URL, serviceInstances []registry.ServiceInstance) []common.URL { - filterInstances := make([]registry.ServiceInstance, len(serviceInstances), len(serviceInstances)) + var filterInstances []registry.ServiceInstance for _, s := range serviceInstances { if !s.IsEnable() || !s.IsHealthy() { continue diff --git a/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer.go b/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer.go index 7d3de7a3c..a7e7a5bc3 100644 --- a/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer.go +++ b/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer.go @@ -22,6 +22,7 @@ import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/registry" "github.com/apache/dubbo-go/registry/service/synthesizer" + "net/url" "strings" ) @@ -41,15 +42,16 @@ func (r RestSubscribedURLsSynthesizer) Support(subscribedURL *common.URL) bool { func (r RestSubscribedURLsSynthesizer) Synthesize(subscribedURL *common.URL, serviceInstances []registry.ServiceInstance) []common.URL { urls := make([]common.URL, len(serviceInstances), len(serviceInstances)) - for _, s := range serviceInstances { + for i, s := range serviceInstances { splitHost := strings.Split(s.GetHost(), ":") - url := common.NewURLWithOptions(common.WithProtocol(subscribedURL.Protocol), common.WithIp(splitHost[0]), + u := common.NewURLWithOptions(common.WithProtocol(subscribedURL.Protocol), common.WithIp(splitHost[0]), common.WithPort(splitHost[1]), common.WithPath(subscribedURL.GetParam(constant.INTERFACE_KEY, subscribedURL.Path)), + common.WithParams(url.Values{}), common.WithParamsValue(constant.SIDE_KEY, constant.PROVIDER_PROTOCOL), common.WithParamsValue(constant.APPLICATION_KEY, s.GetServiceName()), common.WithParamsValue(constant.REGISTRY_KEY, "true"), ) - urls = append(urls, *url) + urls[i] = *u } return urls } diff --git a/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer_test.go b/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer_test.go index 1e8c1172b..8ad1b5022 100644 --- a/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer_test.go +++ b/registry/service/synthesizer/rest/rest_subscribed_urls_synthesizer_test.go @@ -22,6 +22,7 @@ import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/registry" "github.com/stretchr/testify/assert" + "net/url" "testing" ) @@ -50,13 +51,19 @@ func TestRestSubscribedURLsSynthesizer_Synthesize(t *testing.T) { } var expectUrls []common.URL - u1, _ := common.NewURL("rest://127.0.0.1:80/org.apache.dubbo-go.mockService", common.WithParamsValue(constant.SIDE_KEY, - constant.PROVIDER_PROTOCOL), common.WithParamsValue(constant.APPLICATION_KEY, "test1"), + u1 := common.NewURLWithOptions(common.WithProtocol("rest"), common.WithIp("127.0.0.1"), + common.WithPort("80"), common.WithPath("org.apache.dubbo-go.mockService"), + common.WithParams(url.Values{}), + common.WithParamsValue(constant.SIDE_KEY, constant.PROVIDER_PROTOCOL), + common.WithParamsValue(constant.APPLICATION_KEY, "test1"), common.WithParamsValue(constant.REGISTRY_KEY, "true")) - u2, _ := common.NewURL("rest://127.0.0.2:8081/org.apache.dubbo-go.mockService", common.WithParamsValue(constant.SIDE_KEY, - constant.PROVIDER_PROTOCOL), common.WithParamsValue(constant.APPLICATION_KEY, "test2"), + u2 := common.NewURLWithOptions(common.WithProtocol("rest"), common.WithIp("127.0.0.2"), + common.WithPort("8081"), common.WithPath("org.apache.dubbo-go.mockService"), + common.WithParams(url.Values{}), + common.WithParamsValue(constant.SIDE_KEY, constant.PROVIDER_PROTOCOL), + common.WithParamsValue(constant.APPLICATION_KEY, "test2"), common.WithParamsValue(constant.REGISTRY_KEY, "true")) - expectUrls = append(expectUrls, u1, u2) + expectUrls = append(expectUrls, *u1, *u2) result := syn.Synthesize(&subUrl, instances) assert.Equal(t, expectUrls, result) } -- GitLab