Skip to content

Guide for Use

Xin.Zh edited this page Jun 21, 2019 · 3 revisions

Guide for Use

How to start quick?


STEP 1: Define struct and provider

go-for-apache-dubbo/examples/dubbo/go-client/app/user.go go-for-apache-dubbo/examples/dubbo/go-server/app/user.go

// all structs must implements hessian.POJO
type User struct {}
func (User) JavaClassName string {
    return "com.Xxx.User" // same to the java class name
}

// for server
func init() {
	config.SetProviderService(new(UserProvider))
}
UserProvider struct {}
// args list reference "What to pay attention to?/function"
func (u *UserProvider) YourMethod(ctx context.Context, req []interface{}, rsp *User) error {
    // do your any things
}

// for client
func init() {
	config.SetConsumerService(new(UserProvider))
}
type UserProvider struct {
    YourMethod func(ctx context.Context, req []interface{}, rsp *User) error
}

STEP 2: Code client.go

go-for-apache-dubbo/examples/dubbo/go-client/app/client.go

// must import package you will use
// reference "What to pay attention to?/package"

// register pojo 
hessian.RegisterPOJO(&User{})
// Load
conMap, _ := config.Load()
// call
conMap["com.Xxx.User"].GetRPCService().YourMethod(...)

STEP 3: Code server.go

go-for-apache-dubbo/examples/dubbo/go-server/app/server.go

// must import package you will use
// reference "What to pay attention to?/package"

// register pojo 
hessian.RegisterPOJO(&User{})
// Load
_, proMap := config.Load()

STEP 4: Make client.yml and server.yml

go-for-apache-dubbo/config/testdata/consumer_config.yml

go-for-apache-dubbo/config/testdata/provider_config.yml

What to pay attention to?


hessian2

  • all structs must implements hessian.POJO
  • JavaEnum type should be:
const (
	MAN hessian.JavaEnum = iota
	WOMAN
)
var genderName = map[hessian.JavaEnum]string{
	MAN:   "MAN",
	WOMAN: "WOMAN",
}

var genderValue = map[string]hessian.JavaEnum{
	"MAN":   MAN,
	"WOMAN": WOMAN,
}
type Gender hessian.JavaEnum
func (g Gender) JavaClassName() string {
	return "com.Xxx.Gender"
}
func (g Gender) String() string {
	return genderName[hessian.JavaEnum(g)]
}

func (g Gender) EnumValue(s string) hessian.JavaEnum {
    return genderValue[s]
}
hessian.RegisterJavaEnum(Gender(MAN))
hessian.RegisterJavaEnum(Gender(WOMAN))
  • JavaClass type should be:
type User struct {}
func (User) JavaClassName string {
    return "com.Xxx.User" // same to the java class name
}
hessian.RegisterPOJO(&User{})
  • hessian:"" as tag of field unite field name of java and go, eg:
type User struct {
	A int `hessian:"b"` // name of A is b in the java, default a
}

function

  • support these argslist in the dubbo protocol
Method_1(ctx context.Context, req []interface{}, rsp *User) error
Method_2(id string, name string) (User, error)
Method_3(ctx context.Context, req []interface{}, rsp *[]interface{}) error
Method_4(ctx context.Context, req []interface{}) ([]interface{}, error)
Method_5(ctx context.Context, req []interface{}) (map[interface{}]interface{}, error)

package

  • must import
_ "github.com/dubbo/go-for-apache-dubbo/common/proxy/proxy_factory"
_ "github.com/dubbo/go-for-apache-dubbo/registry/protocol"
_ "github.com/dubbo/go-for-apache-dubbo/filter/impl"
_ "github.com/dubbo/go-for-apache-dubbo/cluster/cluster_impl"
_ "github.com/dubbo/go-for-apache-dubbo/cluster/loadbalance"
  • other
_ "github.com/dubbo/go-for-apache-dubbo/protocol/dubbo" // when using dubbo protocol
_ "github.com/dubbo/go-for-apache-dubbo/protocol/jsonrpc" // when using jsonrpc 2.0 protocol
_ "github.com/dubbo/go-for-apache-dubbo/registry/zookeeper" // when using zookeeper registry

config

  • when you want to get local ip, you can delete ip field.