W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
推薦使用 IDL 定義跨語言的服務(wù)與編碼格式,以下展示的是 Golang 語言版本的服務(wù)定義與開發(fā)方式,如果你有遺留系統(tǒng)或無多語言開發(fā)需求,可參考以下使用方式。
通過一個(gè) hellowworld 例子帶領(lǐng)大家快速上手Dubbo-go框架。
協(xié)議:Dubbo
編碼:Hessian2
注冊(cè)中心:Zookeeper
https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-server/app/user.go
type User struct { Id string Name string Age int32 Time time.Time } func (u User) JavaClassName() string { return "com.ikurento.user.User" }
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() }
主要編輯以下部分:
export CONF_PROVIDER_FILE_PATH="xxx" export APP_LOG_CONF_FILE="xxx"
https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-client/app/user.go
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...) }
主要編輯以下部分:
export CONF_CONSUMER_FILE_PATH="xxx" export APP_LOG_CONF_FILE="xxx"
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: