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

优化

parent 25b1512d
No related branches found
No related tags found
No related merge requests found
......@@ -2,16 +2,18 @@ package config
import (
"context"
"github.com/apache/dubbo-go/common/logger"
"errors"
)
const GenericReferKey = "GenericReferKey"
type GenericService struct {
Invoke func(req []interface{}) (interface{}, error) `dubbo:"$invoke"`
referenceStr string
}
func NewGenericService(reference string) *GenericService {
return &GenericService{referenceStr: reference}
func NewGenericService(referenceStr string) *GenericService {
return &GenericService{referenceStr: GenericReferKey}
}
func (u *GenericService) Reference() string {
......@@ -19,6 +21,7 @@ func (u *GenericService) Reference() string {
}
type GenericConsumerConfig struct {
ID string
Protocol string
Registry string
Version string
......@@ -30,27 +33,9 @@ type GenericConsumerConfig struct {
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
}
func (gConfig *GenericConsumerConfig) Load() (err error) {
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 := NewReferenceConfig(context.TODO())
//gr.Filter = "genericConsumer" //todo: add genericConsumer filter
gr.Registry = gConfig.Registry
gr.Protocol = gConfig.Protocol
......@@ -60,4 +45,20 @@ func (gConfig *GenericConsumerConfig) NewGenericReferenceConfig(id string) {
gr.Cluster = gConfig.Cluster
gr.Methods = append(gr.Methods, &MethodConfig{Name: "$invoke", Retries: gConfig.Retries})
gConfig.ref = gr
gConfig.genericService = NewGenericService(gConfig.ID)
SetConsumerService(gConfig.genericService)
rpcService := GetConsumerService(GenericReferKey)
if rpcService == nil {
err = errors.New("get rpcService err,GenericReferKey not Set ")
return
}
gConfig.ref.id = gConfig.ID
gConfig.ref.Refer()
gConfig.ref.Implement(rpcService)
return
}
func (gConfig *GenericConsumerConfig) GetGenericService() *GenericService {
return gConfig.genericService
}
package config
......@@ -61,8 +61,8 @@ func (c *ReferenceConfig) Prefix() string {
return constant.ReferenceConfigPrefix + c.InterfaceName + "."
}
func NewReferenceConfig(id string, ctx context.Context) *ReferenceConfig {
return &ReferenceConfig{id: id, context: ctx}
func NewReferenceConfig(ctx context.Context) *ReferenceConfig {
return &ReferenceConfig{context: ctx}
}
func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
......
......@@ -20,19 +20,21 @@ package main
import (
"context"
"fmt"
"github.com/apache/dubbo-go/protocol/dubbo"
hessian "github.com/dubbogo/hessian2"
"os"
"os/signal"
"syscall"
"time"
)
import (
hessian "github.com/dubbogo/hessian2"
)
import (
"github.com/apache/dubbo-go/common/logger"
_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
"github.com/apache/dubbo-go/config"
_ "github.com/apache/dubbo-go/protocol/dubbo"
"github.com/apache/dubbo-go/protocol/dubbo"
_ "github.com/apache/dubbo-go/registry/protocol"
_ "github.com/apache/dubbo-go/filter/impl"
......@@ -288,16 +290,18 @@ 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")
var genericConfig = config.GenericConsumerConfig{
ID: "UserProviderGer", //GetService的唯一标识不可缺少
InterfaceName: "com.ikurento.user.UserProvider",
Cluster: "failover",
Registry: "hangzhouzk",
Protocol: dubbo.DUBBO,
}
genericConfig.Load()
time.Sleep(3 * time.Second)
resp, err := genericConfig.GetService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}})
println("\n\n\nstart to generic invoke")
resp, err := genericConfig.GetGenericService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}})
if err != nil {
panic(err)
}
......
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