motan-go
motan-go copied to clipboard
使用grpc协议时报错 transport is closing
server端配置
#config fo server
motan-server:
mport: 8002 # agent manage port
log_dir: "./serverlogs"
application: "ray-test" # server identify.
#config of registries
motan-registry: #motan-registry 区用来配置不同的注册中心,多个注册中心以id进行区分
direct-registry: # 注册中心id,service引用时需与此id相同
protocol: direct # 注册中心协议,也即注册中心类型。此处为便于测试使用直连注册中心,实际注册时不发生注册行为。
#conf of services
motan-service:
mytest-motan2:
path: com.weibo.motan.demo.service.MotanDemoService # 服务名称
group: motan-demo-rpc #服务所属group
protocol: grpc
registry: direct-registry
serialization: simple #目前golang版本仅支持simple序列化方式,其他序列化方式会逐步提供
ref : "main.MotanDemoService" #golang中对service的具体实现类引用。此处为`包名.类名`方式引用,也可以使用自定义的id,需要与注册服务实现类时的id参数一致
export: "motan2:8100" #对外提供服务的端口。不同service可以使用相同export端口,前提条件为协议与序列化等配置必须相同。
client端配置
#config fo client
motan-client:
mport: 8002 # client manage port
log_dir: "./clientlogs"
application: "ray-test" # client identify.
#config of registries
motan-registry:
direct-registry: # registry id
protocol: direct # registry type.
host: 127.0.0.1
port: 8100
#conf of refers
motan-refer:
mytest-motan2:
path: com.weibo.motan.demo.service.MotanDemoService # e.g. service name for subscribe
group: motan-demo-rpc # group name
protocol: grpc # rpc protocol
registry: direct-registry
requestTimeout: 1000
serialization: simple
haStrategy: failover
loadbalance: roundrobin
server 端代码
package main
import (
"fmt"
"time"
motan "github.com/weibocom/motan-go"
)
func main() {
runServerDemo()
}
func runServerDemo() {
mscontext := motan.GetMotanServerContext("serverdemo.yaml") //通过配置文件获取配置信息。所有的导出服务推荐只使用一个配置文件进行配置。
mscontext.RegisterService(&MotanDemoService{}, "") // 注册具体service实现类,可以在注册时指定别名,配置中可以通过别名进行引用。如果不使用别名,则通过`包名.类名`进行引用。
mscontext.Start(nil) // 注册完所有服务实现类后,通过start启动所有服务,完成服务注册
time.Sleep(time.Second * 50000000)
}
// service 具体实现类
type MotanDemoService struct{}
func (m *MotanDemoService) Hello(name string) string {
fmt.Printf("MotanDemoService hello:%s\n", name)
return "hello " + name
}
client 端代码
package main
import (
"fmt"
motan "github.com/weibocom/motan-go"
)
func main() {
runClientDemo()
}
func runClientDemo() {
mccontext := motan.GetClientContext("clientdemo.yaml")
mccontext.Start(nil)
mclient := mccontext.GetClient("mytest-motan2")
var reply string
err := mclient.Call("hello", []interface{}{[]byte("Ray")}, &reply) // sync call
if err != nil {
fmt.Printf("motan call fail! err:%v\n", err)
} else {
fmt.Printf("motan call success! reply:%s\n", reply)
}
}
报错信息
motan call fail! err:call fail over 0 times.Exception:transport is closing
可以确认一下error日志中是否有什么异常。 grpc导出的服务,必须是由grpc生成的service,直接用MotanDemoService是不行的。
server端报错如下:
E0621 18:17:09.503669 74411 motanProtocol.go:268] worng magic num:20562, err:
“grpc导出的服务,必须是由grpc生成的service,直接用MotanDemoService是不行的。” 怎么通过motan生成grpc service呢,还是我得自己生成grpc service
另外,如何使用protobuf的序列化方法呢
grpc是一种兼容方案,前提条件就是已经有了grpc服务了,所以如果使用新grpc服务需要自行创建。 如果仅仅使用pb对象作为参数的话,可以修改server和client端的serialization配置
serialization: protobuf