Skip to content
Snippets Groups Projects
Select Git revision
  • a86337d366faf130a14a7954761b099785fe6e70
  • master default protected
  • 3.0
  • develop
  • revert-2069-tripleVersion
  • 3.1
  • rest-protocol
  • feat/remoting_rocketmq
  • dapr-support
  • 1.5
  • 1.4
  • 1.3
  • 1.2
  • 1.1
  • v3.0.3-rc2
  • v3.0.3-rc1
  • v3.0.2
  • v1.5.8
  • v1.5.9-rc1
  • v3.0.1
  • v1.5.8-rc1
  • v3.0.0
  • v3.0.0-rc4-1
  • v3.0.0-rc4
  • v3.0.0-rc3
  • v1.5.7
  • v1.5.7-rc2
  • v3.0.0-rc2
  • remove
  • v1.5.7-rc1
  • v3.0.0-rc1
  • v1.5.7-rc1-tmp
  • 1.5.6
  • v1.5.6
34 results

test.go

Blame
  • user avatar
    AlexStocks authored
    a86337d3
    History
    test.go 4.78 KiB
    package main
    
    import (
    	"context"
    	"fmt"
    	_ "net/http/pprof"
    )
    
    import (
    	// "github.com/AlexStocks/goext/log"
    	log "github.com/AlexStocks/log4go"
    	jerrors "github.com/juju/errors"
    )
    
    import (
    	"github.com/dubbo/dubbo-go/jsonrpc"
    	"github.com/dubbo/dubbo-go/public"
    	"github.com/dubbo/dubbo-go/registry"
    )
    
    func testJsonrpc(userKey string) {
    	var (
    		err        error
    		service    string
    		method     string
    		serviceIdx int
    		user       *JsonRPCUser
    		ctx        context.Context
    		conf       registry.ServiceConfig
    		req        jsonrpc.Request
    		serviceURL *registry.ServiceURL
    		clt        *jsonrpc.HTTPClient
    	)
    
    	clt = jsonrpc.NewHTTPClient(
    		&jsonrpc.HTTPOptions{
    			HandshakeTimeout: clientConfig.connectTimeout,
    			HTTPTimeout:      clientConfig.requestTimeout,
    		},
    	)
    
    	serviceIdx = -1
    	service = "com.ikurento.user.UserProvider"
    	for i := range clientConfig.Service_List {
    		if clientConfig.Service_List[i].Service == service && clientConfig.Service_List[i].Protocol == public.CODECTYPE_JSONRPC.String() {
    			serviceIdx = i
    			break
    		}
    	}
    	if serviceIdx == -1 {
    		panic(fmt.Sprintf("can not find service in config service list:%#v", clientConfig.Service_List))
    	}
    
    	// Create request
    	method = string("GetUser")
    	// gxlog.CInfo("jsonrpc selected service %#v", clientConfig.Service_List[serviceIdx])
    	conf = registry.ServiceConfig{
    		Group:    clientConfig.Service_List[serviceIdx].Group,
    		Protocol: public.CodecType(public.CODECTYPE_JSONRPC).String(),
    		Version:  clientConfig.Service_List[serviceIdx].Version,
    		Service:  clientConfig.Service_List[serviceIdx].Service,
    	}
    	// Attention the last parameter : []UserKey{userKey}
    	req = clt.NewRequest(conf, method, []string{userKey})
    
    	serviceURL, err = clientRegistry.Filter(req.ServiceConfig(), 1)
    	if err != nil {
    		log.Error("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
    		// gxlog.CError("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
    		return
    	}
    	log.Debug("got serviceURL: %s", serviceURL)
    	// Set arbitrary headers in context
    	ctx = context.WithValue(context.Background(), public.DUBBOGO_CTX_KEY, map[string]string{
    		"X-Proxy-Id": "dubbogo",
    		"X-Services": service,
    		"X-Method":   method,
    	})
    
    	user = new(JsonRPCUser)
    	// Call service
    	if err = clt.Call(ctx, *serviceURL, req, user); err != nil {
    		log.Error("client.Call() return error:%+v", jerrors.ErrorStack(err))
    		// gxlog.CError("client.Call() return error:%+v", jerrors.ErrorStack(err))
    		return
    	}
    
    	log.Info("response result:%s", user)
    	// gxlog.CInfo("response result:%s", user)
    }
    
    func testJsonrpcIllegalMethod(userKey string) {
    	var (
    		err        error
    		service    string
    		method     string
    		serviceIdx int
    		user       *JsonRPCUser
    		ctx        context.Context
    		conf       registry.ServiceConfig
    		req        jsonrpc.Request
    		serviceURL *registry.ServiceURL
    		clt        *jsonrpc.HTTPClient
    	)
    
    	clt = jsonrpc.NewHTTPClient(
    		&jsonrpc.HTTPOptions{
    			HandshakeTimeout: clientConfig.connectTimeout,
    			HTTPTimeout:      clientConfig.requestTimeout,
    		},
    	)
    
    	serviceIdx = -1
    	service = "com.ikurento.user.UserProvider"
    	for i := range clientConfig.Service_List {
    		if clientConfig.Service_List[i].Service == service && clientConfig.Service_List[i].Protocol == public.CODECTYPE_JSONRPC.String() {
    			serviceIdx = i
    			break
    		}
    	}
    	if serviceIdx == -1 {
    		panic(fmt.Sprintf("can not find service in config service list:%#v", clientConfig.Service_List))
    	}
    
    	// Create request
    	method = string("GetUser1")
    	// gxlog.CInfo("jsonrpc selected service %#v", clientConfig.Service_List[serviceIdx])
    	conf = registry.ServiceConfig{
    		Group:    clientConfig.Service_List[serviceIdx].Group,
    		Protocol: public.CodecType(public.CODECTYPE_JSONRPC).String(),
    		Version:  clientConfig.Service_List[serviceIdx].Version,
    		Service:  clientConfig.Service_List[serviceIdx].Service,
    	}
    	// Attention the last parameter : []UserKey{userKey}
    	req = clt.NewRequest(conf, method, []string{userKey})
    
    	serviceURL, err = clientRegistry.Filter(req.ServiceConfig(), 1)
    	if err != nil {
    		log.Error("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
    		// gxlog.CError("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
    		return
    	}
    	log.Debug("got serviceURL: %s", serviceURL)
    	// Set arbitrary headers in context
    	ctx = context.WithValue(context.Background(), public.DUBBOGO_CTX_KEY, map[string]string{
    		"X-Proxy-Id": "dubbogo",
    		"X-Services": service,
    		"X-Method":   method,
    	})
    
    	user = new(JsonRPCUser)
    	// Call service
    	if err = clt.Call(ctx, *serviceURL, req, user); err != nil {
    		log.Error("client.Call() return error:%+v", jerrors.ErrorStack(err))
    		// gxlog.CError("client.Call() return error:%+v", jerrors.ErrorStack(err))
    		return
    	}
    
    	log.Info("response result:%s", user)
    	// gxlog.CInfo("response result:%s", user)
    }