diff --git a/doc/md/course/quick-start.md b/doc/md/course/quick-start.md index 085dec3c121781ea08e6d208023b909d875c8dc7..e2307454c95c5d022dce2db0637ea46bc0b55ef0 100644 --- a/doc/md/course/quick-start.md +++ b/doc/md/course/quick-start.md @@ -1,15 +1,15 @@ -# [dubbo-go 蹇€熷紑濮媇(https://github.com/dubbogo/dubbo-go-website/blob/master/docs/zh-cn/user/quick-start.md) +--- +title: 蹇€熷紑濮� +keywords: 蹇€熷紑濮�, hellowworld, Provider +description: 蹇€熶笂鎵媎ubbo-go锛岀紪鍐欎竴涓畝鍗曠殑hellowworld搴旂敤 +--- -| title | keywords | description | -| -------- | ------------------------------- | -------------------------------------------------- | -| 蹇€熷紑濮� | 蹇€熷紑濮�, hellowworld, Provider | 蹇€熶笂鎵� dubbo-go锛岀紪鍐欎竴涓畝鍗曠殑 hellowworld 搴旂敤 | - -# 蹇€熷紑濮� +# [蹇€熷紑濮媇(<(https://github.com/dubbogo/dubbo-go-website/blob/master/docs/zh-cn/user/quick-start.md)>) 閫氳繃涓€涓� `hellowworld` 渚嬪瓙甯﹂澶у蹇€熶笂鎵� Dubbo-go 妗嗘灦銆� -鍗忚锛欴ubbo -缂栫爜锛欻essian2 +鍗忚锛欴ubbo +缂栫爜锛欻essian2 娉ㄥ唽涓績锛歓ookeeper ## 鐜 @@ -21,79 +21,80 @@ ### 绗竴姝ワ細缂栧啓 `Provider` 缁撴瀯浣撳拰鎻愪緵鏈嶅姟鐨勬柟娉� -> https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-server/app/user.go +> <https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-server/app/user.go> 1. 缂栧啓闇€瑕佽缂栫爜鐨勭粨鏋勪綋锛岀敱浜庝娇鐢� `Hessian2` 浣滀负缂栫爜鍗忚锛宍User` 闇€瑕佸疄鐜� `JavaClassName` 鏂规硶锛屽畠鐨勮繑鍥炲€煎湪 dubbo 涓搴� User 绫荤殑绫诲悕銆� - ```go - type User struct { - Id string - Name string - Age int32 - Time time.Time - } - - func (u User) JavaClassName() string { - return "com.ikurento.user.User" - } - ``` - -1. 缂栧啓涓氬姟閫昏緫锛宍UserProvider` 鐩稿綋浜� dubbo 涓殑涓€涓湇鍔″疄鐜般€傞渶瑕佸疄鐜� `Reference` 鏂规硶锛岃繑鍥炲€兼槸杩欎釜鏈嶅姟鐨勫敮涓€鏍囪瘑锛屽搴� dubbo 鐨� `beans` 鍜� `path` 瀛楁銆� - - ```go - 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" - } - ``` - -1. 娉ㄥ唽鏈嶅姟鍜屽璞� - - ```go - func init() { - config.SetProviderService(new(UserProvider)) - // ------for hessian2------ - hessian.RegisterPOJO(&User{}) - } - ``` +```go +type User struct { + Id string + Name string + Age int32 + Time time.Time +} + +func (u User) JavaClassName() string { + return "com.ikurento.user.User" +} +``` + +2. 缂栧啓涓氬姟閫昏緫锛宍UserProvider` 鐩稿綋浜� dubbo 涓殑涓€涓湇鍔″疄鐜般€傞渶瑕佸疄鐜� `Reference` 鏂规硶锛岃繑鍥炲€兼槸杩欎釜鏈嶅姟鐨勫敮涓€鏍囪瘑锛屽搴� dubbo 鐨� `beans` 鍜� `path` 瀛楁銆� + +```go +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" +} +``` + +3. 娉ㄥ唽鏈嶅姟鍜屽璞� + +```go +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 +> <https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-server/app/server.go> 1. 寮曞叆蹇呴渶鐨� dubbo-go 鍖� - ```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" - ) - ``` - -1. main 鍑芥暟 - - ```go - func main() { - config.Load() - } - ``` +```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" +) + +``` + +2. main 鍑芥暟 + +```go +func main() { + config.Load() +} +``` ### 绗笁姝ワ細閰嶇疆 @@ -104,14 +105,15 @@ 涓昏缂栬緫浠ヤ笅閮ㄥ垎锛� - `registries` 缁撶偣涓嬮渶瑕侀厤缃� zk 鐨勬暟閲忓拰鍦板潃 + - `services` 缁撶偣涓嬮厤缃湇鍔$殑鍏蜂綋淇℃伅锛岄渶瑕侀厤缃� `interface` 閰嶇疆锛屼慨鏀逛负瀵瑰簲鏈嶅姟鐨勬帴鍙e悕锛屾湇鍔$殑 key 瀵瑰簲绗竴姝ヤ腑 `Provider` 鐨� `Reference` 杩斿洖鍊� -1. 鎶婁笂闈㈢殑涓や釜閰嶇疆鏂囦欢鍒嗗埆閰嶇疆涓虹幆澧冨彉閲� +2. 鎶婁笂闈㈢殑涓や釜閰嶇疆鏂囦欢鍒嗗埆閰嶇疆涓虹幆澧冨彉閲� - ```sh - export CONF_PROVIDER_FILE_PATH="xxx" - export APP_LOG_CONF_FILE="xxx" - ``` +```shell +export CONF_PROVIDER_FILE_PATH="xxx" +export APP_LOG_CONF_FILE="xxx" +``` #### 2.鑷畾涔夐厤缃枃浠� @@ -121,89 +123,90 @@ 2. 鍦╜config.Load()`涔嬪墠璁剧疆閰嶇疆 - ```go - func main() { - hessian.RegisterPOJO(&User{}) - providerConfig := config.ProviderConfig{} - yaml.Unmarshal([]byte(providerConfigStr), &providerConfig) - config.SetProviderConfig(providerConfig) - defaultServerConfig := dubbo.GetDefaultServerConfig() - dubbo.SetServerConfig(defaultServerConfig) - logger.SetLoggerLevel("warn") // info,warn - config.Load() - select { - } - } - ``` +```go +func main() { + hessian.RegisterPOJO(&User{}) + providerConfig := config.ProviderConfig{} + yaml.Unmarshal([]byte(providerConfigStr), &providerConfig) + config.SetProviderConfig(providerConfig) + defaultServerConfig := dubbo.GetDefaultServerConfig() + dubbo.SetServerConfig(defaultServerConfig) + logger.SetLoggerLevel("warn") // info,warn + config.Load() + select { + } +} +``` ## 鎺ョ潃鏄鎴风 ### 绗竴姝ワ細缂栧啓瀹㈡埛绔� `Provider` -> https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-client/app/user.go +> <https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-client/app/user.go> 1. 鍙傝€冩湇鍔$绗竴姝ョ殑绗竴鐐广€� + 2. 涓庢湇鍔$涓嶅悓鐨勬槸锛屾彁渚涙湇鍔$殑鏂规硶浣滀负缁撴瀯浣撶殑鍙傛暟锛屼笉闇€瑕佺紪鍐欏叿浣撲笟鍔¢€昏緫銆傚彟澶栵紝`Provider` 涓嶅搴� dubbo 涓殑鎺ュ彛锛岃€屾槸瀵瑰簲涓€涓疄鐜般€� - ```go - type UserProvider struct { - GetUser func(ctx context.Context, req []interface{}, rsp *User) error - } +```go +type UserProvider struct { + GetUser func(ctx context.Context, req []interface{}, rsp *User) error +} - func (u *UserProvider) Reference() string { - return "UserProvider" - } - ``` +func (u *UserProvider) Reference() string { + return "UserProvider" +} +``` 3. 娉ㄥ唽鏈嶅姟鍜屽璞� - ```go - func init() { - config.SetConsumerService(userProvider) - hessian.RegisterPOJO(&User{}) - } - ``` +```go +func init() { + config.SetConsumerService(userProvider) + hessian.RegisterPOJO(&User{}) +} +``` ### 绗簩姝ワ細缂栧啓瀹㈡埛绔富绋嬪簭 -> https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-client/app/client.go +> <https://github.com/dubbogo/dubbo-samples/blob/master/golang/helloworld/dubbo/go-client/app/client.go> 1. 寮曞叆蹇呴渶鐨� dubbo-go 鍖� - ```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" - ) - ``` - -1. main 鍑芥暟 - - ```go - 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...) - } - ``` +```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" +) +``` + +2. main 鍑芥暟 + +```go +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...) +} +``` ### 绗笁姝ワ細閰嶇疆 @@ -214,14 +217,15 @@ 涓昏缂栬緫浠ヤ笅閮ㄥ垎锛� - `registries` 缁撶偣涓嬮渶瑕侀厤缃� zk 鐨勬暟閲忓拰鍦板潃 + - `references` 缁撶偣涓嬮厤缃湇鍔$殑鍏蜂綋淇℃伅锛岄渶瑕侀厤缃� `interface` 閰嶇疆锛屼慨鏀逛负瀵瑰簲鏈嶅姟鐨勬帴鍙e悕锛屾湇鍔$殑 key 瀵瑰簲绗竴姝ヤ腑 `Provider` 鐨� `Reference` 杩斿洖鍊� -1. 鎶婁笂闈㈢殑涓や釜閰嶇疆鏂囦欢璐瑰埆閰嶇疆涓虹幆澧冨彉閲忥紝涓洪槻姝� log 鐨勭幆澧冨彉閲忓拰鏈嶅姟绔殑 log 鐜鍙橀噺鍐茬獊锛屽缓璁墍鏈夌殑鐜鍙橀噺涓嶈鍋氬叏灞€閰嶇疆锛屽湪褰撳墠璧锋晥鍗冲彲銆� +2. 鎶婁笂闈㈢殑涓や釜閰嶇疆鏂囦欢璐瑰埆閰嶇疆涓虹幆澧冨彉閲忥紝涓洪槻姝� log 鐨勭幆澧冨彉閲忓拰鏈嶅姟绔殑 log 鐜鍙橀噺鍐茬獊锛屽缓璁墍鏈夌殑鐜鍙橀噺涓嶈鍋氬叏灞€閰嶇疆锛屽湪褰撳墠璧锋晥鍗冲彲銆� - ```sh - export CONF_CONSUMER_FILE_PATH="xxx" - export APP_LOG_CONF_FILE="xxx" - ``` +```shell +export CONF_CONSUMER_FILE_PATH="xxx" +export APP_LOG_CONF_FILE="xxx" +``` #### 2.鑷畾涔夐厤缃枃浠� @@ -231,22 +235,22 @@ 2. 鍦╜config.Load()`涔嬪墠璁剧疆閰嶇疆 - ```go - func main() { - p := config.ConsumerConfig{} - yaml.Unmarshal([]byte(consumerConfigStr), &p) - config.SetConsumerConfig(p) - defaultClientConfig := dubbo.GetDefaultClientConfig() - dubbo.SetClientConf(defaultClientConfig) - logger.SetLoggerLevel("warn") // info,warn - config.Load() - - user := &User{} - err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user) - if err != nil { - log.Print(err) - return - } - log.Print(user) - } - ``` +```go +func main() { + p := config.ConsumerConfig{} + yaml.Unmarshal([]byte(consumerConfigStr), &p) + config.SetConsumerConfig(p) + defaultClientConfig := dubbo.GetDefaultClientConfig() + dubbo.SetClientConf(defaultClientConfig) + logger.SetLoggerLevel("warn") // info,warn + config.Load() + + user := &User{} + err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user) + if err != nil { + log.Print(err) + return + } + log.Print(user) +} +```