-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc_server.go
96 lines (91 loc) · 2.59 KB
/
rpc_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package synapse
import (
"github.com/streadway/amqp"
"github.com/bitly/go-simplejson"
"fmt"
"encoding/json"
)
/**
绑定RPC监听队列
*/
func (s *Server) serverQueue() *amqp.Channel {
channel := s.CreateChannel(s.RpcProcessNum, "RpcServer")
q, err := channel.QueueDeclare(
fmt.Sprintf("%s_%s_server", s.SysName, s.AppName), // name
true, // durable
true, // delete when usused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
Log(fmt.Sprintf("Failed to declare Rpc Queue: %s", err), LogError)
}
err = channel.QueueBind(
q.Name,
fmt.Sprintf("server.%s", s.AppName),
s.SysName,
false,
nil)
if err != nil {
Log(fmt.Sprintf("Failed to Bind Rpc Exchange and Queue: %s", err), LogError)
}
return channel
}
/**
创建RPC监听
callback回调为监听到RPC请求后的处理函数
*/
func (s *Server) rpcServer(channel *amqp.Channel) {
msgs, err := channel.Consume(
fmt.Sprintf("%s_%s_server", s.SysName, s.AppName), // queue
fmt.Sprintf("%s.%s.server.%s", s.SysName, s.AppName, s.AppId),
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
Log(fmt.Sprintf("Failed to register Rpc Server consumer: %s", err), LogError)
}
for d := range msgs {
go s.rpcHandler(d, channel)
}
}
/**
RPC请求处理器
*/
func (s *Server) rpcHandler(d amqp.Delivery, channel *amqp.Channel) {
query, _ := simplejson.NewJson(d.Body)
if s.Debug {
logData, _ := query.MarshalJSON()
Log(fmt.Sprintf("RPC Receive: (%s)%s->%s@%s %s", d.MessageId, d.ReplyTo, d.Type, s.AppName, logData), LogDebug)
}
callback, ok := s.RpcCallback[d.Type]
result, _ := json.Marshal(map[string]string{"rpc_error": "method not found"})
if ok {
result, _ = json.Marshal(callback(query, d))
}
reply := fmt.Sprintf("client.%s.%s", d.ReplyTo, d.AppId)
err = channel.Publish(
s.SysName, // exchange
reply, // routing key
false, // mandatory
false, // immediatec
amqp.Publishing{
AppId: s.AppId,
MessageId: s.randomString(20),
ReplyTo: s.AppName,
Type: d.Type,
CorrelationId: d.MessageId,
Body: result,
})
if s.Debug {
Log(fmt.Sprintf("RPC Return: (%s)%s@%s->%s %s", d.MessageId, d.Type, s.AppName, d.ReplyTo, result), LogDebug)
}
if err != nil {
Log(fmt.Sprintf("Failed to reply Rpc Request: %s", err), LogError)
}
d.Ack(false)
}