Skip to content
Snippets Groups Projects
Commit 97512662 authored by pantianying's avatar pantianying
Browse files

add go to java generic

parent 47c4d200
No related branches found
No related tags found
No related merge requests found
package config
import (
"context"
"github.com/apache/dubbo-go/common/logger"
)
type GenericService struct {
Invoke func(req []interface{}) (interface{}, error) `dubbo:"$invoke"`
referenceStr string
}
func NewGenericService(reference string) *GenericService {
return &GenericService{referenceStr: reference}
}
func (u *GenericService) Reference() string {
return u.referenceStr
}
type GenericConsumerConfig struct {
Protocol string
Registry string
Version string
Group string
InterfaceName string
Cluster string
Retries int64
ref *ReferenceConfig
genericService *GenericService
}
func (gConfig *GenericConsumerConfig) LoadGenericReferenceConfig(key string) {
gConfig.genericService = NewGenericService(key)
SetConsumerService(gConfig.genericService)
gConfig.NewGenericReferenceConfig(key)
rpcService := GetConsumerService(key)
if rpcService == nil {
logger.Warnf("%s is not exsist!", key)
return
}
gConfig.ref.id = key
gConfig.ref.Refer()
gConfig.ref.Implement(rpcService)
}
func (gConfig *GenericConsumerConfig) GetService() *GenericService {
return gConfig.genericService
}
func (gConfig *GenericConsumerConfig) NewGenericReferenceConfig(id string) {
gr := NewReferenceConfig(id, context.TODO())
//gr.Filter = "genericConsumer" //todo: add genericConsumer filter
gr.Registry = gConfig.Registry
gr.Protocol = gConfig.Protocol
gr.Version = gConfig.Version
gr.Group = gConfig.Group
gr.InterfaceName = gConfig.InterfaceName
gr.Cluster = gConfig.Cluster
gr.Methods = append(gr.Methods, &MethodConfig{Name: "$invoke", Retries: gConfig.Retries})
gConfig.ref = gr
}
......@@ -61,8 +61,8 @@ func (c *ReferenceConfig) Prefix() string {
return constant.ReferenceConfigPrefix + c.InterfaceName + "."
}
func NewReferenceConfig(ctx context.Context) *ReferenceConfig {
return &ReferenceConfig{context: ctx}
func NewReferenceConfig(id string, ctx context.Context) *ReferenceConfig {
return &ReferenceConfig{id: id, context: ctx}
}
func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
......@@ -109,7 +109,6 @@ func (refconfig *ReferenceConfig) Refer() {
regUrl.SubURL = url
}
}
if len(refconfig.urls) == 1 {
refconfig.invoker = extension.GetProtocol(refconfig.urls[0].Protocol).Refer(*refconfig.urls[0])
} else {
......
......@@ -20,16 +20,14 @@ package main
import (
"context"
"fmt"
"github.com/apache/dubbo-go/protocol/dubbo"
hessian "github.com/dubbogo/hessian2"
"os"
"os/signal"
"syscall"
"time"
)
import (
"github.com/dubbogo/hessian2"
)
import (
"github.com/apache/dubbo-go/common/logger"
_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
......@@ -53,19 +51,20 @@ var (
// export APP_LOG_CONF_FILE="xxx"
func main() {
hessian.RegisterJavaEnum(Gender(MAN))
hessian.RegisterJavaEnum(Gender(WOMAN))
hessian.RegisterPOJO(&User{})
config.Load()
println("\n\ntest")
test()
println("\n\ntest1")
test1()
println("\n\ntest2")
test2()
//hessian.RegisterJavaEnum(Gender(MAN))
//hessian.RegisterJavaEnum(Gender(WOMAN))
//hessian.RegisterPOJO(&User{})
//
//config.Load()
//println("\n\ntest")
//test()
//println("\n\ntest1")
//test1()
//println("\n\ntest2")
//test2()
//println("\n\ngeneric")
test3()
initSignal()
}
......@@ -288,3 +287,19 @@ func test2() {
}
println("error: %v", err)
}
func test3() {
println("\n\n\nstart to generic invoke")
var genericConfig config.GenericConsumerConfig
genericConfig.InterfaceName = "com.ikurento.user.UserProvider"
genericConfig.Cluster = "failover"
genericConfig.Registry = "hangzhouzk"
genericConfig.Protocol = dubbo.DUBBO
genericConfig.LoadGenericReferenceConfig("getUser-generic")
time.Sleep(3 * time.Second)
resp, err := genericConfig.GetService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}})
if err != nil {
panic(err)
}
fmt.Println("get resp:", resp)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment