Skip to content

Commit

Permalink
Merge pull request #402 from Patrick0308/rest
Browse files Browse the repository at this point in the history
Fix problem of rest protocol PR #352
  • Loading branch information
flycash authored Mar 14, 2020
2 parents 46af97f + e9fc1f8 commit dafc2f4
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 139 deletions.
8 changes: 4 additions & 4 deletions common/extension/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
package extension

import (
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
"github.com/apache/dubbo-go/protocol/rest/client"
)

var (
restClients = make(map[string]func(restOptions *rest_interface.RestOptions) rest_interface.RestClient, 8)
restClients = make(map[string]func(restOptions *client.RestOptions) client.RestClient, 8)
)

func SetRestClient(name string, fun func(restOptions *rest_interface.RestOptions) rest_interface.RestClient) {
func SetRestClient(name string, fun func(restOptions *client.RestOptions) client.RestClient) {
restClients[name] = fun
}

func GetNewRestClient(name string, restOptions *rest_interface.RestOptions) rest_interface.RestClient {
func GetNewRestClient(name string, restOptions *client.RestOptions) client.RestClient {
if restClients[name] == nil {
panic("restClient for " + name + " is not existing, make sure you have import the package.")
}
Expand Down
8 changes: 4 additions & 4 deletions common/extension/rest_config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
package extension

import (
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
"github.com/apache/dubbo-go/config/rest/config_reader"
)

var (
restConfigReaders = make(map[string]func() rest_interface.RestConfigReader)
restConfigReaders = make(map[string]func() config_reader.RestConfigReader)
)

func SetRestConfigReader(name string, fun func() rest_interface.RestConfigReader) {
func SetRestConfigReader(name string, fun func() config_reader.RestConfigReader) {
restConfigReaders[name] = fun
}

func GetSingletonRestConfigReader(name string) rest_interface.RestConfigReader {
func GetSingletonRestConfigReader(name string) config_reader.RestConfigReader {
if name == "" {
name = "default"
}
Expand Down
8 changes: 4 additions & 4 deletions common/extension/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
package extension

import (
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
"github.com/apache/dubbo-go/protocol/rest/server"
)

var (
restServers = make(map[string]func() rest_interface.RestServer, 8)
restServers = make(map[string]func() server.RestServer, 8)
)

func SetRestServer(name string, fun func() rest_interface.RestServer) {
func SetRestServer(name string, fun func() server.RestServer) {
restServers[name] = fun
}

func GetNewRestServer(name string) rest_interface.RestServer {
func GetNewRestServer(name string) server.RestServer {
if restServers[name] == nil {
panic("restServer for " + name + " is not existing, make sure you have import the package.")
}
Expand Down
11 changes: 9 additions & 2 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func checkApplicationName(config *ApplicationConfig) {

// Load Dubbo Init
func Load() {

// init router
if confRouterFile != "" {
if errPro := RouterInit(confRouterFile); errPro != nil {
Expand All @@ -89,7 +90,10 @@ func Load() {
if consumerConfig == nil {
logger.Warnf("consumerConfig is nil!")
} else {

// init rest consumer config
if err := ConsumerRestConfigInit(consumerConfig.RestConfigType); err != nil {
log.Printf("[initConsumerRestConfig] %#v", err)
}
metricConfig = consumerConfig.MetricConfig
applicationConfig = consumerConfig.ApplicationConfig

Expand Down Expand Up @@ -150,7 +154,10 @@ func Load() {
if providerConfig == nil {
logger.Warnf("providerConfig is nil!")
} else {

// init rest provider config
if err := ProviderRestConfigInit(providerConfig.RestConfigType); err != nil {
log.Printf("[initProviderRestConfig] %#v", err)
}
// so, you should know that the consumer's config will be override
metricConfig = providerConfig.MetricConfig
applicationConfig = providerConfig.ApplicationConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* limitations under the License.
*/

package rest_config_reader
package reader_impl

import (
"github.com/apache/dubbo-go/config/rest"
"github.com/apache/dubbo-go/config/rest/config_reader"
"os"
)

Expand All @@ -28,9 +30,7 @@ import (
import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/common/yaml"
"github.com/apache/dubbo-go/protocol/rest/rest_interface"
)

var (
Expand All @@ -48,37 +48,34 @@ func NewDefaultConfigReader() *DefaultConfigReader {
return &DefaultConfigReader{}
}

func (dcr *DefaultConfigReader) ReadConsumerConfig() *rest_interface.RestConsumerConfig {
func (dcr *DefaultConfigReader) ReadConsumerConfig() (*rest.RestConsumerConfig, error) {
confConFile := os.Getenv(constant.CONF_CONSUMER_FILE_PATH)
if len(confConFile) == 0 {
logger.Warnf("[Rest Config] rest consumer configure(consumer) file name is nil")
return nil
return nil, nil
}
restConsumerConfig := &rest_interface.RestConsumerConfig{}
restConsumerConfig := &rest.RestConsumerConfig{}
err := yaml.UnmarshalYMLConfig(confConFile, restConsumerConfig)
if err != nil {
logger.Errorf("[Rest Config] unmarshal Consumer RestYmlConfig error %v", perrors.WithStack(err))
return nil
return nil, perrors.Errorf("[Rest Config] unmarshal Consumer RestYmlConfig error %v", perrors.WithStack(err))
}
return restConsumerConfig
return restConsumerConfig, nil
}

func (dcr *DefaultConfigReader) ReadProviderConfig() *rest_interface.RestProviderConfig {
func (dcr *DefaultConfigReader) ReadProviderConfig() (*rest.RestProviderConfig, error) {
confProFile := os.Getenv(constant.CONF_PROVIDER_FILE_PATH)
if len(confProFile) == 0 {
logger.Warnf("[Rest Config] rest provider configure(provider) file name is nil")
return nil
return nil, nil
}
restProviderConfig := &rest_interface.RestProviderConfig{}
restProviderConfig := &rest.RestProviderConfig{}
err := yaml.UnmarshalYMLConfig(confProFile, restProviderConfig)
if err != nil {
logger.Errorf("[Rest Config] unmarshal Provider RestYmlConfig error %v", perrors.WithStack(err))
return nil
return nil, perrors.Errorf("[Rest Config] unmarshal Provider RestYmlConfig error %v", perrors.WithStack(err))

}
return restProviderConfig
return restProviderConfig, nil
}

func GetDefaultConfigReader() rest_interface.RestConfigReader {
func GetDefaultConfigReader() config_reader.RestConfigReader {
if defaultConfigReader == nil {
defaultConfigReader = NewDefaultConfigReader()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package rest_config_reader
package reader_impl

import (
"os"
Expand All @@ -34,14 +34,16 @@ func TestDefaultConfigReader_ReadConsumerConfig(t *testing.T) {
err := os.Setenv(constant.CONF_CONSUMER_FILE_PATH, "./testdata/consumer_config.yml")
assert.NoError(t, err)
reader := GetDefaultConfigReader()
config := reader.ReadConsumerConfig()
config, err := reader.ReadConsumerConfig()
assert.Nil(t, err)
assert.NotEmpty(t, config)
}

func TestDefaultConfigReader_ReadProviderConfig(t *testing.T) {
err := os.Setenv(constant.CONF_PROVIDER_FILE_PATH, "./testdata/provider_config.yml")
assert.NoError(t, err)
reader := GetDefaultConfigReader()
config := reader.ReadProviderConfig()
config, err := reader.ReadProviderConfig()
assert.Nil(t, err)
assert.NotEmpty(t, config)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* limitations under the License.
*/

package rest_interface
package config_reader

import "github.com/apache/dubbo-go/config/rest"

type RestConfigReader interface {
ReadConsumerConfig() *RestConsumerConfig
ReadProviderConfig() *RestProviderConfig
ReadConsumerConfig() (*rest.RestConsumerConfig, error)
ReadProviderConfig() (*rest.RestProviderConfig, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
* limitations under the License.
*/

package rest_interface
package rest

import "github.com/creasty/defaults"

// RestConsumerConfig ...
type RestConsumerConfig struct {
Client string `default:"resty" yaml:"rest_client" json:"rest_client,omitempty" property:"rest_client"`
Produces string `default:"application/json" yaml:"rest_produces" json:"rest_produces,omitempty" property:"rest_produces"`
Expand All @@ -38,6 +39,7 @@ func (c *RestConsumerConfig) UnmarshalYAML(unmarshal func(interface{}) error) er
return nil
}

// RestProviderConfig ...
type RestProviderConfig struct {
Server string `default:"go-restful" yaml:"rest_server" json:"rest_server,omitempty" property:"rest_server"`
Produces string `default:"*/*" yaml:"rest_produces" json:"rest_produces,omitempty" property:"rest_produces"`
Expand All @@ -57,6 +59,7 @@ func (c *RestProviderConfig) UnmarshalYAML(unmarshal func(interface{}) error) er
return nil
}

// RestServiceConfig ...
type RestServiceConfig struct {
InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"`
Url string `yaml:"url" json:"url,omitempty" property:"url"`
Expand All @@ -82,6 +85,7 @@ func (c *RestServiceConfig) UnmarshalYAML(unmarshal func(interface{}) error) err
return nil
}

// RestMethodConfig ...
type RestMethodConfig struct {
InterfaceName string
MethodName string `required:"true" yaml:"name" json:"name,omitempty" property:"name"`
Expand Down
Loading

0 comments on commit dafc2f4

Please sign in to comment.