此文档已经不再维护。您当前查看的是快照版本。如果想要查看最新版本的文档,请参阅最新版本。
推荐使用 IDL 定义跨语言的服务与编码格式,以下展示的是 Golang 语言版本的服务定义与开发方式,如果你有遗留系统或无多语言开发需求,可参考以下使用方式。
通过一个 hellowworld
例子带领大家快速上手Dubbo-go框架。
协议:Dubbo
编码:Hessian2
注册中心:Zookeeper
Provider
结构体和提供服务的方法https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-server/app/user.go
Hessian2
作为编码协议,User
需要实现 JavaClassName
方法,它的返回值在dubbo中对应User类的类名。type User struct {
Id string
Name string
Age int32
Time time.Time
}
func (u User) JavaClassName() string {
return "com.ikurento.user.User"
}
UserProvider
相当于dubbo中的一个服务实现。需要实现 Reference
方法,返回值是这个服务的唯一标识,对应dubbo的 beans
和 path
字段。type UserProvider struct {
}
func (u *UserProvider) GetUser(ctx context.Context, req []interface{}) (*User, error) {
println("req:%#v", req)
rsp := User{"A001", "hellowworld", 18, time.Now()}
println("rsp:%#v", rsp)
return &rsp, nil
}
func (u *UserProvider) Reference() string {
return "UserProvider"
}
func init() {
config.SetProviderService(new(UserProvider))
// ------for hessian2------
hessian.RegisterPOJO(&User{})
}
https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-server/app/server.go
import (
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/apache/dubbo-go/config"
_ "github.com/apache/dubbo-go/registry/protocol"
_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
_ "github.com/apache/dubbo-go/filter/impl"
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
_ "github.com/apache/dubbo-go/registry/zookeeper"
_ "github.com/apache/dubbo-go/protocol/dubbo"
)
func main() {
config.Load()
}
主要编辑以下部分:
registries
结点下需要配置zk的数量和地址
services
结点下配置服务的具体信息,需要配置 interface
配置,修改为对应服务的接口名,服务的key对应第一步中 Provider
的 Reference
返回值
export CONF_PROVIDER_FILE_PATH="xxx"
export APP_LOG_CONF_FILE="xxx"
Provider
https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-client/app/user.go
参考服务端第一步的第一点。
与服务端不同的是,提供服务的方法作为结构体的参数,不需要编写具体业务逻辑。另外,Provider
不对应dubbo中的接口,而是对应一个实现。
type UserProvider struct {
GetUser func(ctx context.Context, req []interface{}, rsp *User) error
}
func (u *UserProvider) Reference() string {
return "UserProvider"
}
func init() {
config.SetConsumerService(userProvider)
hessian.RegisterPOJO(&User{})
}
https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-client/app/client.go
import (
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/apache/dubbo-go/config"
_ "github.com/apache/dubbo-go/registry/protocol"
_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
_ "github.com/apache/dubbo-go/filter/impl"
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
_ "github.com/apache/dubbo-go/registry/zookeeper"
_ "github.com/apache/dubbo-go/protocol/dubbo"
)
func main() {
config.Load()
time.Sleep(3e9)
println("\n\n\nstart to test dubbo")
user := &User{}
err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user)
if err != nil {
panic(err)
}
println("response result: %v\n", user)
}
func println(format string, args ...interface{}) {
fmt.Printf("\033[32;40m"+format+"\033[0m\n", args...)
}
主要编辑以下部分:
registries
结点下需要配置zk的数量和地址
references
结点下配置服务的具体信息,需要配置 interface
配置,修改为对应服务的接口名,服务的key对应第一步中 Provider
的 Reference
返回值
export CONF_CONSUMER_FILE_PATH="xxx"
export APP_LOG_CONF_FILE="xxx"