From b1ce26eb2b17a49e716015f15012eced09219f5e Mon Sep 17 00:00:00 2001 From: fangyincheng Date: Tue, 2 Jul 2019 19:59:45 +0800 Subject: [PATCH 1/5] Add:support multi-implementation of service --- common/constant/key.go | 2 +- common/proxy/proxy_test.go | 10 +- common/rpc_service.go | 15 +- common/rpc_service_test.go | 17 +- config/base_config_test.go | 23 ++- config/config_loader.go | 22 +- config/config_loader_test.go | 10 +- config/mock_rpcservice.go | 6 +- config/reference_config.go | 3 +- config/reference_config_test.go | 3 +- config/service.go | 4 +- config/service_config.go | 8 +- config/service_config_test.go | 2 +- config/testdata/consumer_config.yml | 4 +- .../consumer_config_with_configcenter.yml | 2 +- config/testdata/provider_config.yml | 2 + examples/dubbo/go-client/app/client.go | 191 +++++++++++++++--- examples/dubbo/go-client/app/user.go | 42 +++- .../dubbo/go-client/profiles/dev/client.yml | 24 ++- .../go-client/profiles/release/client.yml | 21 +- .../dubbo/go-client/profiles/test/client.yml | 25 ++- examples/dubbo/go-server/app/server.go | 2 +- examples/dubbo/go-server/app/user.go | 114 +---------- examples/dubbo/go-server/app/user_provider.go | 102 ++++++++++ .../dubbo/go-server/app/user_provider1.go | 88 ++++++++ .../dubbo/go-server/app/user_provider2.go | 97 +++++++++ .../dubbo/go-server/profiles/dev/server.yml | 38 +++- .../go-server/profiles/release/server.yml | 25 +++ .../dubbo/go-server/profiles/test/server.yml | 37 +++- .../main/java/com/ikurento/user/Consumer.java | 151 ++++++++++++-- .../META-INF/spring/dubbo.consumer.xml | 5 +- .../resources/META-INF/spring/service.xml | 2 + .../with-configcenter-go-client/app/user.go | 8 +- .../with-configcenter-go-server/app/user.go | 13 +- examples/jsonrpc/go-client/app/client.go | 170 +++++++++++++--- examples/jsonrpc/go-client/app/user.go | 40 +++- .../jsonrpc/go-client/profiles/dev/client.yml | 37 +++- .../go-client/profiles/release/client.yml | 21 +- .../go-client/profiles/test/client.yml | 37 +++- examples/jsonrpc/go-server/app/server.go | 2 +- examples/jsonrpc/go-server/app/user.go | 117 +---------- .../jsonrpc/go-server/app/user_provider.go | 98 +++++++++ .../jsonrpc/go-server/app/user_provider1.go | 83 ++++++++ .../jsonrpc/go-server/app/user_provider2.go | 93 +++++++++ .../jsonrpc/go-server/profiles/dev/server.yml | 69 +++++-- .../go-server/profiles/release/server.yml | 51 +++-- .../go-server/profiles/test/server.yml | 69 +++++-- .../main/java/com/ikurento/user/Consumer.java | 143 +++++++++++-- .../META-INF/spring/dubbo.consumer.xml | 4 + .../resources/META-INF/spring/service.xml | 2 + .../with-configcenter-go-client/app/user.go | 8 +- .../with-configcenter-go-server/app/user.go | 8 +- go.mod | 2 +- go.sum | 4 +- protocol/dubbo/client.go | 1 - protocol/dubbo/client_test.go | 22 +- protocol/dubbo/codec_test.go | 11 +- protocol/dubbo/dubbo_exporter.go | 4 +- protocol/dubbo/listener.go | 8 +- protocol/dubbo/readwriter.go | 10 +- protocol/jsonrpc/http_test.go | 12 +- protocol/jsonrpc/jsonrpc_exporter.go | 4 +- protocol/jsonrpc/jsonrpc_invoker_test.go | 4 +- protocol/jsonrpc/server.go | 7 +- 64 files changed, 1689 insertions(+), 570 deletions(-) create mode 100644 examples/dubbo/go-server/app/user_provider.go create mode 100644 examples/dubbo/go-server/app/user_provider1.go create mode 100644 examples/dubbo/go-server/app/user_provider2.go create mode 100644 examples/jsonrpc/go-server/app/user_provider.go create mode 100644 examples/jsonrpc/go-server/app/user_provider1.go create mode 100644 examples/jsonrpc/go-server/app/user_provider2.go diff --git a/common/constant/key.go b/common/constant/key.go index 82df44c3e1..bca658b262 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -29,6 +29,7 @@ const ( SERVICE_KEY = "service" METHODS_KEY = "methods" TIMEOUT_KEY = "timeout" + BEAN_NAME_KEY = "bean.name" ) const ( @@ -44,7 +45,6 @@ const ( WEIGHT_KEY = "weight" WARMUP_KEY = "warmup" RETRIES_KEY = "retries" - BEAN_NAME = "bean.name" ) const ( diff --git a/common/proxy/proxy_test.go b/common/proxy/proxy_test.go index 1cc30457c3..8c1c0295d0 100644 --- a/common/proxy/proxy_test.go +++ b/common/proxy/proxy_test.go @@ -43,21 +43,15 @@ type TestService struct { Echo func(interface{}, *interface{}) error } -func (s *TestService) Service() string { +func (s *TestService) Reference() string { return "com.test.Path" } -func (s *TestService) Version() string { - return "" -} type TestServiceInt int -func (s *TestServiceInt) Service() string { +func (s *TestServiceInt) Reference() string { return "com.test.TestServiceInt" } -func (s *TestServiceInt) Version() string { - return "" -} func TestProxy_Implement(t *testing.T) { diff --git a/common/rpc_service.go b/common/rpc_service.go index 0444f0c17e..4741a6fa3c 100644 --- a/common/rpc_service.go +++ b/common/rpc_service.go @@ -36,8 +36,7 @@ import ( // rpc service interface type RPCService interface { - Service() string // Path InterfaceName - Version() string + Reference() string // rpc service id or reference id } // for lowercase func @@ -149,7 +148,7 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error) return "", perrors.New(s) } - sname = rcvr.Service() + sname = rcvr.Reference() if server := sm.GetService(protocol, sname); server != nil { return "", perrors.New("service already defined: " + sname) } @@ -172,8 +171,8 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error) return strings.TrimSuffix(methods, ","), nil } -func (sm *serviceMap) UnRegister(protocol, serviceName string) error { - if protocol == "" || serviceName == "" { +func (sm *serviceMap) UnRegister(protocol, serviceId string) error { + if protocol == "" || serviceId == "" { return perrors.New("protocol or serviceName is nil") } sm.mutex.RLock() @@ -182,16 +181,16 @@ func (sm *serviceMap) UnRegister(protocol, serviceName string) error { sm.mutex.RUnlock() return perrors.New("no services for " + protocol) } - _, ok = svcs[serviceName] + _, ok = svcs[serviceId] if !ok { sm.mutex.RUnlock() - return perrors.New("no service for " + serviceName) + return perrors.New("no service for " + serviceId) } sm.mutex.RUnlock() sm.mutex.Lock() defer sm.mutex.Unlock() - delete(svcs, serviceName) + delete(svcs, serviceId) delete(sm.serviceMap, protocol) return nil diff --git a/common/rpc_service_test.go b/common/rpc_service_test.go index ec4371da47..f0301e01a6 100644 --- a/common/rpc_service_test.go +++ b/common/rpc_service_test.go @@ -39,12 +39,9 @@ func (s *TestService) MethodTwo(arg1, arg2, arg3 interface{}) (interface{}, erro func (s *TestService) MethodThree() error { return nil } -func (s *TestService) Service() string { +func (s *TestService) Reference() string { return "com.test.Path" } -func (s *TestService) Version() string { - return "" -} func (s *TestService) MethodMapper() map[string]string { return map[string]string{ "MethodTwo": "methodTwo", @@ -65,22 +62,16 @@ func (s *testService) Method3(ctx context.Context, args []interface{}, rsp *stru func (s *testService) Method4(ctx context.Context, args []interface{}, rsp *struct{}) *testService { return nil } -func (s *testService) Service() string { +func (s *testService) Reference() string { return "com.test.Path" } -func (s *testService) Version() string { - return "" -} type TestService1 struct { } -func (s *TestService1) Service() string { +func (s *TestService1) Reference() string { return "com.test.Path1" } -func (s *TestService1) Version() string { - return "" -} func TestServiceMap_Register(t *testing.T) { // lowercase @@ -180,7 +171,7 @@ func TestSuiteMethod(t *testing.T) { // wrong number of in return s1 := &testService{} - method, ok = reflect.TypeOf(s1).MethodByName("Version") + method, ok = reflect.TypeOf(s1).MethodByName("Reference") assert.True(t, ok) methodType = suiteMethod(method) assert.Nil(t, methodType) diff --git a/config/base_config_test.go b/config/base_config_test.go index 7676a11d02..d07d983f64 100644 --- a/config/base_config_test.go +++ b/config/base_config_test.go @@ -35,8 +35,8 @@ func Test_refresh(t *testing.T) { c := &BaseConfig{} mockMap := map[string]string{} mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100" - mockMap["dubbo.reference.MockService.MockService.retries"] = "10" - mockMap["dubbo.MockService.MockService.GetUser.retries"] = "10" + mockMap["dubbo.reference.com.MockService.MockService.retries"] = "10" + mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10" mockMap["dubbo.consumer.check"] = "false" mockMap["dubbo.application.name"] = "dubbo" @@ -88,7 +88,7 @@ func Test_refresh(t *testing.T) { }, References: map[string]*ReferenceConfig{ "MockService": { - InterfaceName: "MockService", + InterfaceName: "com.MockService", Protocol: "mock", Cluster: "failover", Loadbalance: "random", @@ -98,13 +98,14 @@ func Test_refresh(t *testing.T) { Methods: []*MethodConfig{ { InterfaceId: "MockService", - InterfaceName: "MockService", + InterfaceName: "com.MockService", Name: "GetUser", Retries: 2, Loadbalance: "random", }, - {InterfaceId: "MockService", - InterfaceName: "MockService", + { + InterfaceId: "MockService", + InterfaceName: "com.MockService", Name: "GetUser1", Retries: 2, Loadbalance: "random", @@ -128,8 +129,8 @@ func Test_refreshProvider(t *testing.T) { c := &BaseConfig{} mockMap := map[string]string{} mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100" - mockMap["dubbo.service.MockService.MockService.retries"] = "10" - mockMap["dubbo.MockService.MockService.GetUser.retries"] = "10" + mockMap["dubbo.service.com.MockService.MockService.retries"] = "10" + mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10" mockMap["dubbo.consumer.check"] = "false" mockMap["dubbo.application.name"] = "dubbo" mockMap["dubbo.protocols.jsonrpc1.name"] = "jsonrpc" @@ -183,7 +184,7 @@ func Test_refreshProvider(t *testing.T) { }, Services: map[string]*ServiceConfig{ "MockService": { - InterfaceName: "MockService", + InterfaceName: "com.MockService", Protocol: "mock", Cluster: "failover", Loadbalance: "random", @@ -193,13 +194,13 @@ func Test_refreshProvider(t *testing.T) { Methods: []*MethodConfig{ { InterfaceId: "MockService", - InterfaceName: "MockService", + InterfaceName: "com.MockService", Name: "GetUser", Retries: 2, Loadbalance: "random", }, {InterfaceId: "MockService", - InterfaceName: "MockService", + InterfaceName: "com.MockService", Name: "GetUser1", Retries: 2, Loadbalance: "random", diff --git a/config/config_loader.go b/config/config_loader.go index 86260c8673..3e73061890 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -71,16 +71,17 @@ func Load() { logger.Errorf("[consumer config center refresh] %#v", err) } refMap = make(map[string]*ReferenceConfig) - for _, ref := range consumerConfig.References { - rpcService := GetConsumerService(ref.InterfaceName) + for key, ref := range consumerConfig.References { + rpcService := GetConsumerService(key) if rpcService == nil { - logger.Warnf("%s is not exsist!", ref.InterfaceName) + logger.Warnf("%s is not exsist!", key) continue } + ref.id = key ref.Refer() ref.Implement(rpcService) - refMap[ref.InterfaceName] = ref + refMap[key] = ref } //wait for invoker is available, if wait over default 3s, then panic @@ -122,17 +123,18 @@ func Load() { logger.Errorf("[provider config center refresh] %#v", err) } srvMap = make(map[string]*ServiceConfig) - for _, svs := range providerConfig.Services { - rpcService := GetProviderService(svs.InterfaceName) + for key, svs := range providerConfig.Services { + rpcService := GetProviderService(key) if rpcService == nil { - logger.Warnf("%s is not exsist!", svs.InterfaceName) + logger.Warnf("%s is not exsist!", key) continue } + svs.id = key svs.Implement(rpcService) if err := svs.Export(); err != nil { - panic(fmt.Sprintf("service %s export failed! ", svs.InterfaceName)) + panic(fmt.Sprintf("service %s export failed! ", key)) } - srvMap[svs.InterfaceName] = svs + srvMap[key] = svs } } } @@ -144,5 +146,5 @@ func GetRPCService(name string) common.RPCService { // create rpc service for consumer func RPCService(service common.RPCService) { - providerConfig.Services[service.Service()].Implement(service) + providerConfig.Services[service.Reference()].Implement(service) } diff --git a/config/config_loader_test.go b/config/config_loader_test.go index 6e9689c763..497117224f 100644 --- a/config/config_loader_test.go +++ b/config/config_loader_test.go @@ -70,12 +70,12 @@ func TestLoad(t *testing.T) { Load() - assert.Equal(t, ms, GetRPCService(ms.Service())) + assert.Equal(t, ms, GetRPCService(ms.Reference())) ms2 := &struct { MockService }{} RPCService(ms2) - assert.NotEqual(t, ms2, GetRPCService(ms2.Service())) + assert.NotEqual(t, ms2, GetRPCService(ms2.Reference())) conServices = map[string]common.RPCService{} proServices = map[string]common.RPCService{} @@ -83,6 +83,7 @@ func TestLoad(t *testing.T) { consumerConfig = nil providerConfig = nil } + func TestWithNoRegLoad(t *testing.T) { doInit() doinit() @@ -98,12 +99,12 @@ func TestWithNoRegLoad(t *testing.T) { Load() - assert.Equal(t, ms, GetRPCService(ms.Service())) + assert.Equal(t, ms, GetRPCService(ms.Reference())) ms2 := &struct { MockService }{} RPCService(ms2) - assert.NotEqual(t, ms2, GetRPCService(ms2.Service())) + assert.NotEqual(t, ms2, GetRPCService(ms2.Reference())) conServices = map[string]common.RPCService{} proServices = map[string]common.RPCService{} @@ -111,6 +112,7 @@ func TestWithNoRegLoad(t *testing.T) { consumerConfig = nil providerConfig = nil } + func TestConfigLoaderWithConfigCenter(t *testing.T) { extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory { return &config_center.MockDynamicConfigurationFactory{} diff --git a/config/mock_rpcservice.go b/config/mock_rpcservice.go index 0b3b9f22b4..64d431ffb6 100644 --- a/config/mock_rpcservice.go +++ b/config/mock_rpcservice.go @@ -23,14 +23,10 @@ import ( type MockService struct{} -func (*MockService) Service() string { +func (*MockService) Reference() string { return "MockService" } -func (*MockService) Version() string { - return "1.0" -} - func (*MockService) GetUser(ctx context.Context, itf []interface{}, str *struct{}) error { return nil } diff --git a/config/reference_config.go b/config/reference_config.go index 2c38d8aa4a..7de8da395e 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -38,6 +38,7 @@ import ( type ReferenceConfig struct { context context.Context pxy *proxy.Proxy + id string InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` Check *bool `yaml:"check" json:"check,omitempty" property:"check"` Url string `yaml:"url" json:"url,omitempty" property:"url"` @@ -75,7 +76,7 @@ func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) erro } func (refconfig *ReferenceConfig) Refer() { - url := common.NewURLWithOptions(common.WithPath(refconfig.InterfaceName), common.WithProtocol(refconfig.Protocol), common.WithParams(refconfig.getUrlMap())) + url := common.NewURLWithOptions(common.WithPath(refconfig.id), common.WithProtocol(refconfig.Protocol), common.WithParams(refconfig.getUrlMap())) //1. user specified URL, could be peer-to-peer address, or register center's address. if refconfig.Url != "" { diff --git a/config/reference_config_test.go b/config/reference_config_test.go index c41e2a16de..d8fc1525b2 100644 --- a/config/reference_config_test.go +++ b/config/reference_config_test.go @@ -81,7 +81,7 @@ func doInit() { References: map[string]*ReferenceConfig{ "MockService": { Registry: "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2", - InterfaceName: "MockService", + InterfaceName: "com.MockService", Protocol: "mock", Cluster: "failover", Loadbalance: "random", @@ -143,6 +143,7 @@ func Test_ReferP2P(t *testing.T) { } consumerConfig = nil } + func Test_ReferMultiP2P(t *testing.T) { doInit() extension.SetProtocol("dubbo", GetProtocol) diff --git a/config/service.go b/config/service.go index 0f3356f710..2bceac4a8c 100644 --- a/config/service.go +++ b/config/service.go @@ -28,12 +28,12 @@ var ( // SetConService is called by init() of implement of RPCService func SetConsumerService(service common.RPCService) { - conServices[service.Service()] = service + conServices[service.Reference()] = service } // SetProService is called by init() of implement of RPCService func SetProviderService(service common.RPCService) { - proServices[service.Service()] = service + proServices[service.Reference()] = service } func GetConsumerService(name string) common.RPCService { diff --git a/config/service_config.go b/config/service_config.go index 79a29aa330..6aadcdf8b0 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -43,6 +43,7 @@ import ( type ServiceConfig struct { context context.Context + id string Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` //multi protocol support, split by ',' InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` @@ -99,15 +100,12 @@ func (srvconfig *ServiceConfig) Export() error { logger.Errorf(err.Error()) return err } - //contextPath := proto.ContextPath - //if contextPath == "" { - // contextPath = providerConfig.Path - //} - url := common.NewURLWithOptions(common.WithPath(srvconfig.InterfaceName), + url := common.NewURLWithOptions(common.WithPath(srvconfig.id), common.WithProtocol(proto.Name), common.WithIp(proto.Ip), common.WithPort(proto.Port), common.WithParams(urlMap), + common.WithParamsValue(constant.BEAN_NAME_KEY, srvconfig.id), common.WithMethods(strings.Split(methods, ","))) if len(regUrls) > 0 { diff --git a/config/service_config_test.go b/config/service_config_test.go index 4e0b7f95d6..e111c8d110 100644 --- a/config/service_config_test.go +++ b/config/service_config_test.go @@ -74,7 +74,7 @@ func doinit() { }, Services: map[string]*ServiceConfig{ "MockService": { - InterfaceName: "MockService", + InterfaceName: "com.MockService", Protocol: "mock", Registry: "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2", Cluster: "failover", diff --git a/config/testdata/consumer_config.yml b/config/testdata/consumer_config.yml index 08ff59f6fc..22e2116029 100644 --- a/config/testdata/consumer_config.yml +++ b/config/testdata/consumer_config.yml @@ -36,8 +36,10 @@ references: registry: "hangzhouzk,shanghaizk" filter: "" protocol : "dubbo" + version: "1.0" + group: "as" interface : "com.ikurento.user.UserProvider" - url: "dubbo://127.0.0.1:20000" + url: "dubbo://127.0.0.1:20000/UserProvider" cluster: "failover" methods : - name: "GetUser" diff --git a/config/testdata/consumer_config_with_configcenter.yml b/config/testdata/consumer_config_with_configcenter.yml index df896d2442..6a60c30837 100644 --- a/config/testdata/consumer_config_with_configcenter.yml +++ b/config/testdata/consumer_config_with_configcenter.yml @@ -9,7 +9,7 @@ references: filter: "" protocol : "dubbo" interface : "com.ikurento.user.UserProvider" - url: "dubbo://127.0.0.1:20000" + url: "dubbo://127.0.0.1:20000/UserProvider" cluster: "failover" methods : - name: "GetUser" diff --git a/config/testdata/provider_config.yml b/config/testdata/provider_config.yml index 38952220e2..621de175ab 100644 --- a/config/testdata/provider_config.yml +++ b/config/testdata/provider_config.yml @@ -33,6 +33,8 @@ services: # equivalent to interface of dubbo.xml interface : "com.ikurento.user.UserProvider" loadbalance: "random" + version: "1.0" + group: "as" warmup: "100" cluster: "failover" methods: diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index 7918e2df0f..0b77e60cec 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -59,6 +59,45 @@ func main() { config.Load() + println("\n\ntest") + test() + println("\n\ntest1") + test1() + println("\n\ntest2") + test2() + + initSignal() +} + +func initSignal() { + signals := make(chan os.Signal, 1) + // It is not possible to block SIGKILL or syscall.SIGSTOP + signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, + syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + sig := <-signals + logger.Infof("get signal %s", sig.String()) + switch sig { + case syscall.SIGHUP: + // reload() + default: + go time.AfterFunc(time.Duration(survivalTimeout), func() { + logger.Warnf("app exit now by force...") + os.Exit(1) + }) + + // 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出 + fmt.Println("app exit now...") + return + } + } +} + +func println(format string, args ...interface{}) { + fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...) +} + +func test() { println("\n\n\necho") res, err := userProvider.Echo(context.TODO(), "OK") if err != nil { @@ -109,43 +148,143 @@ func main() { println("\n\n\nstart to test dubbo - getErr") user = &User{} err = userProvider.GetErr(context.TODO(), []interface{}{"A003"}, user) - if err != nil { - println("getErr - error: %v", err) + if err == nil { + panic("err is nil") } + println("getErr - error: %v", err) println("\n\n\nstart to test dubbo illegal method") err = userProvider.GetUser1(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("error: %v", err) +} + +func test1() { + println("\n\n\necho") + res, err := userProvider1.Echo(context.TODO(), "OK") if err != nil { panic(err) } + println("res: %v\n", res) - initSignal() -} + time.Sleep(3e9) -func initSignal() { - signals := make(chan os.Signal, 1) - // It is not possible to block SIGKILL or syscall.SIGSTOP - signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, - syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) - for { - sig := <-signals - logger.Infof("get signal %s", sig.String()) - switch sig { - case syscall.SIGHUP: - // reload() - default: - go time.AfterFunc(time.Duration(survivalTimeout)*time.Second, func() { - logger.Warnf("app exit now by force...") - os.Exit(1) - }) + println("\n\n\nstart to test dubbo") + user := &User{} + err = userProvider1.GetUser(context.TODO(), []interface{}{"A003"}, user) + if err != nil { + panic(err) + } + println("response result: %v", user) - // 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出 - fmt.Println("app exit now...") - return - } + println("\n\n\nstart to test dubbo - GetUser0") + ret, err := userProvider1.GetUser0("A003", "Moorse") + if err != nil { + panic(err) } + println("response result: %v", ret) + + println("\n\n\nstart to test dubbo - GetUsers") + ret1, err := userProvider1.GetUsers([]interface{}{[]interface{}{"A002", "A003"}}) + if err != nil { + panic(err) + } + println("response result: %v", ret1) + + println("\n\n\nstart to test dubbo - getUser") + user = &User{} + var i int32 = 1 + err = userProvider1.GetUser2(context.TODO(), []interface{}{i}, user) + if err != nil { + panic(err) + } + println("response result: %v", user) + + println("\n\n\nstart to test dubbo - GetUser3") + err = userProvider1.GetUser3() + if err != nil { + panic(err) + } + println("succ!") + + println("\n\n\nstart to test dubbo - getErr") + user = &User{} + err = userProvider1.GetErr(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("getErr - error: %v", err) + + println("\n\n\nstart to test dubbo illegal method") + err = userProvider1.GetUser1(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("error: %v", err) } -func println(format string, args ...interface{}) { - fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...) +func test2() { + println("\n\n\necho") + res, err := userProvider2.Echo(context.TODO(), "OK") + if err != nil { + panic(err) + } + println("res: %v\n", res) + + time.Sleep(3e9) + + println("\n\n\nstart to test dubbo") + user := &User{} + err = userProvider2.GetUser(context.TODO(), []interface{}{"A003"}, user) + if err != nil { + panic(err) + } + println("response result: %v", user) + + println("\n\n\nstart to test dubbo - GetUser0") + ret, err := userProvider2.GetUser0("A003", "Moorse") + if err != nil { + panic(err) + } + println("response result: %v", ret) + + println("\n\n\nstart to test dubbo - GetUsers") + ret1, err := userProvider2.GetUsers([]interface{}{[]interface{}{"A002", "A003"}}) + if err != nil { + panic(err) + } + println("response result: %v", ret1) + + println("\n\n\nstart to test dubbo - getUser") + user = &User{} + var i int32 = 1 + err = userProvider2.GetUser2(context.TODO(), []interface{}{i}, user) + if err != nil { + panic(err) + } + println("response result: %v", user) + + println("\n\n\nstart to test dubbo - GetUser3") + err = userProvider2.GetUser3() + if err != nil { + panic(err) + } + println("succ!") + + println("\n\n\nstart to test dubbo - getErr") + user = &User{} + err = userProvider2.GetErr(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("getErr - error: %v", err) + + println("\n\n\nstart to test dubbo illegal method") + err = userProvider2.GetUser1(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("error: %v", err) } diff --git a/examples/dubbo/go-client/app/user.go b/examples/dubbo/go-client/app/user.go index d491c36333..ac64d06811 100644 --- a/examples/dubbo/go-client/app/user.go +++ b/examples/dubbo/go-client/app/user.go @@ -34,10 +34,16 @@ import ( type Gender hessian.JavaEnum -var userProvider = new(UserProvider) +var ( + userProvider = new(UserProvider) + userProvider1 = new(UserProvider1) + userProvider2 = new(UserProvider2) +) func init() { config.SetConsumerService(userProvider) + config.SetConsumerService(userProvider1) + config.SetConsumerService(userProvider2) } const ( @@ -108,10 +114,36 @@ type UserProvider struct { Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" +func (u *UserProvider) Reference() string { + return "UserProvider" +} + +type UserProvider1 struct { + GetUsers func(req []interface{}) ([]interface{}, error) + GetErr func(ctx context.Context, req []interface{}, rsp *User) error + GetUser func(ctx context.Context, req []interface{}, rsp *User) error + GetUser0 func(id string, name string) (User, error) + GetUser1 func(ctx context.Context, req []interface{}, rsp *User) error + GetUser2 func(ctx context.Context, req []interface{}, rsp *User) error `dubbo:"getUser"` + GetUser3 func() error + Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used +} + +func (u *UserProvider1) Reference() string { + return "UserProvider1" +} + +type UserProvider2 struct { + GetUsers func(req []interface{}) ([]interface{}, error) + GetErr func(ctx context.Context, req []interface{}, rsp *User) error + GetUser func(ctx context.Context, req []interface{}, rsp *User) error + GetUser0 func(id string, name string) (User, error) + GetUser1 func(ctx context.Context, req []interface{}, rsp *User) error + GetUser2 func(ctx context.Context, req []interface{}, rsp *User) error `dubbo:"getUser"` + GetUser3 func() error + Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used } -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider2) Reference() string { + return "UserProvider2" } diff --git a/examples/dubbo/go-client/profiles/dev/client.yml b/examples/dubbo/go-client/profiles/dev/client.yml index 1595f23c34..3ff83a4482 100644 --- a/examples/dubbo/go-client/profiles/dev/client.yml +++ b/examples/dubbo/go-client/profiles/dev/client.yml @@ -18,7 +18,6 @@ application_config: registries : "hangzhouzk": - # 对应java配置中address属性的zookeeper protocol: "zookeeper" timeout : "3s" address: "127.0.0.1:2181" @@ -39,8 +38,27 @@ references: interface : "com.ikurento.user.UserProvider" cluster: "failover" methods : - - name: "GetUser" - retries: 3 + - name: "GetUser" + retries: 3 + "UserProvider1": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 + "UserProvider2": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 protocol_conf: dubbo: diff --git a/examples/dubbo/go-client/profiles/release/client.yml b/examples/dubbo/go-client/profiles/release/client.yml index 7a193ea64d..18fef2e78f 100644 --- a/examples/dubbo/go-client/profiles/release/client.yml +++ b/examples/dubbo/go-client/profiles/release/client.yml @@ -35,13 +35,30 @@ references: # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 registry: "hangzhouzk" protocol : "dubbo" -# version: "2.0" -# group: "as" interface : "com.ikurento.user.UserProvider" cluster: "failover" methods : - name: "GetUser" retries: 3 + "UserProvider1": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 + "UserProvider2": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 protocol_conf: dubbo: diff --git a/examples/dubbo/go-client/profiles/test/client.yml b/examples/dubbo/go-client/profiles/test/client.yml index 24ede19d7d..44b50fad65 100644 --- a/examples/dubbo/go-client/profiles/test/client.yml +++ b/examples/dubbo/go-client/profiles/test/client.yml @@ -35,13 +35,30 @@ references: # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 registry: "hangzhouzk" protocol : "dubbo" -# version: "2.0" -# group: "as" interface : "com.ikurento.user.UserProvider" cluster: "failover" methods : - - name: "GetUser" - retries: 3 + - name: "GetUser" + retries: 3 + "UserProvider1": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 + "UserProvider2": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 protocol_conf: dubbo: diff --git a/examples/dubbo/go-server/app/server.go b/examples/dubbo/go-server/app/server.go index 788fc665b8..23c9b7e659 100644 --- a/examples/dubbo/go-server/app/server.go +++ b/examples/dubbo/go-server/app/server.go @@ -74,7 +74,7 @@ func initSignal() { case syscall.SIGHUP: // reload() default: - go time.AfterFunc(time.Duration(float64(survivalTimeout)*float64(time.Second)), func() { + go time.AfterFunc(time.Duration(survivalTimeout), func() { logger.Warnf("app exit now by force...") os.Exit(1) }) diff --git a/examples/dubbo/go-server/app/user.go b/examples/dubbo/go-server/app/user.go index fcd9ea7b86..dee0dd5eae 100644 --- a/examples/dubbo/go-server/app/user.go +++ b/examples/dubbo/go-server/app/user.go @@ -18,7 +18,6 @@ package main import ( - "context" "fmt" "strconv" "time" @@ -26,19 +25,10 @@ import ( import ( "github.com/dubbogo/hessian2" - perrors "github.com/pkg/errors" -) - -import ( - "github.com/apache/dubbo-go/config" ) type Gender hessian.JavaEnum -func init() { - config.SetProviderService(new(UserProvider)) -} - const ( MAN hessian.JavaEnum = iota WOMAN @@ -85,10 +75,6 @@ type ( Time time.Time Sex Gender // 注意此处,java enum Object <--> go string } - - UserProvider struct { - user map[string]User - } ) var ( @@ -97,17 +83,17 @@ var ( Sex: Gender(MAN), } - userMap = UserProvider{user: make(map[string]User)} + userMap = make(map[string]User) ) func init() { - userMap.user["A000"] = DefaultUser - userMap.user["A001"] = User{Id: "001", Name: "ZhangSheng", Age: 18, Sex: Gender(MAN)} - userMap.user["A002"] = User{Id: "002", Name: "Lily", Age: 20, Sex: Gender(WOMAN)} - userMap.user["A003"] = User{Id: "113", Name: "Moorse", Age: 30, Sex: Gender(WOMAN)} - for k, v := range userMap.user { + userMap["A000"] = DefaultUser + userMap["A001"] = User{Id: "001", Name: "ZhangSheng", Age: 18, Sex: Gender(MAN)} + userMap["A002"] = User{Id: "002", Name: "Lily", Age: 20, Sex: Gender(WOMAN)} + userMap["A003"] = User{Id: "113", Name: "Moorse", Age: 30, Sex: Gender(WOMAN)} + for k, v := range userMap { v.Time = time.Now() - userMap.user[k] = v + userMap[k] = v } } @@ -122,92 +108,6 @@ func (u User) JavaClassName() string { return "com.ikurento.user.User" } -func (u *UserProvider) getUser(userId string) (*User, error) { - if user, ok := userMap.user[userId]; ok { - return &user, nil - } - - return nil, fmt.Errorf("invalid user id:%s", userId) -} - -func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error { - var ( - err error - user *User - ) - - println("req:%#v", req) - user, err = u.getUser(req[0].(string)) - if err == nil { - *rsp = *user - println("rsp:%#v", rsp) - } - return err -} - -func (u *UserProvider) GetUser0(id string, name string) (User, error) { - var err error - - println("id:%s, name:%s", id, name) - user, err := u.getUser(id) - if err != nil { - return User{}, err - } - if user.Name != name { - return User{}, perrors.New("name is not " + user.Name) - } - return *user, err -} - -func (u *UserProvider) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { - var err error - - println("req:%#v", req) - rsp.Id = strconv.Itoa(int(req[0].(int32))) - return err -} - -func (u *UserProvider) GetUser3() error { - return nil -} - -func (u *UserProvider) GetErr(ctx context.Context, req []interface{}, rsp *User) error { - return hessian.NewThrowable("exception") -} - -func (u *UserProvider) GetUsers(req []interface{}) ([]interface{}, error) { - var err error - - println("req:%s", req) - t := req[0].([]interface{}) - user, err := u.getUser(t[0].(string)) - if err != nil { - return nil, err - } - println("user:%v", user) - user1, err := u.getUser(t[1].(string)) - if err != nil { - return nil, err - } - println("user1:%v", user1) - - return []interface{}{user, user1}, err -} - -func (s *UserProvider) MethodMapper() map[string]string { - return map[string]string{ - "GetUser2": "getUser", - } -} - -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" -} - func println(format string, args ...interface{}) { fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...) } diff --git a/examples/dubbo/go-server/app/user_provider.go b/examples/dubbo/go-server/app/user_provider.go new file mode 100644 index 0000000000..7b3721d0c7 --- /dev/null +++ b/examples/dubbo/go-server/app/user_provider.go @@ -0,0 +1,102 @@ +package main + +import ( + "context" + "fmt" + "strconv" +) + +import ( + "github.com/apache/dubbo-go/config" + "github.com/dubbogo/hessian2/java_exception" + perrors "github.com/pkg/errors" +) + +func init() { + config.SetProviderService(new(UserProvider)) +} + +type UserProvider struct { +} + +func (u *UserProvider) getUser(userId string) (*User, error) { + if user, ok := userMap[userId]; ok { + return &user, nil + } + + return nil, fmt.Errorf("invalid user id:%s", userId) +} + +func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error { + var ( + err error + user *User + ) + + println("req:%#v", req) + user, err = u.getUser(req[0].(string)) + if err == nil { + *rsp = *user + println("rsp:%#v", rsp) + } + return err +} + +func (u *UserProvider) GetUser0(id string, name string) (User, error) { + var err error + + println("id:%s, name:%s", id, name) + user, err := u.getUser(id) + if err != nil { + return User{}, err + } + if user.Name != name { + return User{}, perrors.New("name is not " + user.Name) + } + return *user, err +} + +func (u *UserProvider) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { + var err error + + println("req:%#v", req) + rsp.Id = strconv.Itoa(int(req[0].(int32))) + return err +} + +func (u *UserProvider) GetUser3() error { + return nil +} + +func (u *UserProvider) GetErr(ctx context.Context, req []interface{}, rsp *User) error { + return java_exception.NewThrowable("exception") +} + +func (u *UserProvider) GetUsers(req []interface{}) ([]interface{}, error) { + var err error + + println("req:%s", req) + t := req[0].([]interface{}) + user, err := u.getUser(t[0].(string)) + if err != nil { + return nil, err + } + println("user:%v", user) + user1, err := u.getUser(t[1].(string)) + if err != nil { + return nil, err + } + println("user1:%v", user1) + + return []interface{}{user, user1}, err +} + +func (s *UserProvider) MethodMapper() map[string]string { + return map[string]string{ + "GetUser2": "getUser", + } +} + +func (u *UserProvider) Reference() string { + return "UserProvider" +} diff --git a/examples/dubbo/go-server/app/user_provider1.go b/examples/dubbo/go-server/app/user_provider1.go new file mode 100644 index 0000000000..d101973036 --- /dev/null +++ b/examples/dubbo/go-server/app/user_provider1.go @@ -0,0 +1,88 @@ +package main + +import ( + "context" + "fmt" + "strconv" +) + +import ( + "github.com/apache/dubbo-go/config" + "github.com/dubbogo/hessian2/java_exception" + perrors "github.com/pkg/errors" +) + +func init() { + config.SetProviderService(new(UserProvider1)) +} + +type UserProvider1 struct { +} + +func (u *UserProvider1) getUser(userId string) (*User, error) { + if user, ok := userMap[userId]; ok { + return &user, nil + } + + return nil, fmt.Errorf("invalid user id:%s", userId) +} + +func (u *UserProvider1) GetUser(ctx context.Context, req []interface{}, rsp *User) error { + var ( + err error + user *User + ) + + println("req:%#v", req) + user, err = u.getUser(req[0].(string)) + if err == nil { + *rsp = *user + println("rsp:%#v", rsp) + } + return err +} + +func (u *UserProvider1) GetUser0(id string, name string) (User, error) { + var err error + + println("id:%s, name:%s", id, name) + user, err := u.getUser(id) + if err != nil { + return User{}, err + } + if user.Name != name { + return User{}, perrors.New("name is not " + user.Name) + } + return *user, err +} + +func (u *UserProvider1) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { + var err error + + println("req:%#v", req) + rsp.Id = strconv.Itoa(int(req[0].(int32))) + return err +} + +func (u *UserProvider1) GetUser3() error { + return nil +} + +func (u *UserProvider1) GetErr(ctx context.Context, req []interface{}, rsp *User) error { + return java_exception.NewThrowable("exception") +} + +func (u *UserProvider1) GetUsers(req []interface{}) ([]interface{}, error) { + + return []interface{}{}, nil +} + +func (s *UserProvider1) MethodMapper() map[string]string { + return map[string]string{ + "GetUser2": "getUser", + } +} + +func (u *UserProvider1) Reference() string { + return "UserProvider1" +} diff --git a/examples/dubbo/go-server/app/user_provider2.go b/examples/dubbo/go-server/app/user_provider2.go new file mode 100644 index 0000000000..556301ca5a --- /dev/null +++ b/examples/dubbo/go-server/app/user_provider2.go @@ -0,0 +1,97 @@ +package main + +import ( + "context" + "fmt" + "strconv" +) + +import ( + "github.com/apache/dubbo-go/config" + "github.com/dubbogo/hessian2/java_exception" + perrors "github.com/pkg/errors" +) + +func init() { + config.SetProviderService(new(UserProvider2)) +} + +type UserProvider2 struct { +} + +func (u *UserProvider2) getUser(userId string) (*User, error) { + if user, ok := userMap[userId]; ok { + return &user, nil + } + + return nil, fmt.Errorf("invalid user id:%s", userId) +} + +func (u *UserProvider2) GetUser(ctx context.Context, req []interface{}, rsp *User) error { + var ( + err error + user *User + ) + + println("req:%#v", req) + user, err = u.getUser(req[0].(string)) + if err == nil { + *rsp = *user + println("rsp:%#v", rsp) + } + return err +} + +func (u *UserProvider2) GetUser0(id string, name string) (User, error) { + var err error + + println("id:%s, name:%s", id, name) + user, err := u.getUser(id) + if err != nil { + return User{}, err + } + if user.Name != name { + return User{}, perrors.New("name is not " + user.Name) + } + return *user, err +} + +func (u *UserProvider2) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { + var err error + + println("req:%#v", req) + rsp.Id = strconv.Itoa(int(req[0].(int32))) + return err +} + +func (u *UserProvider2) GetUser3() error { + return nil +} + +func (u *UserProvider2) GetErr(ctx context.Context, req []interface{}, rsp *User) error { + return java_exception.NewThrowable("exception") +} + +func (u *UserProvider2) GetUsers(req []interface{}) ([]interface{}, error) { + var err error + + println("req:%s", req) + t := req[0].([]interface{}) + user, err := u.getUser(t[0].(string)) + if err != nil { + return nil, err + } + println("user:%v", user) + + return []interface{}{user}, err +} + +func (s *UserProvider2) MethodMapper() map[string]string { + return map[string]string{ + "GetUser2": "getUser", + } +} + +func (u *UserProvider2) Reference() string { + return "UserProvider2" +} diff --git a/examples/dubbo/go-server/profiles/dev/server.yml b/examples/dubbo/go-server/profiles/dev/server.yml index bc4b288542..6ccceea621 100644 --- a/examples/dubbo/go-server/profiles/dev/server.yml +++ b/examples/dubbo/go-server/profiles/dev/server.yml @@ -27,7 +27,6 @@ registries : services: - "UserProvider": # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 registry: "hangzhouzk" @@ -38,15 +37,40 @@ services: warmup: "100" cluster: "failover" methods: - - name: "GetUser" - retries: 1 - loadbalance: "random" + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider1": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider2": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" protocols: "dubbo1": - name: "dubbo" - # ip : "127.0.0.1" - port: 20000 + name: "dubbo" + # ip : "127.0.0.1" + port: 20000 protocol_conf: diff --git a/examples/dubbo/go-server/profiles/release/server.yml b/examples/dubbo/go-server/profiles/release/server.yml index 3db12902f1..d759946a07 100644 --- a/examples/dubbo/go-server/profiles/release/server.yml +++ b/examples/dubbo/go-server/profiles/release/server.yml @@ -39,6 +39,31 @@ services: - name: "GetUser" retries: 1 loadbalance: "random" + "UserProvider1": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider2": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" protocols: "dubbo1": diff --git a/examples/dubbo/go-server/profiles/test/server.yml b/examples/dubbo/go-server/profiles/test/server.yml index 12f10e004c..f2cd38dc02 100644 --- a/examples/dubbo/go-server/profiles/test/server.yml +++ b/examples/dubbo/go-server/profiles/test/server.yml @@ -36,15 +36,40 @@ services: warmup: "100" cluster: "failover" methods: - - name: "GetUser" - retries: 1 - loadbalance: "random" + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider1": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider2": + registry: "hangzhouzk" + protocol: "dubbo" + version: "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" protocols: "dubbo1": - name: "dubbo" - # ip : "127.0.0.1" - port: 20000 + name: "dubbo" + # ip : "127.0.0.1" + port: 20000 protocol_conf: diff --git a/examples/dubbo/java-client/src/main/java/com/ikurento/user/Consumer.java b/examples/dubbo/java-client/src/main/java/com/ikurento/user/Consumer.java index edf4c0d2b2..9122a629f4 100644 --- a/examples/dubbo/java-client/src/main/java/com/ikurento/user/Consumer.java +++ b/examples/dubbo/java-client/src/main/java/com/ikurento/user/Consumer.java @@ -19,23 +19,32 @@ public class Consumer { //定义一个私有变量 (Spring中要求) private UserProvider userProvider; + private UserProvider userProvider1; + private UserProvider userProvider2; //Spring注入(Spring中要求) public void setUserProvider(UserProvider u) { this.userProvider = u; } + public void setUserProvider1(UserProvider u) { + this.userProvider1 = u; + } + public void setUserProvider2(UserProvider u) { + this.userProvider2 = u; + } - private void benchmarkSayHello() { - for (int i = 0; i < Integer.MAX_VALUE; i ++) { - try { - // String hello = demoService.sayHello("world" + i); - // System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hello); - } catch (Exception e) { - e.printStackTrace(); - } - - // Thread.sleep(2000); - } + //启动consumer的入口函数(在配置文件中指定) + public void start() throws Exception { + System.out.println("\n\ntest"); + testGetUser(); + testGetUsers(); + System.out.println("\n\ntest1"); + testGetUser1(); + testGetUsers1(); + System.out.println("\n\ntest2"); + testGetUser2(); + testGetUsers2(); + Thread.sleep(2000); } private void testGetUser() throws Exception { @@ -49,12 +58,12 @@ private void testGetUser() throws Exception { + ", age:" + user1.getAge() + ", time:" + user1.getTime().toString()); User user2 = userProvider.GetUser0("A003","Moorse"); System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + - " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() - + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); + " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() + + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); User user3 = userProvider.getUser(1); System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + - " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() - + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); + " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() + + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); userProvider.GetUser3(); System.out.println("GetUser3 succ"); @@ -86,16 +95,114 @@ private void testGetUsers() throws Exception { " UserInfo, Id:" + user.getId() + ", name:" + user.getName() + ", sex:" + user.getSex().toString() + ", age:" + user.getAge() + ", time:" + user.getTime().toString()); } - } catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } } - //启动consumer的入口函数(在配置文件中指定) - public void start() throws Exception { - testGetUser(); - testGetUsers(); -// Thread.sleep(120000); -Thread.sleep(2000); + private void testGetUser1() throws Exception { + try { + EchoService echoService = (EchoService)userProvider1; + Object status = echoService.$echo("OK"); + System.out.println("echo: "+status); + User user1 = userProvider1.GetUser("A003"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user1.getId() + ", name:" + user1.getName() + ", sex:" + user1.getSex().toString() + + ", age:" + user1.getAge() + ", time:" + user1.getTime().toString()); + User user2 = userProvider1.GetUser0("A003","Moorse"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() + + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); + User user3 = userProvider1.getUser(1); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() + + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); + userProvider1.GetUser3(); + System.out.println("GetUser3 succ"); + + User user9 = userProvider1.GetUser1("A003"); + } catch (Exception e) { + System.out.println("*************exception***********"); + e.printStackTrace(); + } + try { + userProvider1.GetErr("A003"); + } catch (Throwable t) { + System.out.println("*************exception***********"); + t.printStackTrace(); + } + } + + private void testGetUsers1() throws Exception { + try { + List userIDList = new ArrayList(); + userIDList.add("A001"); + userIDList.add("A002"); + userIDList.add("A003"); + + List userList = userProvider1.GetUsers(userIDList); + + for (int i = 0; i < userList.size(); i++) { + User user = userList.get(i); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user.getId() + ", name:" + user.getName() + ", sex:" + user.getSex().toString() + + ", age:" + user.getAge() + ", time:" + user.getTime().toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void testGetUser2() throws Exception { + try { + EchoService echoService = (EchoService)userProvider2; + Object status = echoService.$echo("OK"); + System.out.println("echo: "+status); + User user1 = userProvider2.GetUser("A003"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user1.getId() + ", name:" + user1.getName() + ", sex:" + user1.getSex().toString() + + ", age:" + user1.getAge() + ", time:" + user1.getTime().toString()); + User user2 = userProvider2.GetUser0("A003","Moorse"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() + + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); + User user3 = userProvider2.getUser(1); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() + + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); + userProvider2.GetUser3(); + System.out.println("GetUser3 succ"); + + User user9 = userProvider2.GetUser1("A003"); + } catch (Exception e) { + System.out.println("*************exception***********"); + e.printStackTrace(); + } + try { + userProvider2.GetErr("A003"); + } catch (Throwable t) { + System.out.println("*************exception***********"); + t.printStackTrace(); + } + } + + private void testGetUsers2() throws Exception { + try { + List userIDList = new ArrayList(); + userIDList.add("A001"); + userIDList.add("A002"); + userIDList.add("A003"); + + List userList = userProvider2.GetUsers(userIDList); + + for (int i = 0; i < userList.size(); i++) { + User user = userList.get(i); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user.getId() + ", name:" + user.getName() + ", sex:" + user.getSex().toString() + + ", age:" + user.getAge() + ", time:" + user.getTime().toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/examples/dubbo/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml b/examples/dubbo/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml index 727007257f..e7e5445a95 100644 --- a/examples/dubbo/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml +++ b/examples/dubbo/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml @@ -33,9 +33,12 @@ - + + + + diff --git a/examples/dubbo/java-client/src/main/resources/META-INF/spring/service.xml b/examples/dubbo/java-client/src/main/resources/META-INF/spring/service.xml index 625a879f97..db9fc3cba5 100644 --- a/examples/dubbo/java-client/src/main/resources/META-INF/spring/service.xml +++ b/examples/dubbo/java-client/src/main/resources/META-INF/spring/service.xml @@ -24,6 +24,8 @@ + + diff --git a/examples/dubbo/with-configcenter-go-client/app/user.go b/examples/dubbo/with-configcenter-go-client/app/user.go index d491c36333..bbc55283bf 100644 --- a/examples/dubbo/with-configcenter-go-client/app/user.go +++ b/examples/dubbo/with-configcenter-go-client/app/user.go @@ -108,10 +108,6 @@ type UserProvider struct { Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider) Reference() string { + return "UserProvider" } diff --git a/examples/dubbo/with-configcenter-go-server/app/user.go b/examples/dubbo/with-configcenter-go-server/app/user.go index e4400cc270..def7eec101 100644 --- a/examples/dubbo/with-configcenter-go-server/app/user.go +++ b/examples/dubbo/with-configcenter-go-server/app/user.go @@ -25,12 +25,13 @@ import ( ) import ( + hessian "github.com/dubbogo/hessian2" + "github.com/dubbogo/hessian2/java_exception" perrors "github.com/pkg/errors" ) import ( "github.com/apache/dubbo-go/config" - hessian "github.com/dubbogo/hessian2" ) type Gender hessian.JavaEnum @@ -146,7 +147,7 @@ func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User } func (u *UserProvider) GetErr(ctx context.Context, req []interface{}, rsp *User) error { - return hessian.NewThrowable("exception") + return java_exception.NewThrowable("exception") } func (u *UserProvider) GetUser0(id string, name string) (User, error) { @@ -182,12 +183,8 @@ func (u *UserProvider) GetUsers(req []interface{}) ([]interface{}, error) { return []interface{}{user, user1}, err } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider) Reference() string { + return "UserProvider" } func println(format string, args ...interface{}) { diff --git a/examples/jsonrpc/go-client/app/client.go b/examples/jsonrpc/go-client/app/client.go index 478a88d194..b781f860bc 100644 --- a/examples/jsonrpc/go-client/app/client.go +++ b/examples/jsonrpc/go-client/app/client.go @@ -51,6 +51,45 @@ func main() { config.Load() + println("\n\ntest") + test() + println("\n\ntest1") + test1() + println("\n\ntest2") + test2() + + initSignal() +} + +func initSignal() { + signals := make(chan os.Signal, 1) + // It is not possible to block SIGKILL or syscall.SIGSTOP + signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, + syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + sig := <-signals + logger.Infof("get signal %s", sig.String()) + switch sig { + case syscall.SIGHUP: + // reload() + default: + go time.AfterFunc(time.Duration(survivalTimeout), func() { + logger.Warnf("app exit now by force...") + os.Exit(1) + }) + + // 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出 + fmt.Println("app exit now...") + return + } + } +} + +func println(format string, args ...interface{}) { + fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...) +} + +func test() { println("\n\n\necho") res, err := userProvider.Echo(context.TODO(), "OK") if err != nil { @@ -100,37 +139,120 @@ func main() { println("\n\n\nstart to test jsonrpc illegal method") err = userProvider.GetUser1(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("error: %v", err) +} + +func test1() { + println("\n\n\necho") + res, err := userProvider1.Echo(context.TODO(), "OK") + if err != nil { + println("echo - error: %v", err) + } else { + println("res: %v", res) + } + + time.Sleep(3e9) + + println("\n\n\nstart to test jsonrpc") + user := &JsonRPCUser{} + err = userProvider1.GetUser(context.TODO(), []interface{}{"A003"}, user) if err != nil { panic(err) } + println("response result: %v", user) - initSignal() -} + println("\n\n\nstart to test jsonrpc - GetUser0") + ret, err := userProvider1.GetUser0("A003", "Moorse") + if err != nil { + panic(err) + } + println("response result: %v", ret) -func initSignal() { - signals := make(chan os.Signal, 1) - // It is not possible to block SIGKILL or syscall.SIGSTOP - signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, - syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) - for { - sig := <-signals - logger.Infof("get signal %s", sig.String()) - switch sig { - case syscall.SIGHUP: - // reload() - default: - go time.AfterFunc(time.Duration(survivalTimeout)*time.Second, func() { - logger.Warnf("app exit now by force...") - os.Exit(1) - }) + println("\n\n\nstart to test jsonrpc - GetUsers") + ret1, err := userProvider1.GetUsers([]interface{}{[]interface{}{"A002", "A003"}}) + if err != nil { + panic(err) + } + println("response result: %v", ret1) - // 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出 - fmt.Println("app exit now...") - return - } + println("\n\n\nstart to test jsonrpc - getUser") + user = &JsonRPCUser{} + err = userProvider1.GetUser2(context.TODO(), []interface{}{1}, user) + if err != nil { + panic(err) + } + println("response result: %v", user) + + println("\n\n\nstart to test jsonrpc - GetUser3") + err = userProvider1.GetUser3() + if err != nil { + panic(err) } + println("succ!") + + println("\n\n\nstart to test jsonrpc illegal method") + err = userProvider1.GetUser1(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("error: %v", err) } -func println(format string, args ...interface{}) { - fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...) +func test2() { + println("\n\n\necho") + res, err := userProvider2.Echo(context.TODO(), "OK") + if err != nil { + println("echo - error: %v", err) + } else { + println("res: %v", res) + } + + time.Sleep(3e9) + + println("\n\n\nstart to test jsonrpc") + user := &JsonRPCUser{} + err = userProvider2.GetUser(context.TODO(), []interface{}{"A003"}, user) + if err != nil { + panic(err) + } + println("response result: %v", user) + + println("\n\n\nstart to test jsonrpc - GetUser0") + ret, err := userProvider2.GetUser0("A003", "Moorse") + if err != nil { + panic(err) + } + println("response result: %v", ret) + + println("\n\n\nstart to test jsonrpc - GetUsers") + ret1, err := userProvider2.GetUsers([]interface{}{[]interface{}{"A002", "A003"}}) + if err != nil { + panic(err) + } + println("response result: %v", ret1) + + println("\n\n\nstart to test jsonrpc - getUser") + user = &JsonRPCUser{} + err = userProvider2.GetUser2(context.TODO(), []interface{}{1}, user) + if err != nil { + panic(err) + } + println("response result: %v", user) + + println("\n\n\nstart to test jsonrpc - GetUser3") + err = userProvider2.GetUser3() + if err != nil { + panic(err) + } + println("succ!") + + println("\n\n\nstart to test jsonrpc illegal method") + err = userProvider2.GetUser1(context.TODO(), []interface{}{"A003"}, user) + if err == nil { + panic("err is nil") + } + println("error: %v", err) } diff --git a/examples/jsonrpc/go-client/app/user.go b/examples/jsonrpc/go-client/app/user.go index ca98b1af0b..c6fdbe13a8 100644 --- a/examples/jsonrpc/go-client/app/user.go +++ b/examples/jsonrpc/go-client/app/user.go @@ -27,10 +27,16 @@ import ( "github.com/apache/dubbo-go/config" ) -var userProvider = new(UserProvider) +var ( + userProvider = new(UserProvider) + userProvider1 = new(UserProvider1) + userProvider2 = new(UserProvider2) +) func init() { config.SetConsumerService(userProvider) + config.SetConsumerService(userProvider1) + config.SetConsumerService(userProvider2) } type JsonRPCUser struct { @@ -58,10 +64,34 @@ type UserProvider struct { Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" +func (u *UserProvider) Reference() string { + return "UserProvider" +} + +type UserProvider1 struct { + GetUsers func(req []interface{}) ([]JsonRPCUser, error) + GetUser func(ctx context.Context, req []interface{}, rsp *JsonRPCUser) error + GetUser0 func(id string, name string) (JsonRPCUser, error) + GetUser1 func(ctx context.Context, req []interface{}, rsp *JsonRPCUser) error + GetUser2 func(ctx context.Context, req []interface{}, rsp *JsonRPCUser) error `dubbo:"getUser"` + GetUser3 func() error + Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used +} + +func (u *UserProvider1) Reference() string { + return "UserProvider1" +} + +type UserProvider2 struct { + GetUsers func(req []interface{}) ([]JsonRPCUser, error) + GetUser func(ctx context.Context, req []interface{}, rsp *JsonRPCUser) error + GetUser0 func(id string, name string) (JsonRPCUser, error) + GetUser1 func(ctx context.Context, req []interface{}, rsp *JsonRPCUser) error + GetUser2 func(ctx context.Context, req []interface{}, rsp *JsonRPCUser) error `dubbo:"getUser"` + GetUser3 func() error + Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used } -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider2) Reference() string { + return "UserProvider2" } diff --git a/examples/jsonrpc/go-client/profiles/dev/client.yml b/examples/jsonrpc/go-client/profiles/dev/client.yml index 8d4346510f..c8f2ee3a33 100644 --- a/examples/jsonrpc/go-client/profiles/dev/client.yml +++ b/examples/jsonrpc/go-client/profiles/dev/client.yml @@ -8,12 +8,12 @@ connect_timeout : "3s" # application config application_config: - organization : "ikurento.com" - name : "BDTService" - module : "dubbogo user-info client" - version : "0.0.1" - owner : "ZX" - environment : "dev" + organization : "ikurento.com" + name : "BDTService" + module : "dubbogo user-info client" + version : "0.0.1" + owner : "ZX" + environment : "dev" registries : "hangzhouzk": @@ -34,13 +34,30 @@ references: # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 registry: "hangzhouzk" protocol : "jsonrpc" -# version : "2.0" -# group: "as" interface : "com.ikurento.user.UserProvider" cluster: "failover" methods : - - name: "GetUser" - retries: 3 + - name: "GetUser" + retries: 3 + "UserProvider1": + registry: "hangzhouzk" + protocol: "jsonrpc" + version : "2.0" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 + "UserProvider2": + registry: "hangzhouzk" + protocol: "jsonrpc" + version : "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 protocol_conf: dubbo: diff --git a/examples/jsonrpc/go-client/profiles/release/client.yml b/examples/jsonrpc/go-client/profiles/release/client.yml index 3b82dd07bd..e521f4c58c 100644 --- a/examples/jsonrpc/go-client/profiles/release/client.yml +++ b/examples/jsonrpc/go-client/profiles/release/client.yml @@ -34,13 +34,30 @@ references: # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 registry: "hangzhouzk" protocol : "jsonrpc" -# version : "2.0" -# group: "as" interface : "com.ikurento.user.UserProvider" cluster: "failover" methods : - name: "GetUser" retries: 3 + "UserProvider1": + registry: "hangzhouzk" + protocol: "jsonrpc" + version : "2.0" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 + "UserProvider2": + registry: "hangzhouzk" + protocol: "jsonrpc" + version : "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 protocol_conf: dubbo: diff --git a/examples/jsonrpc/go-client/profiles/test/client.yml b/examples/jsonrpc/go-client/profiles/test/client.yml index 1b0529c455..086f7b9699 100644 --- a/examples/jsonrpc/go-client/profiles/test/client.yml +++ b/examples/jsonrpc/go-client/profiles/test/client.yml @@ -8,12 +8,12 @@ connect_timeout : "3s" # application config application_config: - organization : "ikurento.com" - name : "BDTService" - module : "dubbogo user-info client" - version : "0.0.1" - owner : "ZX" - environment : "test" + organization : "ikurento.com" + name : "BDTService" + module : "dubbogo user-info client" + version : "0.0.1" + owner : "ZX" + environment : "test" registries : "hangzhouzk": @@ -34,13 +34,30 @@ references: # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 registry: "hangzhouzk" protocol : "jsonrpc" -# version : "2.0" -# group: "as" interface : "com.ikurento.user.UserProvider" cluster: "failover" methods : - - name: "GetUser" - retries: 3 + - name: "GetUser" + retries: 3 + "UserProvider1": + registry: "hangzhouzk" + protocol: "jsonrpc" + version : "2.0" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 + "UserProvider2": + registry: "hangzhouzk" + protocol: "jsonrpc" + version : "2.0" + group: "as" + interface: "com.ikurento.user.UserProvider" + cluster: "failover" + methods: + - name: "GetUser" + retries: 3 protocol_conf: dubbo: diff --git a/examples/jsonrpc/go-server/app/server.go b/examples/jsonrpc/go-server/app/server.go index 8a226e23ad..231082b5b4 100644 --- a/examples/jsonrpc/go-server/app/server.go +++ b/examples/jsonrpc/go-server/app/server.go @@ -64,7 +64,7 @@ func initSignal() { case syscall.SIGHUP: // reload() default: - go time.AfterFunc(time.Duration(float64(survivalTimeout)*float64(time.Second)), func() { + go time.AfterFunc(time.Duration(survivalTimeout), func() { logger.Warnf("app exit now by force...") os.Exit(1) }) diff --git a/examples/jsonrpc/go-server/app/user.go b/examples/jsonrpc/go-server/app/user.go index e86d915417..837661003e 100644 --- a/examples/jsonrpc/go-server/app/user.go +++ b/examples/jsonrpc/go-server/app/user.go @@ -18,26 +18,12 @@ package main import ( - "context" "fmt" - "strconv" "time" ) -import ( - perrors "github.com/pkg/errors" -) - -import ( - "github.com/apache/dubbo-go/config" -) - type Gender int -func init() { - config.SetProviderService(new(UserProvider)) -} - const ( MAN = iota WOMAN @@ -61,10 +47,6 @@ type ( Birth int `json:"time"` Sex string `json:"sex"` } - - UserProvider struct { - user map[string]User - } ) var ( @@ -75,103 +57,20 @@ var ( sex: Gender(MAN), } - userMap = UserProvider{user: make(map[string]User)} + userMap = make(map[string]User) ) func init() { DefaultUser.Sex = DefaultUser.sex.String() - userMap.user["A000"] = DefaultUser - userMap.user["A001"] = User{Id: "001", Name: "ZhangSheng", Age: 18, sex: MAN} - userMap.user["A002"] = User{Id: "002", Name: "Lily", Age: 20, sex: WOMAN} - userMap.user["A003"] = User{Id: "113", Name: "Moorse", Age: 30, sex: MAN} - for k, v := range userMap.user { + userMap["A000"] = DefaultUser + userMap["A001"] = User{Id: "001", Name: "ZhangSheng", Age: 18, sex: MAN} + userMap["A002"] = User{Id: "002", Name: "Lily", Age: 20, sex: WOMAN} + userMap["A003"] = User{Id: "113", Name: "Moorse", Age: 30, sex: MAN} + for k, v := range userMap { v.Birth = int(time.Now().AddDate(-1*v.Age, 0, 0).Unix()) - v.Sex = userMap.user[k].sex.String() - userMap.user[k] = v - } -} - -func (u *UserProvider) getUser(userId string) (*User, error) { - if user, ok := userMap.user[userId]; ok { - return &user, nil - } - - return nil, fmt.Errorf("invalid user id:%s", userId) -} - -func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error { - var ( - err error - user *User - ) - - println("req:%#v", req) - user, err = u.getUser(req[0].(string)) - if err == nil { - *rsp = *user - println("rsp:%#v", rsp) - } - return err -} - -func (u *UserProvider) GetUser0(id string, name string) (User, error) { - var err error - - println("id:%s, name:%s", id, name) - user, err := u.getUser(id) - if err != nil { - return User{}, err - } - if user.Name != name { - return User{}, perrors.New("name is not " + user.Name) + v.Sex = userMap[k].sex.String() + userMap[k] = v } - return *user, err -} - -func (u *UserProvider) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { - var err error - - println("req:%#v", req) - rsp.Id = strconv.FormatFloat(req[0].(float64), 'f', 0, 64) - rsp.Sex = Gender(MAN).String() - return err -} - -func (u *UserProvider) GetUser3() error { - return nil -} - -func (u *UserProvider) GetUsers(req []interface{}) ([]User, error) { - var err error - - println("req:%s", req) - t := req[0].([]interface{}) - user, err := u.getUser(t[0].(string)) - if err != nil { - return nil, err - } - println("user:%v", user) - user1, err := u.getUser(t[1].(string)) - if err != nil { - return nil, err - } - println("user1:%v", user1) - - return []User{*user, *user1}, err -} - -func (s *UserProvider) MethodMapper() map[string]string { - return map[string]string{ - "GetUser2": "getUser", - } -} - -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" } func println(format string, args ...interface{}) { diff --git a/examples/jsonrpc/go-server/app/user_provider.go b/examples/jsonrpc/go-server/app/user_provider.go new file mode 100644 index 0000000000..4af4bb0404 --- /dev/null +++ b/examples/jsonrpc/go-server/app/user_provider.go @@ -0,0 +1,98 @@ +package main + +import ( + "context" + "fmt" + "strconv" +) + +import ( + "github.com/apache/dubbo-go/config" + perrors "github.com/pkg/errors" +) + +func init() { + config.SetProviderService(new(UserProvider)) +} + +type UserProvider struct { +} + +func (u *UserProvider) getUser(userId string) (*User, error) { + if user, ok := userMap[userId]; ok { + return &user, nil + } + + return nil, fmt.Errorf("invalid user id:%s", userId) +} + +func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error { + var ( + err error + user *User + ) + + println("req:%#v", req) + user, err = u.getUser(req[0].(string)) + if err == nil { + *rsp = *user + println("rsp:%#v", rsp) + } + return err +} + +func (u *UserProvider) GetUser0(id string, name string) (User, error) { + var err error + + println("id:%s, name:%s", id, name) + user, err := u.getUser(id) + if err != nil { + return User{}, err + } + if user.Name != name { + return User{}, perrors.New("name is not " + user.Name) + } + return *user, err +} + +func (u *UserProvider) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { + var err error + + println("req:%#v", req) + rsp.Id = strconv.FormatFloat(req[0].(float64), 'f', 0, 64) + rsp.Sex = Gender(MAN).String() + return err +} + +func (u *UserProvider) GetUser3() error { + return nil +} + +func (u *UserProvider) GetUsers(req []interface{}) ([]User, error) { + var err error + + println("req:%s", req) + t := req[0].([]interface{}) + user, err := u.getUser(t[0].(string)) + if err != nil { + return nil, err + } + println("user:%v", user) + user1, err := u.getUser(t[1].(string)) + if err != nil { + return nil, err + } + println("user1:%v", user1) + + return []User{*user, *user1}, err +} + +func (s *UserProvider) MethodMapper() map[string]string { + return map[string]string{ + "GetUser2": "getUser", + } +} + +func (u *UserProvider) Reference() string { + return "UserProvider" +} diff --git a/examples/jsonrpc/go-server/app/user_provider1.go b/examples/jsonrpc/go-server/app/user_provider1.go new file mode 100644 index 0000000000..1557c6b8d2 --- /dev/null +++ b/examples/jsonrpc/go-server/app/user_provider1.go @@ -0,0 +1,83 @@ +package main + +import ( + "context" + "fmt" + "strconv" +) + +import ( + "github.com/apache/dubbo-go/config" + perrors "github.com/pkg/errors" +) + +func init() { + config.SetProviderService(new(UserProvider1)) +} + +type UserProvider1 struct { +} + +func (u *UserProvider1) getUser(userId string) (*User, error) { + if user, ok := userMap[userId]; ok { + return &user, nil + } + + return nil, fmt.Errorf("invalid user id:%s", userId) +} + +func (u *UserProvider1) GetUser(ctx context.Context, req []interface{}, rsp *User) error { + var ( + err error + user *User + ) + + println("req:%#v", req) + user, err = u.getUser(req[0].(string)) + if err == nil { + *rsp = *user + println("rsp:%#v", rsp) + } + return err +} + +func (u *UserProvider1) GetUser0(id string, name string) (User, error) { + var err error + + println("id:%s, name:%s", id, name) + user, err := u.getUser(id) + if err != nil { + return User{}, err + } + if user.Name != name { + return User{}, perrors.New("name is not " + user.Name) + } + return *user, err +} + +func (u *UserProvider1) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { + var err error + + println("req:%#v", req) + rsp.Id = strconv.FormatFloat(req[0].(float64), 'f', 0, 64) + rsp.Sex = Gender(MAN).String() + return err +} + +func (u *UserProvider1) GetUser3() error { + return nil +} + +func (u *UserProvider1) GetUsers(req []interface{}) ([]User, error) { + return []User{}, nil +} + +func (s *UserProvider1) MethodMapper() map[string]string { + return map[string]string{ + "GetUser2": "getUser", + } +} + +func (u *UserProvider1) Reference() string { + return "UserProvider1" +} diff --git a/examples/jsonrpc/go-server/app/user_provider2.go b/examples/jsonrpc/go-server/app/user_provider2.go new file mode 100644 index 0000000000..7d2fb80a99 --- /dev/null +++ b/examples/jsonrpc/go-server/app/user_provider2.go @@ -0,0 +1,93 @@ +package main + +import ( + "context" + "fmt" + "strconv" +) + +import ( + "github.com/apache/dubbo-go/config" + perrors "github.com/pkg/errors" +) + +func init() { + config.SetProviderService(new(UserProvider2)) +} + +type UserProvider2 struct { +} + +func (u *UserProvider2) getUser(userId string) (*User, error) { + if user, ok := userMap[userId]; ok { + return &user, nil + } + + return nil, fmt.Errorf("invalid user id:%s", userId) +} + +func (u *UserProvider2) GetUser(ctx context.Context, req []interface{}, rsp *User) error { + var ( + err error + user *User + ) + + println("req:%#v", req) + user, err = u.getUser(req[0].(string)) + if err == nil { + *rsp = *user + println("rsp:%#v", rsp) + } + return err +} + +func (u *UserProvider2) GetUser0(id string, name string) (User, error) { + var err error + + println("id:%s, name:%s", id, name) + user, err := u.getUser(id) + if err != nil { + return User{}, err + } + if user.Name != name { + return User{}, perrors.New("name is not " + user.Name) + } + return *user, err +} + +func (u *UserProvider2) GetUser2(ctx context.Context, req []interface{}, rsp *User) error { + var err error + + println("req:%#v", req) + rsp.Id = strconv.FormatFloat(req[0].(float64), 'f', 0, 64) + rsp.Sex = Gender(MAN).String() + return err +} + +func (u *UserProvider2) GetUser3() error { + return nil +} + +func (u *UserProvider2) GetUsers(req []interface{}) ([]User, error) { + var err error + + println("req:%s", req) + t := req[0].([]interface{}) + user, err := u.getUser(t[0].(string)) + if err != nil { + return nil, err + } + println("user:%v", user) + + return []User{*user}, err +} + +func (s *UserProvider2) MethodMapper() map[string]string { + return map[string]string{ + "GetUser2": "getUser", + } +} + +func (u *UserProvider2) Reference() string { + return "UserProvider2" +} diff --git a/examples/jsonrpc/go-server/profiles/dev/server.yml b/examples/jsonrpc/go-server/profiles/dev/server.yml index b5384a28a3..4d74d2ef6f 100644 --- a/examples/jsonrpc/go-server/profiles/dev/server.yml +++ b/examples/jsonrpc/go-server/profiles/dev/server.yml @@ -2,12 +2,12 @@ # application config application_config: - organization : "ikurento.com" - name : "BDTService" - module : "dubbogo user-info server" - version : "0.0.1" - owner : "ZX" - environment : "dev" + organization : "ikurento.com" + name : "BDTService" + module : "dubbogo user-info server" + version : "0.0.1" + owner : "ZX" + environment : "dev" registries : "hangzhouzk": @@ -25,26 +25,51 @@ registries : services: - "UserProvider": - # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 - registry: "hangzhouzk" - protocol : "jsonrpc" - # 相当于dubbo.xml中的interface - interface : "com.ikurento.user.UserProvider" - loadbalance: "random" - warmup: "100" - cluster: "failover" - methods: - - name: "GetUser" - retries: 1 - loadbalance: "random" + "UserProvider": + # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 + registry: "hangzhouzk" + protocol : "jsonrpc" + # 相当于dubbo.xml中的interface + interface : "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider1": + registry: "hangzhouzk" + protocol: "jsonrpc" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + version: "2.0" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider2": + registry: "hangzhouzk" + protocol: "jsonrpc" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + version: "2.0" + group: "as" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" protocols: #- name: "dubbo" # ip : "127.0.0.1" # port : 20000 "jsonrpc1": - name: "jsonrpc" - ip: "127.0.0.1" - port: 20001 + name: "jsonrpc" + ip: "127.0.0.1" + port: 20001 diff --git a/examples/jsonrpc/go-server/profiles/release/server.yml b/examples/jsonrpc/go-server/profiles/release/server.yml index 213d31d53e..3f7d2fdfff 100644 --- a/examples/jsonrpc/go-server/profiles/release/server.yml +++ b/examples/jsonrpc/go-server/profiles/release/server.yml @@ -25,19 +25,44 @@ registries : services: - "UserProvider": - # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 - registry: "hangzhouzk" - protocol : "jsonrpc" - # 相当于dubbo.xml中的interface - interface : "com.ikurento.user.UserProvider" - loadbalance: "random" - warmup: "100" - cluster: "failover" - methods: - - name: "GetUser" - retries: 1 - loadbalance: "random" + "UserProvider": + # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 + registry: "hangzhouzk" + protocol : "jsonrpc" + # 相当于dubbo.xml中的interface + interface : "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider1": + registry: "hangzhouzk" + protocol: "jsonrpc" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + version: "2.0" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider2": + registry: "hangzhouzk" + protocol: "jsonrpc" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + version: "2.0" + group: "as" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" protocols: #- name: "dubbo" diff --git a/examples/jsonrpc/go-server/profiles/test/server.yml b/examples/jsonrpc/go-server/profiles/test/server.yml index 9b6dcb0b5c..dd0637e797 100644 --- a/examples/jsonrpc/go-server/profiles/test/server.yml +++ b/examples/jsonrpc/go-server/profiles/test/server.yml @@ -2,12 +2,12 @@ # application config application_config: - organization : "ikurento.com" - name : "BDTService" - module : "dubbogo user-info server" - version : "0.0.1" - owner : "ZX" - environment : "test" + organization : "ikurento.com" + name : "BDTService" + module : "dubbogo user-info server" + version : "0.0.1" + owner : "ZX" + environment : "test" registries : "hangzhouzk": @@ -25,26 +25,51 @@ registries : services: - "UserProvider": - # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 - registry: "hangzhouzk" - protocol : "jsonrpc" - # 相当于dubbo.xml中的interface - interface : "com.ikurento.user.UserProvider" - loadbalance: "random" - warmup: "100" - cluster: "failover" - methods: - - name: "GetUser" - retries: 1 - loadbalance: "random" + "UserProvider": + # 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册 + registry: "hangzhouzk" + protocol : "jsonrpc" + # 相当于dubbo.xml中的interface + interface : "com.ikurento.user.UserProvider" + loadbalance: "random" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider1": + registry: "hangzhouzk" + protocol: "jsonrpc" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + version: "2.0" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" + "UserProvider2": + registry: "hangzhouzk" + protocol: "jsonrpc" + interface: "com.ikurento.user.UserProvider" + loadbalance: "random" + version: "2.0" + group: "as" + warmup: "100" + cluster: "failover" + methods: + - name: "GetUser" + retries: 1 + loadbalance: "random" protocols: #- name: "dubbo" # ip : "127.0.0.1" # port : 20000 "jsonrpc1": - name: "jsonrpc" - ip: "127.0.0.1" - port: 20001 + name: "jsonrpc" + ip: "127.0.0.1" + port: 20001 diff --git a/examples/jsonrpc/java-client/src/main/java/com/ikurento/user/Consumer.java b/examples/jsonrpc/java-client/src/main/java/com/ikurento/user/Consumer.java index ddf899aa10..2c7b27b5e6 100644 --- a/examples/jsonrpc/java-client/src/main/java/com/ikurento/user/Consumer.java +++ b/examples/jsonrpc/java-client/src/main/java/com/ikurento/user/Consumer.java @@ -19,23 +19,32 @@ public class Consumer { //定义一个私有变量 (Spring中要求) private UserProvider userProvider; + private UserProvider userProvider1; + private UserProvider userProvider2; //Spring注入(Spring中要求) public void setUserProvider(UserProvider u) { this.userProvider = u; } + public void setUserProvider1(UserProvider u) { + this.userProvider1 = u; + } + public void setUserProvider2(UserProvider u) { + this.userProvider2 = u; + } - private void benchmarkSayHello() { - for (int i = 0; i < Integer.MAX_VALUE; i ++) { - try { - // String hello = demoService.sayHello("world" + i); - // System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hello); - } catch (Exception e) { - e.printStackTrace(); - } - - // Thread.sleep(2000); - } + //启动consumer的入口函数(在配置文件中指定) + public void start() throws Exception { + System.out.println("\n\ntest"); + testGetUser(); + testGetUsers(); + System.out.println("\n\ntest1"); + testGetUser1(); + testGetUsers1(); + System.out.println("\n\ntest2"); + testGetUser2(); + testGetUsers2(); + Thread.sleep(2000); } private void testGetUser() throws Exception { @@ -53,12 +62,12 @@ private void testGetUser() throws Exception { + ", age:" + user1.getAge() + ", time:" + user1.getTime().toString()); User user2 = userProvider.GetUser0("A003","Moorse"); System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + - " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() - + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); + " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() + + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); User user3 = userProvider.getUser(1); System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + - " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() - + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); + " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() + + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); userProvider.GetUser3(); System.out.println("GetUser3 succ"); @@ -82,16 +91,106 @@ private void testGetUsers() throws Exception { " UserInfo, Id:" + user.getId() + ", name:" + user.getName() + ", sex:" + user.getSex().toString() + ", age:" + user.getAge() + ", time:" + user.getTime().toString()); } - } catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } } - //启动consumer的入口函数(在配置文件中指定) - public void start() throws Exception { - testGetUser(); - testGetUsers(); -// Thread.sleep(120000); -Thread.sleep(2000); + private void testGetUser1() throws Exception { + try { + EchoService echoService = (EchoService)userProvider1; + Object status = echoService.$echo("OK"); + System.out.println("echo: "+status); + } catch (Exception e) { + e.printStackTrace(); + } + try { + User user1 = userProvider1.GetUser("A003"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user1.getId() + ", name:" + user1.getName() + ", sex:" + user1.getSex().toString() + + ", age:" + user1.getAge() + ", time:" + user1.getTime().toString()); + User user2 = userProvider1.GetUser0("A003","Moorse"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() + + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); + User user3 = userProvider1.getUser(1); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() + + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); + + userProvider1.GetUser3(); + System.out.println("GetUser3 succ"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void testGetUsers1() throws Exception { + try { + List userIDList = new ArrayList(); + userIDList.add("A001"); + userIDList.add("A002"); + userIDList.add("A003"); + + List userList = userProvider1.GetUsers(userIDList); + + for (int i = 0; i < userList.size(); i++) { + User user = userList.get(i); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user.getId() + ", name:" + user.getName() + ", sex:" + user.getSex().toString() + + ", age:" + user.getAge() + ", time:" + user.getTime().toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void testGetUser2() throws Exception { + try { + EchoService echoService = (EchoService)userProvider2; + Object status = echoService.$echo("OK"); + System.out.println("echo: "+status); + } catch (Exception e) { + e.printStackTrace(); + } + try { + User user1 = userProvider2.GetUser("A003"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user1.getId() + ", name:" + user1.getName() + ", sex:" + user1.getSex().toString() + + ", age:" + user1.getAge() + ", time:" + user1.getTime().toString()); + User user2 = userProvider2.GetUser0("A003","Moorse"); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user2.getId() + ", name:" + user2.getName() + ", sex:" + user2.getSex().toString() + + ", age:" + user2.getAge() + ", time:" + user2.getTime().toString()); + User user3 = userProvider2.getUser(1); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user3.getId() + ", name:" + user3.getName() + ", sex:" + user3.getSex().toString() + + ", age:" + user3.getAge() + ", time:" + user3.getTime().toString()); + + userProvider2.GetUser3(); + System.out.println("GetUser3 succ"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void testGetUsers2() throws Exception { + try { + List userIDList = new ArrayList(); + userIDList.add("A001"); + userIDList.add("A002"); + userIDList.add("A003"); + + List userList = userProvider2.GetUsers(userIDList); + + for (int i = 0; i < userList.size(); i++) { + User user = userList.get(i); + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + + " UserInfo, Id:" + user.getId() + ", name:" + user.getName() + ", sex:" + user.getSex().toString() + + ", age:" + user.getAge() + ", time:" + user.getTime().toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml b/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml index c01f4ab064..da21e8a359 100644 --- a/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml +++ b/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/dubbo.consumer.xml @@ -37,5 +37,9 @@ + + + + diff --git a/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/service.xml b/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/service.xml index 625a879f97..db9fc3cba5 100644 --- a/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/service.xml +++ b/examples/jsonrpc/java-client/src/main/resources/META-INF/spring/service.xml @@ -24,6 +24,8 @@ + + diff --git a/examples/jsonrpc/with-configcenter-go-client/app/user.go b/examples/jsonrpc/with-configcenter-go-client/app/user.go index ca98b1af0b..fef665bb3d 100644 --- a/examples/jsonrpc/with-configcenter-go-client/app/user.go +++ b/examples/jsonrpc/with-configcenter-go-client/app/user.go @@ -58,10 +58,6 @@ type UserProvider struct { Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider) Reference() string { + return "UserProvider" } diff --git a/examples/jsonrpc/with-configcenter-go-server/app/user.go b/examples/jsonrpc/with-configcenter-go-server/app/user.go index fbe6f3339c..9ab9e58cb4 100644 --- a/examples/jsonrpc/with-configcenter-go-server/app/user.go +++ b/examples/jsonrpc/with-configcenter-go-server/app/user.go @@ -146,12 +146,8 @@ func (u *UserProvider) GetUsers(req []interface{}) ([]User, error) { return []User{*user, *user1}, err } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider) Reference() string { + return "UserProvider" } func println(format string, args ...interface{}) { diff --git a/go.mod b/go.mod index ef4c7b2ca8..0153907683 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/apache/dubbo-go require ( github.com/dubbogo/getty v1.0.7 github.com/dubbogo/gost v1.0.0 - github.com/dubbogo/hessian2 v1.0.2 + github.com/dubbogo/hessian2 v1.0.3-0.20190702113824-3b1b4945581d github.com/magiconair/properties v1.8.1 github.com/pkg/errors v0.8.1 github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec diff --git a/go.sum b/go.sum index 3af6b3eb0d..8a1e2c97fd 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/dubbogo/getty v1.0.7 h1:5Hg+JwXyCKm9Yr4yJkm98ahhnoa8c2h6br5QJxwQ+YU= github.com/dubbogo/getty v1.0.7/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo= github.com/dubbogo/gost v1.0.0 h1:obKvpJYdrIY2BidHYwYoj2E50OtwCDqVVVTcH2nnhAY= github.com/dubbogo/gost v1.0.0/go.mod h1:R7wZm1DrmrKGr50mBZVcg6C9ekG8aL5hP+sgWcIDwQg= -github.com/dubbogo/hessian2 v1.0.2 h1:Ka9Z32ZszGAdCpgrGuZQmwkT0qe1pd3o9r7ERCDnSlQ= -github.com/dubbogo/hessian2 v1.0.2/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM= +github.com/dubbogo/hessian2 v1.0.3-0.20190702113824-3b1b4945581d h1:ovbb5cbkHltqL5CzhrwHEtRRHTFaBUyjPT1SQjqm80E= +github.com/dubbogo/hessian2 v1.0.3-0.20190702113824-3b1b4945581d/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= diff --git a/protocol/dubbo/client.go b/protocol/dubbo/client.go index 56f95090c2..8ba46e7b0b 100644 --- a/protocol/dubbo/client.go +++ b/protocol/dubbo/client.go @@ -217,7 +217,6 @@ func (c *Client) call(ct CallType, addr string, svcUrl common.URL, method string p := &DubboPackage{} p.Service.Path = strings.TrimPrefix(svcUrl.Path, "/") - p.Service.Target = svcUrl.GetParam(constant.INTERFACE_KEY, "") p.Service.Interface = svcUrl.GetParam(constant.INTERFACE_KEY, "") p.Service.Version = svcUrl.GetParam(constant.VERSION_KEY, "") p.Service.Method = method diff --git a/protocol/dubbo/client_test.go b/protocol/dubbo/client_test.go index f4a5f4a847..2f9697dc75 100644 --- a/protocol/dubbo/client_test.go +++ b/protocol/dubbo/client_test.go @@ -63,13 +63,13 @@ func TestClient_Call(t *testing.T) { c.pool = newGettyRPCClientConnPool(c, clientConf.PoolSize, time.Duration(int(time.Second)*clientConf.PoolTTL)) user := &User{} - //err := c.Call("127.0.0.1:20000", url, "GetBigPkg", []interface{}{nil}, user) - //assert.NoError(t, err) - //assert.NotEqual(t, "", user.Id) - //assert.NotEqual(t, "", user.Name) + err := c.Call("127.0.0.1:20000", url, "GetBigPkg", []interface{}{nil}, user) + assert.NoError(t, err) + assert.NotEqual(t, "", user.Id) + assert.NotEqual(t, "", user.Name) user = &User{} - err := c.Call("127.0.0.1:20000", url, "GetUser", []interface{}{"1", "username"}, user) + err = c.Call("127.0.0.1:20000", url, "GetUser", []interface{}{"1", "username"}, user) assert.NoError(t, err) assert.Equal(t, User{Id: "1", Name: "username"}, *user) @@ -195,11 +195,11 @@ func InitTest(t *testing.T) (protocol.Protocol, common.URL) { // Export proto := GetProtocol() - url, err := common.NewURL(context.Background(), "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+ + url, err := common.NewURL(context.Background(), "dubbo://127.0.0.1:20000/UserProvider?anyhost=true&"+ "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+ "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+ - "side=provider&timeout=3000×tamp=1556509797245") + "side=provider&timeout=3000×tamp=1556509797245&bean.name=UserProvider") assert.NoError(t, err) proto.Export(protocol.NewBaseInvoker(url)) @@ -274,12 +274,8 @@ func (u *UserProvider) GetUser6(id int64) (*User, error) { return &User{Id: "1"}, nil } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider) Reference() string { + return "UserProvider" } func (u User) JavaClassName() string { diff --git a/protocol/dubbo/codec_test.go b/protocol/dubbo/codec_test.go index f585666b37..f5b27ea1d7 100644 --- a/protocol/dubbo/codec_test.go +++ b/protocol/dubbo/codec_test.go @@ -20,10 +20,11 @@ package dubbo import ( "testing" "time" +) - "github.com/stretchr/testify/assert" - +import ( hessian "github.com/dubbogo/hessian2" + "github.com/stretchr/testify/assert" ) func TestDubboPackage_MarshalAndUnmarshal(t *testing.T) { @@ -49,7 +50,7 @@ func TestDubboPackage_MarshalAndUnmarshal(t *testing.T) { // request pkg.Header.Type = hessian.PackageRequest pkg.Service.Interface = "Service" - pkg.Service.Target = "Service" + pkg.Service.Path = "path" pkg.Service.Version = "2.6" pkg.Service.Method = "Method" pkg.Service.Timeout = time.Second @@ -64,10 +65,10 @@ func TestDubboPackage_MarshalAndUnmarshal(t *testing.T) { assert.Equal(t, byte(S_Dubbo), pkgres.Header.SerialID) assert.Equal(t, int64(10086), pkgres.Header.ID) assert.Equal(t, "2.5.4", pkgres.Body.([]interface{})[0]) - assert.Equal(t, "Service", pkgres.Body.([]interface{})[1]) + assert.Equal(t, "path", pkgres.Body.([]interface{})[1]) assert.Equal(t, "2.6", pkgres.Body.([]interface{})[2]) assert.Equal(t, "Method", pkgres.Body.([]interface{})[3]) assert.Equal(t, "Ljava/lang/String;", pkgres.Body.([]interface{})[4]) assert.Equal(t, []interface{}{"a"}, pkgres.Body.([]interface{})[5]) - assert.Equal(t, map[interface{}]interface{}{"interface": "Service", "path": "", "timeout": "1000"}, pkgres.Body.([]interface{})[6]) + assert.Equal(t, map[interface{}]interface{}{"group": "", "interface": "Service", "path": "path", "timeout": "1000"}, pkgres.Body.([]interface{})[6]) } diff --git a/protocol/dubbo/dubbo_exporter.go b/protocol/dubbo/dubbo_exporter.go index bdec8a349f..cb06b6b69c 100644 --- a/protocol/dubbo/dubbo_exporter.go +++ b/protocol/dubbo/dubbo_exporter.go @@ -39,9 +39,9 @@ func NewDubboExporter(key string, invoker protocol.Invoker, exporterMap *sync.Ma } func (de *DubboExporter) Unexport() { - service := de.GetInvoker().GetUrl().GetParam(constant.INTERFACE_KEY, "") + serviceId := de.GetInvoker().GetUrl().GetParam(constant.BEAN_NAME_KEY, "") de.BaseExporter.Unexport() - err := common.ServiceMap.UnRegister(DUBBO, service) + err := common.ServiceMap.UnRegister(DUBBO, serviceId) if err != nil { logger.Errorf("[DubboExporter.Unexport] error: %v", err) } diff --git a/protocol/dubbo/listener.go b/protocol/dubbo/listener.go index 55ee929301..120ecf9a4d 100644 --- a/protocol/dubbo/listener.go +++ b/protocol/dubbo/listener.go @@ -208,12 +208,8 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) { twoway = false } - group := p.Body.(map[string]interface{})["attachments"].(map[interface{}]interface{})[constant.GROUP_KEY] - if group == nil { - group = "" - } u := common.NewURLWithOptions(common.WithPath(p.Service.Path), common.WithParams(url.Values{}), - common.WithParamsValue(constant.GROUP_KEY, group.(string)), + common.WithParamsValue(constant.GROUP_KEY, p.Service.Group), common.WithParamsValue(constant.INTERFACE_KEY, p.Service.Interface), common.WithParamsValue(constant.VERSION_KEY, p.Service.Version)) exporter, _ := dubboProtocol.ExporterMap().Load(u.ServiceKey()) @@ -229,7 +225,7 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) { if invoker != nil { result := invoker.Invoke(invocation.NewRPCInvocationForProvider(p.Service.Method, p.Body.(map[string]interface{})["args"].([]interface{}), map[string]string{ constant.PATH_KEY: p.Service.Path, - constant.GROUP_KEY: group.(string), + constant.GROUP_KEY: p.Service.Group, constant.INTERFACE_KEY: p.Service.Interface, constant.VERSION_KEY: p.Service.Version, })) diff --git a/protocol/dubbo/readwriter.go b/protocol/dubbo/readwriter.go index 042b878910..1b4db88e55 100644 --- a/protocol/dubbo/readwriter.go +++ b/protocol/dubbo/readwriter.go @@ -24,7 +24,7 @@ import ( import ( "github.com/dubbogo/getty" - hessian "github.com/dubbogo/hessian2" + "github.com/dubbogo/hessian2" perrors "github.com/pkg/errors" ) import ( @@ -140,11 +140,17 @@ func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface attachments = req[6].(map[interface{}]interface{}) } pkg.Service.Interface = attachments[constant.INTERFACE_KEY].(string) + if pkg.Service.Path == "" && attachments[constant.PATH_KEY] != nil { + pkg.Service.Path = attachments[constant.PATH_KEY].(string) + } + if attachments[constant.GROUP_KEY] != nil { + pkg.Service.Group = attachments[constant.GROUP_KEY].(string) + } pkg.Body = map[string]interface{}{ "dubboVersion": dubboVersion, "argsTypes": argsTypes, "args": args, - "service": common.ServiceMap.GetService(DUBBO, pkg.Service.Interface), + "service": common.ServiceMap.GetService(DUBBO, pkg.Service.Path), // path as a key "attachments": attachments, } } diff --git a/protocol/jsonrpc/http_test.go b/protocol/jsonrpc/http_test.go index f05f47b8c1..1f446803fd 100644 --- a/protocol/jsonrpc/http_test.go +++ b/protocol/jsonrpc/http_test.go @@ -54,11 +54,11 @@ func TestHTTPClient_Call(t *testing.T) { // Export proto := GetProtocol() - url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&"+ + url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20001/UserProvider?anyhost=true&"+ "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+ "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+ - "side=provider&timeout=3000×tamp=1556509797245") + "side=provider&timeout=3000×tamp=1556509797245&bean.name=UserProvider") assert.NoError(t, err) proto.Export(protocol.NewBaseInvoker(url)) time.Sleep(time.Second * 2) @@ -185,10 +185,6 @@ func (u *UserProvider) GetUser4(id float64) (*User, error) { return &User{Id: "1"}, nil } -func (u *UserProvider) Service() string { - return "com.ikurento.user.UserProvider" -} - -func (u *UserProvider) Version() string { - return "" +func (u *UserProvider) Reference() string { + return "UserProvider" } diff --git a/protocol/jsonrpc/jsonrpc_exporter.go b/protocol/jsonrpc/jsonrpc_exporter.go index 21a2465cdd..6720330494 100644 --- a/protocol/jsonrpc/jsonrpc_exporter.go +++ b/protocol/jsonrpc/jsonrpc_exporter.go @@ -39,9 +39,9 @@ func NewJsonrpcExporter(key string, invoker protocol.Invoker, exporterMap *sync. } func (je *JsonrpcExporter) Unexport() { - service := je.GetInvoker().GetUrl().GetParam(constant.INTERFACE_KEY, "") + serviceId := je.GetInvoker().GetUrl().GetParam(constant.BEAN_NAME_KEY, "") je.BaseExporter.Unexport() - err := common.ServiceMap.UnRegister(JSONRPC, service) + err := common.ServiceMap.UnRegister(JSONRPC, serviceId) if err != nil { logger.Errorf("[JsonrpcExporter.Unexport] error: %v", err) } diff --git a/protocol/jsonrpc/jsonrpc_invoker_test.go b/protocol/jsonrpc/jsonrpc_invoker_test.go index 0dd427eb69..1a7f61b64e 100644 --- a/protocol/jsonrpc/jsonrpc_invoker_test.go +++ b/protocol/jsonrpc/jsonrpc_invoker_test.go @@ -41,11 +41,11 @@ func TestJsonrpcInvoker_Invoke(t *testing.T) { // Export proto := GetProtocol() - url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20001/com.ikurento.user.UserProvider?anyhost=true&"+ + url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20001/UserProvider?anyhost=true&"+ "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+ "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+ - "side=provider&timeout=3000×tamp=1556509797245") + "side=provider&timeout=3000×tamp=1556509797245&bean.name=UserProvider") assert.NoError(t, err) proto.Export(protocol.NewBaseInvoker(url)) time.Sleep(time.Second * 2) diff --git a/protocol/jsonrpc/server.go b/protocol/jsonrpc/server.go index 22dc7cfc49..a88896ae98 100644 --- a/protocol/jsonrpc/server.go +++ b/protocol/jsonrpc/server.go @@ -348,15 +348,14 @@ func serveRequest(ctx context.Context, } } } - serviceName := invoker.GetUrl().Service() // get method - svc := common.ServiceMap.GetService(JSONRPC, serviceName) + svc := common.ServiceMap.GetService(JSONRPC, path) if svc == nil { - return perrors.New("cannot find svc " + serviceName) + return perrors.New("cannot find svc " + path) } method := svc.Method()[methodName] if method == nil { - return perrors.New("cannot find method " + methodName + " of svc " + serviceName) + return perrors.New("cannot find method " + methodName + " of svc " + path) } in := []reflect.Value{svc.Rcvr()} From e894ce172e973e95b8e32d8354841909ca1274d7 Mon Sep 17 00:00:00 2001 From: "vito.he" Date: Tue, 2 Jul 2019 20:02:59 +0800 Subject: [PATCH 2/5] Ftr: allow user set custom params to register to registry --- config/base_config.go | 25 +++++++++++++---------- config/config_loader_test.go | 1 + config/reference_config.go | 31 +++++++++++++++++------------ config/reference_config_test.go | 4 ++++ config/service_config.go | 27 +++++++++++++++---------- config/testdata/consumer_config.yml | 3 +++ registry/zookeeper/registry_test.go | 4 ++-- 7 files changed, 58 insertions(+), 37 deletions(-) diff --git a/config/base_config.go b/config/base_config.go index 19acea2fd6..5cb57ffb6d 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -107,7 +107,8 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC setBaseValue := func(f reflect.Value) { ok, value := config.GetProperty(getKeyPrefix(val, id) + key) if ok { - if f.Kind() == reflect.Int64 { + switch f.Kind() { + case reflect.Int64: x, err := strconv.Atoi(value) if err != nil { logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}", @@ -120,21 +121,16 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the int64 value {%v} from config center is overflow", int64(x))) } } - - } - - if f.Kind() == reflect.String { + case reflect.String: f.SetString(value) - } - if f.Kind() == reflect.Bool { + case reflect.Bool: x, err := strconv.ParseBool(value) if err != nil { logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}", val.Type().Name(), val.Type().Field(i).Name, err) } f.SetBool(x) - } - if f.Kind() == reflect.Float64 { + case reflect.Float64: x, err := strconv.ParseFloat(value, 64) if err != nil { logger.Errorf("Dynamic change the configuration in struct {%v} field {%v} error ,error message is {%v}", @@ -147,7 +143,10 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC val.Type().Name(), val.Type().Field(i).Name, perrors.Errorf("the float64 value {%v} from config center is overflow", x)) } } + default: + logger.Warnf("The kind of field {%v} is not supported ", f.Kind().String()) } + } } @@ -191,14 +190,18 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC for _, k := range f.MapKeys() { v := f.MapIndex(k) - if v.Kind() == reflect.Ptr { + switch v.Kind() { + case reflect.Ptr: if v.Elem().Kind() == reflect.Struct { setFieldValue(v.Elem(), k, config) } else { setBaseValue(v.Elem()) } + case reflect.Int64, reflect.String, reflect.Bool, reflect.Float64: + setBaseValue(v) + default: + logger.Warnf("The kind of field {%v} is not supported ", v.Kind().String()) } - } } diff --git a/config/config_loader_test.go b/config/config_loader_test.go index 6e9689c763..cbdc397c28 100644 --- a/config/config_loader_test.go +++ b/config/config_loader_test.go @@ -54,6 +54,7 @@ func TestConfigLoader(t *testing.T) { assert.NotEqual(t, ConsumerConfig{}, GetConsumerConfig()) assert.NotNil(t, providerConfig) assert.NotEqual(t, ProviderConfig{}, GetProviderConfig()) + assert.Equal(t, "soa.com.ikurento.user.UserProvider", GetConsumerConfig().References["UserProvider"].Params["serviceid"]) } func TestLoad(t *testing.T) { diff --git a/config/reference_config.go b/config/reference_config.go index 2c38d8aa4a..835d17f054 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -38,19 +38,20 @@ import ( type ReferenceConfig struct { context context.Context pxy *proxy.Proxy - InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` - Check *bool `yaml:"check" json:"check,omitempty" property:"check"` - Url string `yaml:"url" json:"url,omitempty" property:"url"` - Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` - Protocol string `yaml:"protocol" json:"protocol,omitempty" property:"protocol"` - Registry string `yaml:"registry" json:"registry,omitempty" property:"registry"` - Cluster string `yaml:"cluster" json:"cluster,omitempty" property:"cluster"` - Loadbalance string `yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"` - Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"` - Group string `yaml:"group" json:"group,omitempty" property:"group"` - Version string `yaml:"version" json:"version,omitempty" property:"version"` - Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"` - async bool `yaml:"async" json:"async,omitempty" property:"async"` + InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` + Check *bool `yaml:"check" json:"check,omitempty" property:"check"` + Url string `yaml:"url" json:"url,omitempty" property:"url"` + Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` + Protocol string `yaml:"protocol" json:"protocol,omitempty" property:"protocol"` + Registry string `yaml:"registry" json:"registry,omitempty" property:"registry"` + Cluster string `yaml:"cluster" json:"cluster,omitempty" property:"cluster"` + Loadbalance string `yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"` + Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"` + Group string `yaml:"group" json:"group,omitempty" property:"group"` + Version string `yaml:"version" json:"version,omitempty" property:"version"` + Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"` + async bool `yaml:"async" json:"async,omitempty" property:"async"` + Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` invoker protocol.Invoker urls []*common.URL } @@ -143,6 +144,10 @@ func (refconfig *ReferenceConfig) GetRPCService() common.RPCService { func (refconfig *ReferenceConfig) getUrlMap() url.Values { urlMap := url.Values{} + //first set user params + for k, v := range refconfig.Params { + urlMap.Set(k, v) + } urlMap.Set(constant.INTERFACE_KEY, refconfig.InterfaceName) urlMap.Set(constant.TIMESTAMP_KEY, strconv.FormatInt(time.Now().Unix(), 10)) urlMap.Set(constant.CLUSTER_KEY, refconfig.Cluster) diff --git a/config/reference_config_test.go b/config/reference_config_test.go index c41e2a16de..1a856872a6 100644 --- a/config/reference_config_test.go +++ b/config/reference_config_test.go @@ -80,6 +80,9 @@ func doInit() { }, References: map[string]*ReferenceConfig{ "MockService": { + Params: map[string]string{ + "serviceid": "soa.mock", + }, Registry: "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2", InterfaceName: "MockService", Protocol: "mock", @@ -125,6 +128,7 @@ func Test_Refer(t *testing.T) { for _, reference := range consumerConfig.References { reference.Refer() + assert.Equal(t, "soa.mock", reference.Params["serviceid"]) assert.NotNil(t, reference.invoker) assert.NotNil(t, reference.pxy) } diff --git a/config/service_config.go b/config/service_config.go index 79a29aa330..1b78c2ef3d 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -43,17 +43,18 @@ import ( type ServiceConfig struct { context context.Context - Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` - Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` //multi protocol support, split by ',' - InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` - Registry string `yaml:"registry" json:"registry,omitempty" property:"registry"` - Cluster string `default:"failover" yaml:"cluster" json:"cluster,omitempty" property:"cluster"` - Loadbalance string `default:"random" yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"` - Group string `yaml:"group" json:"group,omitempty" property:"group"` - Version string `yaml:"version" json:"version,omitempty" property:"version" ` - Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"` - Warmup string `yaml:"warmup" json:"warmup,omitempty" property:"warmup"` - Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"` + Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` + Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` //multi protocol support, split by ',' + InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` + Registry string `yaml:"registry" json:"registry,omitempty" property:"registry"` + Cluster string `default:"failover" yaml:"cluster" json:"cluster,omitempty" property:"cluster"` + Loadbalance string `default:"random" yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"` + Group string `yaml:"group" json:"group,omitempty" property:"group"` + Version string `yaml:"version" json:"version,omitempty" property:"version" ` + Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"` + Warmup string `yaml:"warmup" json:"warmup,omitempty" property:"warmup"` + Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"` + Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` unexported *atomic.Bool exported *atomic.Bool rpcService common.RPCService @@ -148,6 +149,10 @@ func (srvconfig *ServiceConfig) Implement(s common.RPCService) { func (srvconfig *ServiceConfig) getUrlMap() url.Values { urlMap := url.Values{} + //first set user params + for k, v := range srvconfig.Params { + urlMap.Set(k, v) + } urlMap.Set(constant.INTERFACE_KEY, srvconfig.InterfaceName) urlMap.Set(constant.TIMESTAMP_KEY, strconv.FormatInt(time.Now().Unix(), 10)) urlMap.Set(constant.CLUSTER_KEY, srvconfig.Cluster) diff --git a/config/testdata/consumer_config.yml b/config/testdata/consumer_config.yml index 08ff59f6fc..68398623b6 100644 --- a/config/testdata/consumer_config.yml +++ b/config/testdata/consumer_config.yml @@ -42,6 +42,9 @@ references: methods : - name: "GetUser" retries: 3 + params: + "serviceid": + "soa.com.ikurento.user.UserProvider" protocol_conf: dubbo: diff --git a/registry/zookeeper/registry_test.go b/registry/zookeeper/registry_test.go index ba2755fa99..168246e157 100644 --- a/registry/zookeeper/registry_test.go +++ b/registry/zookeeper/registry_test.go @@ -36,13 +36,13 @@ import ( func Test_Register(t *testing.T) { regurl, _ := common.NewURL(context.TODO(), "registry://127.0.0.1:1111", common.WithParamsValue(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER))) - url, _ := common.NewURL(context.TODO(), "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParamsValue(constant.CLUSTER_KEY, "mock"), common.WithMethods([]string{"GetUser", "AddUser"})) + url, _ := common.NewURL(context.TODO(), "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParamsValue(constant.CLUSTER_KEY, "mock"), common.WithParamsValue("serviceid", "soa.mock"), common.WithMethods([]string{"GetUser", "AddUser"})) ts, reg, err := newMockZkRegistry(®url) defer ts.Stop() err = reg.Register(url) children, _ := reg.client.GetChildren("/dubbo/com.ikurento.user.UserProvider/providers") - assert.Regexp(t, ".*dubbo%3A%2F%2F127.0.0.1%3A20000%2Fcom.ikurento.user.UserProvider%3Fanyhost%3Dtrue%26category%3Dproviders%26cluster%3Dmock%26dubbo%3Ddubbo-provider-golang-2.6.0%26.*provider", children) + assert.Regexp(t, ".*dubbo%3A%2F%2F127.0.0.1%3A20000%2Fcom.ikurento.user.UserProvider%3Fanyhost%3Dtrue%26category%3Dproviders%26cluster%3Dmock%26dubbo%3Ddubbo-provider-golang-2.6.0%26.*.serviceid%3Dsoa.mock%26.*provider", children) assert.NoError(t, err) } From e89f362407605714c2249b9aca50f528e97fbfb5 Mon Sep 17 00:00:00 2001 From: "vito.he" Date: Tue, 2 Jul 2019 20:15:06 +0800 Subject: [PATCH 3/5] Fix:base_config.go setFieldValue bug --- config/base_config.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/config/base_config.go b/config/base_config.go index 5cb57ffb6d..54ad8aba36 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -179,13 +179,16 @@ func setFieldValue(val reflect.Value, id reflect.Value, config *config.InmemoryC } if f.Kind() == reflect.Map { - //initiate config - s := reflect.New(f.Type().Elem().Elem()) - prefix := s.MethodByName("Prefix").Call(nil)[0].String() - m := config.GetSubProperty(prefix) - for k := range m { - f.SetMapIndex(reflect.ValueOf(k), reflect.New(f.Type().Elem().Elem())) + if f.Type().Elem().Kind() == reflect.Ptr { + //initiate config + s := reflect.New(f.Type().Elem().Elem()) + prefix := s.MethodByName("Prefix").Call(nil)[0].String() + m := config.GetSubProperty(prefix) + for k := range m { + f.SetMapIndex(reflect.ValueOf(k), reflect.New(f.Type().Elem().Elem())) + } } + //iter := f.MapRange() for _, k := range f.MapKeys() { From d7e948e039da3a8f0224da7c38763d9dc2ca8183 Mon Sep 17 00:00:00 2001 From: "vito.he" Date: Tue, 2 Jul 2019 21:26:03 +0800 Subject: [PATCH 4/5] Fix: unit test TestListener in listener_test.go --- remoting/zookeeper/listener_test.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/remoting/zookeeper/listener_test.go b/remoting/zookeeper/listener_test.go index b1f0d43d34..10cc6dcdd6 100644 --- a/remoting/zookeeper/listener_test.go +++ b/remoting/zookeeper/listener_test.go @@ -19,6 +19,7 @@ package zookeeper import ( "fmt" + "sync" "testing" "time" ) @@ -86,32 +87,36 @@ func TestListener(t *testing.T) { dubbo.service.com.ikurento.user.UserProvider.warmup=100 dubbo.service.com.ikurento.user.UserProvider.cluster=failover ` - + var wait sync.WaitGroup ts, client, event := initZkData(t) defer ts.Stop() client.Wait.Add(1) + wait.Add(1) go client.HandleZkEvent(event) listener := NewZkEventListener(client) - dataListener := &mockDataListener{client: client, changedData: changedData} + dataListener := &mockDataListener{client: client, changedData: changedData, wait: &wait} listener.ListenServiceEvent("/dubbo", dataListener) _, err := client.Conn.Set("/dubbo/dubbo.properties", []byte(changedData), 1) assert.NoError(t, err) - client.Wait.Wait() + wait.Wait() assert.Equal(t, changedData, dataListener.eventList[1].Content) + client.Close() + } type mockDataListener struct { eventList []remoting.Event client *ZookeeperClient changedData string + wait *sync.WaitGroup } func (m *mockDataListener) DataChange(eventType remoting.Event) bool { fmt.Println(eventType) m.eventList = append(m.eventList, eventType) if eventType.Content == m.changedData { - m.client.Wait.Done() + m.wait.Done() } return true } From 5ba44f9d32c22dd6f43067f760c717845598431e Mon Sep 17 00:00:00 2001 From: fangyincheng Date: Wed, 3 Jul 2019 16:50:56 +0800 Subject: [PATCH 5/5] Mod:modify rpcinvocation.go --- cluster/cluster_impl/failover_cluster_test.go | 3 +- cluster/loadbalance/least_active_test.go | 3 +- cluster/loadbalance/random_test.go | 6 +- common/proxy/proxy.go | 4 +- filter/impl/echo_filter_test.go | 4 +- protocol/dubbo/dubbo_invoker_test.go | 3 +- protocol/dubbo/listener.go | 2 +- protocol/invocation/rpcinvocation.go | 96 ++++++++++--------- protocol/jsonrpc/jsonrpc_invoker_test.go | 4 +- protocol/jsonrpc/server.go | 2 +- 10 files changed, 65 insertions(+), 62 deletions(-) diff --git a/cluster/cluster_impl/failover_cluster_test.go b/cluster/cluster_impl/failover_cluster_test.go index cc533ea098..dc039db8de 100644 --- a/cluster/cluster_impl/failover_cluster_test.go +++ b/cluster/cluster_impl/failover_cluster_test.go @@ -143,8 +143,7 @@ func Test_FailoverInvoke2(t *testing.T) { urlParams.Set(constant.RETRIES_KEY, "2") urlParams.Set("methods.test."+constant.RETRIES_KEY, "3") - ivc := &invocation.RPCInvocation{} - ivc.SetMethod("test") + ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test")) result := normalInvoke(t, 3, urlParams, ivc) assert.NoError(t, result.Error()) count = 0 diff --git a/cluster/loadbalance/least_active_test.go b/cluster/loadbalance/least_active_test.go index c29a2092a1..7663ea3ce6 100644 --- a/cluster/loadbalance/least_active_test.go +++ b/cluster/loadbalance/least_active_test.go @@ -43,8 +43,7 @@ func TestLeastActiveByWeight(t *testing.T) { invokers = append(invokers, protocol.NewBaseInvoker(url)) } - inv := new(invocation.RPCInvocation) - inv.SetMethod("test") + inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test")) protocol.BeginCount(invokers[2].GetUrl(), inv.MethodName()) loop = 10000 diff --git a/cluster/loadbalance/random_test.go b/cluster/loadbalance/random_test.go index 09d3d259a9..ffe65d78ac 100644 --- a/cluster/loadbalance/random_test.go +++ b/cluster/loadbalance/random_test.go @@ -67,8 +67,7 @@ func Test_RandomlbSelectWeight(t *testing.T) { urlParams.Set("methods.test."+constant.WEIGHT_KEY, "10000000000000") urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams)) invokers = append(invokers, protocol.NewBaseInvoker(urll)) - ivc := &invocation.RPCInvocation{} - ivc.SetMethod("test") + ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test")) var selectedInvoker []protocol.Invoker var selected float64 @@ -99,8 +98,7 @@ func Test_RandomlbSelectWarmup(t *testing.T) { urlParams.Set(constant.REMOTE_TIMESTAMP_KEY, strconv.FormatInt(time.Now().Add(time.Minute*(-9)).Unix(), 10)) urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams)) invokers = append(invokers, protocol.NewBaseInvoker(urll)) - ivc := &invocation.RPCInvocation{} - ivc.SetMethod("test") + ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test")) var selectedInvoker []protocol.Invoker var selected float64 diff --git a/common/proxy/proxy.go b/common/proxy/proxy.go index 96d42eb211..1c079f6bca 100644 --- a/common/proxy/proxy.go +++ b/common/proxy/proxy.go @@ -116,7 +116,9 @@ func (p *Proxy) Implement(v common.RPCService) { } } - inv = invocation_impl.NewRPCInvocationForConsumer(methodName, nil, inArr, reply.Interface(), p.callBack, common.URL{}, nil) + inv = invocation_impl.NewRPCInvocationWithOptions(invocation_impl.WithMethodName(methodName), + invocation_impl.WithArguments(inArr), invocation_impl.WithReply(reply.Interface()), + invocation_impl.WithCallBack(p.callBack)) for k, value := range p.attachments { inv.SetAttachments(k, value) diff --git a/filter/impl/echo_filter_test.go b/filter/impl/echo_filter_test.go index e2752c85b2..e2e5929747 100644 --- a/filter/impl/echo_filter_test.go +++ b/filter/impl/echo_filter_test.go @@ -34,11 +34,11 @@ import ( func TestEchoFilter_Invoke(t *testing.T) { filter := GetFilter() result := filter.Invoke(protocol.NewBaseInvoker(common.URL{}), - invocation.NewRPCInvocationForProvider("$echo", []interface{}{"OK"}, nil)) + invocation.NewRPCInvocation("$echo", []interface{}{"OK"}, nil)) assert.Equal(t, "OK", result.Result()) result = filter.Invoke(protocol.NewBaseInvoker(common.URL{}), - invocation.NewRPCInvocationForProvider("MethodName", []interface{}{"OK"}, nil)) + invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, nil)) assert.Nil(t, result.Error()) assert.Nil(t, result.Result()) } diff --git a/protocol/dubbo/dubbo_invoker_test.go b/protocol/dubbo/dubbo_invoker_test.go index 182d6d8e0b..e88e782730 100644 --- a/protocol/dubbo/dubbo_invoker_test.go +++ b/protocol/dubbo/dubbo_invoker_test.go @@ -44,7 +44,8 @@ func TestDubboInvoker_Invoke(t *testing.T) { invoker := NewDubboInvoker(url, c) user := &User{} - inv := invocation.NewRPCInvocationForConsumer("GetUser", nil, []interface{}{"1", "username"}, user, nil, url, nil) + inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"), invocation.WithArguments([]interface{}{"1", "username"}), + invocation.WithReply(user)) // Call res := invoker.Invoke(inv) diff --git a/protocol/dubbo/listener.go b/protocol/dubbo/listener.go index 120ecf9a4d..1333fd3dc6 100644 --- a/protocol/dubbo/listener.go +++ b/protocol/dubbo/listener.go @@ -223,7 +223,7 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) { } invoker := exporter.(protocol.Exporter).GetInvoker() if invoker != nil { - result := invoker.Invoke(invocation.NewRPCInvocationForProvider(p.Service.Method, p.Body.(map[string]interface{})["args"].([]interface{}), map[string]string{ + result := invoker.Invoke(invocation.NewRPCInvocation(p.Service.Method, p.Body.(map[string]interface{})["args"].([]interface{}), map[string]string{ constant.PATH_KEY: p.Service.Path, constant.GROUP_KEY: p.Service.Group, constant.INTERFACE_KEY: p.Service.Interface, diff --git a/protocol/invocation/rpcinvocation.go b/protocol/invocation/rpcinvocation.go index d515cc4c8a..2124a22f16 100644 --- a/protocol/invocation/rpcinvocation.go +++ b/protocol/invocation/rpcinvocation.go @@ -22,8 +22,6 @@ import ( ) import ( - "github.com/apache/dubbo-go/common" - "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/protocol" ) @@ -41,27 +39,7 @@ type RPCInvocation struct { invoker protocol.Invoker } -func NewRPCInvocationForConsumer(methodName string, parameterTypes []reflect.Type, arguments []interface{}, - reply interface{}, callBack interface{}, url common.URL, invoker protocol.Invoker) *RPCInvocation { - - attachments := map[string]string{} - attachments[constant.PATH_KEY] = url.Path - attachments[constant.GROUP_KEY] = url.GetParam(constant.GROUP_KEY, "") - attachments[constant.INTERFACE_KEY] = url.GetParam(constant.INTERFACE_KEY, "") - attachments[constant.VERSION_KEY] = url.GetParam(constant.VERSION_KEY, "") - - return &RPCInvocation{ - methodName: methodName, - parameterTypes: parameterTypes, - arguments: arguments, - reply: reply, - callBack: callBack, - attachments: attachments, - invoker: invoker, - } -} - -func NewRPCInvocationForProvider(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation { +func NewRPCInvocation(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation { return &RPCInvocation{ methodName: methodName, arguments: arguments, @@ -69,26 +47,6 @@ func NewRPCInvocationForProvider(methodName string, arguments []interface{}, att } } -type option func(invo *RPCInvocation) - -func WithMethodName(methodName string) option { - return func(invo *RPCInvocation) { - invo.methodName = methodName - } -} - -func WithParameterTypes(parameterTypes []reflect.Type) option { - return func(invo *RPCInvocation) { - invo.parameterTypes = parameterTypes - } -} - -func WithArguments(arguments []interface{}) option { - return func(invo *RPCInvocation) { - invo.arguments = arguments - } -} - func NewRPCInvocationWithOptions(opts ...option) *RPCInvocation { invo := &RPCInvocation{} for _, opt := range opts { @@ -147,14 +105,58 @@ func (r *RPCInvocation) SetInvoker() protocol.Invoker { return r.invoker } +func (r *RPCInvocation) CallBack() interface{} { + return r.callBack +} + func (r *RPCInvocation) SetCallBack(c interface{}) { r.callBack = c } -func (r *RPCInvocation) CallBack() interface{} { - return r.callBack +/////////////////////////// +// option +/////////////////////////// + +type option func(invo *RPCInvocation) + +func WithMethodName(methodName string) option { + return func(invo *RPCInvocation) { + invo.methodName = methodName + } +} + +func WithParameterTypes(parameterTypes []reflect.Type) option { + return func(invo *RPCInvocation) { + invo.parameterTypes = parameterTypes + } +} + +func WithArguments(arguments []interface{}) option { + return func(invo *RPCInvocation) { + invo.arguments = arguments + } +} + +func WithReply(reply interface{}) option { + return func(invo *RPCInvocation) { + invo.reply = reply + } +} + +func WithCallBack(callBack interface{}) option { + return func(invo *RPCInvocation) { + invo.callBack = callBack + } +} + +func WithAttachments(attachments map[string]string) option { + return func(invo *RPCInvocation) { + invo.attachments = attachments + } } -func (r *RPCInvocation) SetMethod(method string) { - r.methodName = method +func WithInvoker(invoker protocol.Invoker) option { + return func(invo *RPCInvocation) { + invo.invoker = invoker + } } diff --git a/protocol/jsonrpc/jsonrpc_invoker_test.go b/protocol/jsonrpc/jsonrpc_invoker_test.go index 1a7f61b64e..bc88759bf5 100644 --- a/protocol/jsonrpc/jsonrpc_invoker_test.go +++ b/protocol/jsonrpc/jsonrpc_invoker_test.go @@ -57,7 +57,9 @@ func TestJsonrpcInvoker_Invoke(t *testing.T) { jsonInvoker := NewJsonrpcInvoker(url, client) user := &User{} - res := jsonInvoker.Invoke(invocation.NewRPCInvocationForConsumer("GetUser", nil, []interface{}{"1", "username"}, user, nil, url, nil)) + res := jsonInvoker.Invoke(invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"), invocation.WithArguments([]interface{}{"1", "username"}), + invocation.WithReply(user))) + assert.NoError(t, res.Error()) assert.Equal(t, User{Id: "1", Name: "username"}, *res.Result().(*User)) diff --git a/protocol/jsonrpc/server.go b/protocol/jsonrpc/server.go index a88896ae98..6b3a39c68b 100644 --- a/protocol/jsonrpc/server.go +++ b/protocol/jsonrpc/server.go @@ -325,7 +325,7 @@ func serveRequest(ctx context.Context, exporter, _ := jsonrpcProtocol.ExporterMap().Load(path) invoker := exporter.(*JsonrpcExporter).GetInvoker() if invoker != nil { - result := invoker.Invoke(invocation.NewRPCInvocationForProvider(methodName, args, map[string]string{ + result := invoker.Invoke(invocation.NewRPCInvocation(methodName, args, map[string]string{ constant.PATH_KEY: path, constant.VERSION_KEY: codec.req.Version, }))