修改使用的协议
1. 准备工作
- 已安装 dubbo-go cli 工具和依赖工具
- 创建一个新的演示应用程序
2. 如何配置网络协议
如快速入门部分所示,生成的 Demo 将 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. 启动服务
打开两个终端,分别进入服务器和客户端目录
分别执行;
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}
调用成功