Skip to content
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

Fix: Register service instance after provider config load #694

Merged
merged 14 commits into from
Aug 20, 2020
7 changes: 5 additions & 2 deletions common/extension/metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"fmt"
)

import (
perrors "github.com/pkg/errors"
)

import (
"github.com/apache/dubbo-go/metadata/service"
)
Expand All @@ -36,12 +40,11 @@ func SetMetadataService(msType string, creator func() (service.MetadataService,
}

// GetMetadataService will create a MetadataService instance
// it will panic if msType not found
func GetMetadataService(msType string) (service.MetadataService, error) {
if creator, ok := metadataServiceInsMap[msType]; ok {
return creator()
}
panic(fmt.Sprintf("could not find the metadata service creator for metadataType: %s, please check whether you have imported relative packages, \n"+
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: %s, please check whether you have imported relative packages, \n"+
"local - github.com/apache/dubbo-go/metadata/service/inmemory, \n"+
"remote - github.com/apache/dubbo-go/metadata/service/remote", msType))
}
95 changes: 95 additions & 0 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"sync"
"time"
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)

Expand All @@ -35,6 +38,7 @@ import (
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
_ "github.com/apache/dubbo-go/common/observer/dispatcher"
"github.com/apache/dubbo-go/registry"
)

var (
Expand Down Expand Up @@ -206,6 +210,97 @@ func loadProviderConfig() {
panic(fmt.Sprintf("service %s export failed! err: %#v", key, err))
}
}
registerServiceInstance()
}

// registerServiceInstance register service instance
func registerServiceInstance() {
url := selectMetadataServiceExportedURL()
if url == nil {
return
}
instance, err := createInstance(*url)
if err != nil {
panic(err)
}
p := extension.GetProtocol(constant.REGISTRY_KEY)
var rp registry.RegistryFactory
var ok bool
if rp, ok = p.(registry.RegistryFactory); !ok {
panic("dubbo registry protocol{" + reflect.TypeOf(p).String() + "} is invalid")
}
rs := rp.GetRegistries()
for _, r := range rs {
var sdr registry.ServiceDiscoveryHolder
if sdr, ok = r.(registry.ServiceDiscoveryHolder); !ok {
continue
}
err := sdr.GetServiceDiscovery().Register(instance)
if err != nil {
panic(err)
}
}
}

// createInstance
Patrick0308 marked this conversation as resolved.
Show resolved Hide resolved
func createInstance(url common.URL) (registry.ServiceInstance, error) {
appConfig := GetApplicationConfig()
port, err := strconv.ParseInt(url.Port, 10, 32)
if err != nil {
return nil, perrors.WithMessage(err, "invalid port: "+url.Port)
}

host := url.Ip
if len(host) == 0 {
host, err = gxnet.GetLocalIP()
if err != nil {
return nil, perrors.WithMessage(err, "could not get the local Ip")
}
}

// usually we will add more metadata
metadata := make(map[string]string, 8)
metadata[constant.METADATA_STORAGE_TYPE_PROPERTY_NAME] = appConfig.MetadataType

return &registry.DefaultServiceInstance{
ServiceName: appConfig.Name,
Host: host,
Port: int(port),
Id: host + constant.KEY_SEPARATOR + url.Port,
Enable: true,
Healthy: true,
Metadata: metadata,
}, nil
}

// selectMetadataServiceExportedURL get already be exported url
func selectMetadataServiceExportedURL() *common.URL {
var selectedUrl common.URL
metaDataService, err := extension.GetMetadataService(GetApplicationConfig().MetadataType)
if err != nil {
logger.Warn(err)
return nil
}
list, err := metaDataService.GetExportedURLs(constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not define a new variable with var selectedUrl common.URL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selectedUrl is above

if err != nil {
panic(err)
}
if len(list) == 0 {
return nil
}
for _, urlStr := range list {
url, err := common.NewURL(urlStr.(string))
if err != nil {
logger.Errorf("url format error {%v}", url)
continue
}
selectedUrl = url
// rest first
if url.Protocol == "rest" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if break this loop when find rest protocol, is it a problem or bug in multi protocol ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dubbo in java use rest url first.

break
}
}
return &selectedUrl
}

func initRouter() {
Expand Down
Loading