修改使用的協定
1. 準備工作
- 已安裝 dubbo-go 命令列工具和相關工具
- 建立一個新的範例應用程式
2. 如何設定網路協定
如同在快速入門章節中所見,生成的範例將 Protocol 設定為 tri,表示使用 Triple 協定進行服務暴露和服務呼叫。快速入門章節中使用的設定 API 撰寫了設定,其優點是不需要使用設定檔。我們擷取並解釋與網路協定相關的內容。
使用設定檔
參考 samples/helloworld
- 客戶端使用設定檔設定網路協定
dubbo:
consumer:
references:
GreeterClientImpl:
protocol: tri # set protocol to tri
interface: com.apache.dubbo.sample.basic.IGreeter
- 伺服器使用設定檔設定網路協定
dubbo:
protocols:
triple: # define protocol-id 'triple'
name: tri # set protocol to tri
port: 20000 # set port to be listened
provider:
services:
GreeterProvider:
protocol-ids: triple # use protocol-ids named 'triple'
interface: com.apache.dubbo.sample.basic.IGreeter
3. 撰寫 Dubbo 協定的介面和實作
3.1 定義介面和傳輸結構,位於 api/api.go
package api
import (
"context"
"dubbo.apache.org/dubbo-go/v3/config"
hessian "github.com/apache/dubbo-go-hessian2"
"time"
)
//1. Define the transmission structure. If Java intercommunication is required, the field needs to correspond to the Java side, and the first letter is capitalized
type User struct {
ID string
name string
Age int32
Time time. Time
}
func (u *User) JavaClassName() string {
return "org.apache.dubbo.User" // If it communicates with Java, it needs to correspond to the full name of the User class on the Java side,
}
var (
UserProviderClient = &UserProvider{} // client pointer
)
// 2. Define the client stub class: UserProvider
type UserProvider struct {
// The dubbo label is used to adapt the uppercase method name of the go side client -> the lowercase method name of the java side, only the dubbo protocol client needs to use it
GetUser func(ctx context.Context, req int32) (*User, error) //`dubbo:"getUser"`
}
func init(){
hessian.RegisterPOJO(&User{}) // register transfer structure to hessian library
// Register the client stub class to the framework, and instantiate the client interface pointer userProvider
config. SetConsumerService(UserProviderClient)
}
2.2 撰寫 Go-Server 設定和程式碼
server/dubbogo.yaml
dubbo:
registries:
demoZK: # Define the service registration discovery center
protocol: zookeeper
address: 127.0.0.1:2181
protocols:
dubbo:
name: dubbo # protocol name dubbo
port: 20000 # Listening port
provider:
services:
UserProvider: # service provider structure class name
interface: org.apache.dubbo.UserProvider # The interface needs to correspond to the go/java client
server/server.go
package main
import (
"context"
"dubbo.apache.org/dubbo-go/v3/common/logger" // dubbogo framework log
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports" // dubbogo framework dependency, all dubbogo processes need to import once implicitly
"dubbo3-demo/api"
"strconv"
"time"
)
type UserProvider struct {
}
// implement the interface method
func (u *UserProvider) GetUser(ctx context.Context, req int32) (*api.User, error) {
var err error
logger.Infof("req:%#v", req)
user := &api. User{}
user.ID = strconv.Itoa(int(req))
user.Name = "Laurence"
user.Age = 22
user.Time = time.Now()
return user, err
}
//// MethodMapper defines method name mapping, from Go method name to Java lowercase method name, only dubbo protocol service interface needs to use
//// go -> go interoperability does not need to be used
//func (s *UserProvider) MethodMapper() map[string]string {
// return map[string]string{
// "GetUser": "getUser",
// }
//}
func init(){
config.SetProviderService(&UserProvider{}) // Register the service provider class, the class name corresponds to the service in the configuration file
}
// export DUBBO_GO_CONFIG_PATH=dubbogo.yml needs to set environment variables before running, and specify the location of the configuration file
func main() {
if err := config.Load(); err != nil {
panic(err)
}
select {}
}
2.3 撰寫 Go-Client 設定和程式碼
client/dubbogo.yaml
dubbo:
registries:
demoZK: # Define the service registration discovery center
protocol: zookeeper
address: 127.0.0.1:2181
consumer:
references:
UserProvider: # stub class name
protocol: dubbo # dubbo protocol, default hessian2 serialization method
interface: org.apache.dubbo.UserProvider # The interface needs to correspond to the go/java client
client/client.go
package main
import (
"context"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"
"dubbo3-demo/api"
)
func main(){
// start frame
if err := config.Load(); err != nil{
panic(err)
}
var i int32 = 1
// initiate the call
user, err := api.UserProviderClient.GetUser(context.TODO(), i)
if err != nil {
panic(err)
}
logger.Infof("response result: %+v", user)
}
4. 啟動服務
開啟兩個終端機,分別進入 server 和 client 目錄
分別執行;
export DUBBO_GO_CONFIG_PATH=dubbogo.yml
go run .
依序啟動伺服器和客戶端,即可在客戶端看到輸出
response result: &{ID:1 Name:laurence Age:22 Time:2021-11-12 17:59:39.185 +0800 CST}
呼叫成功