Skip to content
Snippets Groups Projects
Commit dd2abb94 authored by vito.he's avatar vito.he
Browse files

Add:interface definition of metadata

parent e35dffd7
No related branches found
No related tags found
No related merge requests found
Showing
with 467 additions and 15 deletions
......@@ -164,6 +164,10 @@ const (
// metadata report
const (
METACONFIG_REMOTE = "remote"
METACONFIG_LOCAL = "local"
METACONFIG_REMOTE = "remote"
METACONFIG_LOCAL = "local"
KEY_SEPARATOR = ":"
DEFAULT_PATH_TAG = "metadata"
PATH_SEPARATOR = "/"
KEY_REVISON_PREFIX = "revision"
)
/*
* 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 extension
import (
"github.com/apache/dubbo-go/metadata"
)
var (
metaDataReportFactories = make(map[string]func() metadata.MetadataReportFactory)
)
// SetMetadataReportFactory ...
func SetMetadataReportFactory(name string, v func() metadata.MetadataReportFactory) {
metaDataReportFactories[name] = v
}
// GetConfigCenterFactory ...
func GetMetadataReportFactory(name string) metadata.MetadataReportFactory {
if metaDataReportFactories[name] == nil {
panic("metadata report for " + name + " is not existing, make sure you have import the package.")
}
return metaDataReportFactories[name]()
}
......@@ -129,6 +129,11 @@ func ConsumerInit(confConFile string) error {
return perrors.WithMessagef(err, "time.ParseDuration(Connect_Timeout{%#v})", consumerConfig.Connect_Timeout)
}
}
//start the metadata report if config set
if err := startMetadataReport(); err != nil {
return perrors.WithMessagef(err, "Consumer start metadata report error ,error is {%#v}", err)
}
logger.Debugf("consumer config{%#v}\n", consumerConfig)
return nil
}
......@@ -155,14 +160,3 @@ func configCenterRefreshConsumer() error {
}
return err
}
// StartMetadataReport: The entry of metadata report start
func startMetadataReport() error {
metadataConfig := consumerConfig.ApplicationConfig.MetadataType
if metadataConfig == constant.METACONFIG_REMOTE && consumerConfig.MetadataReportConfig == nil {
return perrors.New("No MetadataConfig found, you must specify the remote Metadata Center address when 'metadata=remote' is enabled.")
} else if metadataConfig == constant.METACONFIG_REMOTE && len(consumerConfig.MetadataReportConfig.Address) == 0 {
return perrors.New("MetadataConfig address can not be empty.")
}
return nil
}
......@@ -18,6 +18,7 @@
package config
import (
"github.com/apache/dubbo-go/metadata"
"net/url"
)
import (
......@@ -32,6 +33,7 @@ import (
// MethodConfig ...
type MetadataReportConfig struct {
Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty"`
Address string `yaml:"address" json:"address,omitempty" property:"address"`
Username string `yaml:"username" json:"username,omitempty" property:"username"`
Password string `yaml:"password" json:"password,omitempty" property:"password"`
......@@ -70,6 +72,7 @@ func (c *MetadataReportConfig) ToUrl() (*common.URL, error) {
common.WithUsername(c.Username),
common.WithPassword(c.Password),
common.WithLocation(c.Address),
common.WithProtocol(c.Protocol),
)
if err != nil {
return nil, perrors.New("Invalid MetadataReportConfig.")
......@@ -77,3 +80,23 @@ func (c *MetadataReportConfig) ToUrl() (*common.URL, error) {
url.SetParam("metadata", url.Protocol)
return &url, nil
}
// StartMetadataReport: The entry of metadata report start
func startMetadataReport() error {
metadataConfig := consumerConfig.ApplicationConfig.MetadataType
if consumerConfig.MetadataReportConfig == nil {
return nil
} else {
if metadataConfig == constant.METACONFIG_REMOTE {
return perrors.New("No MetadataConfig found, you must specify the remote Metadata Center address when 'metadata=remote' is enabled.")
} else if metadataConfig == constant.METACONFIG_REMOTE && len(consumerConfig.MetadataReportConfig.Address) == 0 {
return perrors.New("MetadataConfig address can not be empty.")
}
if url, err := consumerConfig.MetadataReportConfig.ToUrl(); err == nil {
metadata.Init(url)
} else {
return perrors.New("MetadataConfig is invalid!")
}
}
return nil
}
......@@ -8,7 +8,8 @@ import (
func TestMetadataReportConfig_ToUrl(t *testing.T) {
metadataReportConfig := MetadataReportConfig{
Address: "mock://127.0.0.1",
Protocol: "mock",
Address: "127.0.0.1:2181",
Username: "test",
Password: "test",
TimeoutStr: "3s",
......@@ -19,6 +20,9 @@ func TestMetadataReportConfig_ToUrl(t *testing.T) {
url, error := metadataReportConfig.ToUrl()
assert.NoError(t, error)
assert.Equal(t, "mock", url.Protocol)
assert.Equal(t, "127.0.0.1:2181", url.Location)
assert.Equal(t, "127.0.0.1", url.Ip)
assert.Equal(t, "2181", url.Port)
assert.Equal(t, "test", url.Username)
assert.Equal(t, "test", url.Password)
assert.Equal(t, "v", url.GetParam("k", ""))
......
......@@ -103,7 +103,10 @@ func ProviderInit(confProFile string) error {
n.InterfaceId = k
}
}
//start the metadata report if config set
if err := startMetadataReport(); err != nil {
return perrors.WithMessagef(err, "Provider start metadata report error ,error is {%#v}", err)
}
logger.Debugf("provider config{%#v}\n", providerConfig)
return nil
......
/*
* 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 definition
type ServiceDefinition struct {
CanonicalName string
CodeSource string
Methods []MethodDefinition
Types []TypeDefinition
}
type MethodDefinition struct {
Name string
ParameterTypes []string
ReturnType string
Parameters []TypeDefinition
}
type TypeDefinition struct {
Id string
Type string
Items []TypeDefinition
Enums []string
Properties map[string]TypeDefinition
TypeBuilderName string
}
/*
* 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/apache/dubbo-go/common"
type MetadataExporter interface {
Export() MetadataExporter
Unexport() MetadataExporter
GetExportedURLs() []*common.URL
IsExported() bool
}
/*
* 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 identifier
import (
"encoding/base64"
)
import (
"github.com/apache/dubbo-go/common/constant"
)
type BaseMetadataIdentifier interface {
getFilePathKey(params ...string) string
getIdentifierKey(params ...string) string
}
type BaseServiceMetadataIdentifier struct {
serviceInterface string
version string
group string
side string
}
// joinParams...
func joinParams(joinChar string, params []string) string {
var joinedStr string
for _, param := range params {
joinedStr += joinChar
joinedStr += param
}
return joinedStr
}
// getIdentifierKey...
func (mdi *BaseServiceMetadataIdentifier) getIdentifierKey(params ...string) string {
return mdi.serviceInterface +
constant.KEY_SEPARATOR + mdi.version +
constant.KEY_SEPARATOR + mdi.group +
constant.KEY_SEPARATOR + mdi.side +
joinParams(constant.KEY_SEPARATOR, params)
}
// getIdentifierKey...
func (mdi *BaseServiceMetadataIdentifier) getFilePathKey(params ...string) string {
path := serviceToPath(mdi.serviceInterface)
return constant.DEFAULT_PATH_TAG +
withPathSeparator(path) +
withPathSeparator(mdi.version) +
withPathSeparator(mdi.group) +
withPathSeparator(mdi.side) +
joinParams(constant.PATH_SEPARATOR, params)
}
// serviceToPath...
func serviceToPath(serviceInterface string) string {
if serviceInterface == constant.ANY_VALUE {
return ""
} else {
decoded, err := base64.URLEncoding.DecodeString(serviceInterface)
if err != nil {
return ""
}
return string(decoded)
}
}
//withPathSeparator...
func withPathSeparator(path string) string {
if len(path) != 0 {
path = constant.PATH_SEPARATOR + path
}
return path
}
/*
* 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 identifier
type MetadataIdentifier struct {
application string
BaseMetadataIdentifier
}
// getIdentifierKey...
func (mdi *MetadataIdentifier) getIdentifierKey(params ...string) string {
return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.application)
}
// getIdentifierKey...
func (mdi *MetadataIdentifier) getFilePathKey(params ...string) string {
return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.application)
}
/*
* 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 identifier
import "github.com/apache/dubbo-go/common/constant"
type ServiceMetadataIdentifier struct {
revision string
protocol string
BaseMetadataIdentifier
}
// getIdentifierKey...
func (mdi *ServiceMetadataIdentifier) getIdentifierKey(params ...string) string {
return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.protocol + constant.KEY_REVISON_PREFIX + mdi.revision)
}
// getIdentifierKey...
func (mdi *ServiceMetadataIdentifier) getFilePathKey(params ...string) string {
return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.protocol + constant.KEY_REVISON_PREFIX + mdi.revision)
}
package identifier
type SubscriberMetadataIdentifier struct {
revision string
BaseMetadataIdentifier
}
// getIdentifierKey...
func (mdi *SubscriberMetadataIdentifier) getIdentifierKey(params ...string) string {
return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.revision)
}
// getIdentifierKey...
func (mdi *SubscriberMetadataIdentifier) getFilePathKey(params ...string) string {
return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.revision)
}
/*
* 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/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"sync"
)
var (
instance MetadataReport
once sync.Once
)
// GetEnvInstance ...
func Init(url *common.URL) {
once.Do(func() {
instance = extension.GetMetadataReportFactory(url.Protocol).createMetadataReport(url)
})
}
func GetMetadataReportInstance() MetadataReport {
return instance
}
/*
* 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/apache/dubbo-go/common"
"github.com/apache/dubbo-go/metadata/definition"
"github.com/apache/dubbo-go/metadata/identifier"
)
type MetadataReport interface {
StoreProviderMetadata(*identifier.MetadataIdentifier, *definition.ServiceDefinition)
StoreConsumeretadata(*identifier.MetadataIdentifier, map[string]string)
SaveServiceMetadata(*identifier.ServiceMetadataIdentifier, *common.URL)
RemoveServiceMetadata(*identifier.ServiceMetadataIdentifier)
GetExportedURLs(*identifier.ServiceMetadataIdentifier) []string
SaveSubscribedData(*identifier.SubscriberMetadataIdentifier, []*common.URL)
GetSubscribedURLs(*identifier.SubscriberMetadataIdentifier) []string
GetServiceDefinition(*identifier.MetadataIdentifier)
}
/*
* 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/apache/dubbo-go/common"
var (
MetadataReportInstance MetadataReport
)
type MetadataReportFactory interface {
createMetadataReport(*common.URL) MetadataReport
}
/*
* 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/apache/dubbo-go/common"
gxset "github.com/dubbogo/gost/container/set"
)
type MetadataService interface {
ServiceName() string
ExportURL(url *common.URL) bool
UnexportURL(url *common.URL) bool
RefreshMetadata(exportedRevision string, subscribedRevision string) bool
SubscribeURL(url *common.URL) bool
UnsubscribeURL(url *common.URL) bool
PublishServiceDefinition(url *common.URL)
GetExportedURLs(serviceInterface string, group string, version string, protocol string) gxset.HashSet
GetServiceDefinition(interfaceName string, version string, group string) string
GetServiceDefinitionByServiceKey(serviceKey string) string
}
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