Skip to content

Commit fafa25e

Browse files
authoredJun 11, 2019
Merge pull request apache#84 from fangyincheng/fix-hessian2
Fix: handle java exception
2 parents 4f7ef9d + 2954fad commit fafa25e

File tree

8 files changed

+87
-56
lines changed

8 files changed

+87
-56
lines changed
 

‎examples/dubbo/go-client/app/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func main() {
9393
}
9494
println("response result: %v", ret1)
9595

96-
println("\n\n\nstart to test dubbo - getUser")
96+
println("\n\n\nstart to test dubbo - getUser2")
9797
user = &User{}
9898
err = conMap["com.ikurento.user.UserProvider"].GetRPCService().(*UserProvider).GetUser2(context.TODO(), []interface{}{1}, user)
9999
if err != nil {

‎go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/apache/dubbo-go
22

33
require (
4-
github.com/dubbogo/getty v0.0.0-20190607120257-8b0e100a88af // indirect
5-
github.com/dubbogo/hessian2 v0.0.0-20190607144249-afb8cbfad2cb // indirect
4+
github.com/dubbogo/getty v1.0.7
5+
github.com/dubbogo/hessian2 v1.0.1
66
github.com/pkg/errors v0.8.1
77
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec
88
github.com/stretchr/testify v1.3.0

‎go.sum

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/dubbogo/getty v0.0.0-20190607120257-8b0e100a88af/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo=
5-
github.com/dubbogo/hessian2 v0.0.0-20190607144249-afb8cbfad2cb/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM=
4+
github.com/dubbogo/getty v1.0.7 h1:5Hg+JwXyCKm9Yr4yJkm98ahhnoa8c2h6br5QJxwQ+YU=
5+
github.com/dubbogo/getty v1.0.7/go.mod h1:cRMSuoCmwc5lULFFnYZTxyCfZhObmRTNbS7XRnPNHSo=
6+
github.com/dubbogo/hessian2 v1.0.1 h1:ztI7gJxR3Isxrrl2jE1IZKX61eNR93eRKGhn49vPEX8=
7+
github.com/dubbogo/hessian2 v1.0.1/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM=
8+
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
69
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
10+
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
711
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
812
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
913
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -21,9 +25,11 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
2125
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
2226
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
2327
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
28+
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 h1:kcXqo9vE6fsZY5X5Rd7R1l7fTgnWaDCVmln65REefiE=
2429
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
2530
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2631
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
32+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2733
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2834
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
2935
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

‎protocol/dubbo/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
import (
2727
"github.com/dubbogo/getty"
28-
hessian "github.com/dubbogo/hessian2"
28+
"github.com/dubbogo/hessian2"
2929
perrors "github.com/pkg/errors"
3030
"go.uber.org/atomic"
3131
"gopkg.in/yaml.v2"

‎protocol/dubbo/client_test.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package dubbo
1919

2020
import (
21+
"bytes"
2122
"context"
2223
"sync"
2324
"testing"
@@ -73,7 +74,13 @@ func TestClient_Call(t *testing.T) {
7374
c.pool = newGettyRPCClientConnPool(c, clientConf.PoolSize, time.Duration(int(time.Second)*clientConf.PoolTTL))
7475

7576
user := &User{}
76-
err := c.Call("127.0.0.1:20000", url, "GetUser", []interface{}{"1", "username"}, user)
77+
err := c.Call("127.0.0.1:20000", url, "GetBigPkg", []interface{}{}, user)
78+
assert.NoError(t, err)
79+
assert.NotEqual(t, "", user.Id)
80+
assert.NotEqual(t, "", user.Name)
81+
82+
user = &User{}
83+
err = c.Call("127.0.0.1:20000", url, "GetUser", []interface{}{"1", "username"}, user)
7784
assert.NoError(t, err)
7885
assert.Equal(t, User{Id: "1", Name: "username"}, *user)
7986

@@ -137,7 +144,7 @@ func InitTest(t *testing.T) (protocol.Protocol, common.URL) {
137144

138145
methods, err := common.ServiceMap.Register("dubbo", &UserProvider{})
139146
assert.NoError(t, err)
140-
assert.Equal(t, "GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4", methods)
147+
assert.Equal(t, "GetBigPkg,GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4", methods)
141148

142149
// config
143150
SetClientConf(ClientConfig{
@@ -156,10 +163,10 @@ func InitTest(t *testing.T) (protocol.Protocol, common.URL) {
156163
TcpWBufSize: 65536,
157164
PkgRQSize: 1024,
158165
PkgWQSize: 512,
159-
TcpReadTimeout: "1s",
166+
TcpReadTimeout: "4s",
160167
TcpWriteTimeout: "5s",
161168
WaitTimeout: "1s",
162-
MaxMsgLen: 1024,
169+
MaxMsgLen: 10240000000,
163170
SessionName: "client",
164171
},
165172
})
@@ -180,7 +187,7 @@ func InitTest(t *testing.T) (protocol.Protocol, common.URL) {
180187
TcpReadTimeout: "1s",
181188
TcpWriteTimeout: "5s",
182189
WaitTimeout: "1s",
183-
MaxMsgLen: 1024,
190+
MaxMsgLen: 10240000000,
184191
SessionName: "server",
185192
}})
186193
assert.NoError(t, srvConf.CheckValidity())
@@ -200,6 +207,18 @@ func InitTest(t *testing.T) (protocol.Protocol, common.URL) {
200207
return proto, url
201208
}
202209

210+
// size:4801228
211+
func (u *UserProvider) GetBigPkg(ctx context.Context, req []interface{}, rsp *User) error {
212+
argBuf := new(bytes.Buffer)
213+
for i := 0; i < 4000; i++ {
214+
argBuf.WriteString("击鼓其镗,踊跃用兵。土国城漕,我独南行。从孙子仲,平陈与宋。不我以归,忧心有忡。爰居爰处?爰丧其马?于以求之?于林之下。死生契阔,与子成说。执子之手,与子偕老。于嗟阔兮,不我活兮。于嗟洵兮,不我信兮。")
215+
argBuf.WriteString("击鼓其镗,踊跃用兵。土国城漕,我独南行。从孙子仲,平陈与宋。不我以归,忧心有忡。爰居爰处?爰丧其马?于以求之?于林之下。死生契阔,与子成说。执子之手,与子偕老。于嗟阔兮,不我活兮。于嗟洵兮,不我信兮。")
216+
}
217+
rsp.Id = argBuf.String()
218+
rsp.Name = argBuf.String()
219+
return nil
220+
}
221+
203222
func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error {
204223
rsp.Id = req[0].(string)
205224
rsp.Name = req[1].(string)

‎protocol/dubbo/codec.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
import (
28-
hessian "github.com/dubbogo/hessian2"
28+
"github.com/dubbogo/hessian2"
2929
perrors "github.com/pkg/errors"
3030
)
3131

@@ -88,16 +88,14 @@ func (p *DubboPackage) Unmarshal(buf *bytes.Buffer, opts ...interface{}) error {
8888
return perrors.Errorf("opts[0] is not of type *Client")
8989
}
9090

91-
p.Body = client.GetPendingResponse(SequenceType(p.Header.ID)).reply
92-
if p.Body == nil {
91+
pendingRsp := client.GetPendingResponse(SequenceType(p.Header.ID))
92+
if pendingRsp == nil {
9393
return perrors.Errorf("client.GetPendingResponse(%v) = nil", p.Header.ID)
94+
} else {
95+
p.Body = &hessian.Response{RspObj: pendingRsp.reply}
9496
}
9597
}
9698

97-
if p.Header.Type&hessian.PackageHeartbeat != 0x00 {
98-
return nil
99-
}
100-
10199
// read body
102100
err = codec.ReadBody(p.Body)
103101
return perrors.WithStack(err)

‎protocol/dubbo/listener.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func (h *RpcClientHandler) OnMessage(session getty.Session, pkg interface{}) {
8686

8787
if p.Header.Type&hessian.PackageHeartbeat != 0x00 {
8888
logger.Debugf("get rpc heartbeat response{header: %#v, body: %#v}", p.Header, p.Body)
89+
if p.Err != nil {
90+
logger.Errorf("rpc heartbeat response{error: %#v}", p.Err)
91+
}
8992
return
9093
}
9194
logger.Debugf("get rpc response{header: %#v, body: %#v}", p.Header, p.Body)
@@ -203,7 +206,6 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) {
203206
// not twoway
204207
if p.Header.Type&hessian.PackageRequest_TwoWay == 0x00 {
205208
twoway = false
206-
h.reply(session, p, hessian.PackageResponse)
207209
}
208210

209211
invoker := h.exporter.GetInvoker()

‎protocol/dubbo/readwriter.go

+43-37
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
import (
2626
"github.com/dubbogo/getty"
27-
hessian "github.com/dubbogo/hessian2"
27+
"github.com/dubbogo/hessian2"
2828
perrors "github.com/pkg/errors"
2929
)
3030
import (
@@ -63,7 +63,10 @@ func (p *RpcClientPackageHandler) Read(ss getty.Session, data []byte) (interface
6363
return nil, 0, perrors.WithStack(err)
6464
}
6565

66-
return pkg, len(data), nil
66+
pkg.Err = pkg.Body.(*hessian.Response).Exception
67+
pkg.Body = pkg.Body.(*hessian.Response).RspObj
68+
69+
return pkg, hessian.HEADER_LENGTH + pkg.Header.BodyLen, nil
6770
}
6871

6972
func (p *RpcClientPackageHandler) Write(ss getty.Session, pkg interface{}) error {
@@ -110,44 +113,47 @@ func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface
110113

111114
return nil, 0, perrors.WithStack(err)
112115
}
113-
// convert params of request
114-
req := pkg.Body.([]interface{}) // length of body should be 7
115-
if len(req) > 0 {
116-
var dubboVersion, argsTypes string
117-
var args []interface{}
118-
var attachments map[interface{}]interface{}
119-
if req[0] != nil {
120-
dubboVersion = req[0].(string)
121-
}
122-
if req[1] != nil {
123-
pkg.Service.Path = req[1].(string)
124-
}
125-
if req[2] != nil {
126-
pkg.Service.Version = req[2].(string)
127-
}
128-
if req[3] != nil {
129-
pkg.Service.Method = req[3].(string)
130-
}
131-
if req[4] != nil {
132-
argsTypes = req[4].(string)
133-
}
134-
if req[5] != nil {
135-
args = req[5].([]interface{})
136-
}
137-
if req[6] != nil {
138-
attachments = req[6].(map[interface{}]interface{})
139-
}
140-
pkg.Service.Interface = attachments[constant.INTERFACE_KEY].(string)
141-
pkg.Body = map[string]interface{}{
142-
"dubboVersion": dubboVersion,
143-
"argsTypes": argsTypes,
144-
"args": args,
145-
"service": common.ServiceMap.GetService(DUBBO, pkg.Service.Interface),
146-
"attachments": attachments,
116+
117+
if pkg.Header.Type&hessian.PackageHeartbeat == 0x00 {
118+
// convert params of request
119+
req := pkg.Body.([]interface{}) // length of body should be 7
120+
if len(req) > 0 {
121+
var dubboVersion, argsTypes string
122+
var args []interface{}
123+
var attachments map[interface{}]interface{}
124+
if req[0] != nil {
125+
dubboVersion = req[0].(string)
126+
}
127+
if req[1] != nil {
128+
pkg.Service.Path = req[1].(string)
129+
}
130+
if req[2] != nil {
131+
pkg.Service.Version = req[2].(string)
132+
}
133+
if req[3] != nil {
134+
pkg.Service.Method = req[3].(string)
135+
}
136+
if req[4] != nil {
137+
argsTypes = req[4].(string)
138+
}
139+
if req[5] != nil {
140+
args = req[5].([]interface{})
141+
}
142+
if req[6] != nil {
143+
attachments = req[6].(map[interface{}]interface{})
144+
}
145+
pkg.Service.Interface = attachments[constant.INTERFACE_KEY].(string)
146+
pkg.Body = map[string]interface{}{
147+
"dubboVersion": dubboVersion,
148+
"argsTypes": argsTypes,
149+
"args": args,
150+
"service": common.ServiceMap.GetService(DUBBO, pkg.Service.Interface),
151+
"attachments": attachments,
152+
}
147153
}
148154
}
149155

150-
return pkg, len(data), nil
156+
return pkg, hessian.HEADER_LENGTH + pkg.Header.BodyLen, nil
151157
}
152158

153159
func (p *RpcServerPackageHandler) Write(ss getty.Session, pkg interface{}) error {

0 commit comments

Comments
 (0)
Please sign in to comment.