Browse Source

增加公共调用方法

master v0.1.3
guzeng 3 years ago
parent
commit
78ddac3025
6 changed files with 198 additions and 1 deletions
  1. +65
    -0
      client.go
  2. +27
    -0
      client_test.go
  3. +6
    -1
      common.go
  4. +10
    -0
      go.mod
  5. +24
    -0
      go.sum
  6. +66
    -0
      send_delay.go

+ 65
- 0
client.go View File

@ -0,0 +1,65 @@
package rabbitmqrpc
import (
"encoding/json"
"log"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
func Send(exchange, key string, data interface{}, url ...string) (*MsgSend, error) {
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
arg_data, err := json.Marshal(data)
if err != nil {
log.Println("json marshal error:", err)
}
arg := MsgBody{}
arg.Exchange = exchange
arg.Key = key
arg.Data = string(arg_data)
data_json, err := json.Marshal(arg)
if err != nil {
return nil, err
}
now_int64 := time.Now().Unix()
encryData := crypter.DesEn(string(data_json), DES_KEY)
now := strconv.FormatInt(now_int64, 10)
sign := Sign(encryData, now)
req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
res := &Response{}
err = conn.Send(req, res)
if err != nil {
return nil, err
}
res_data, err := GetOrgData(res)
var res_arr MsgSend
err = json.Unmarshal([]byte(res_data), &res_arr)
if err != nil {
return nil, err
}
return &res_arr, nil
}

+ 27
- 0
client_test.go View File

@ -0,0 +1,27 @@
package rabbitmqrpc
import (
"encoding/json"
"testing"
)
func Test_Send(t *testing.T) {
exchange := "send_msg"
key := "api"
msg := map[string]interface{}{
"site_id": "100002",
"dbname": "testing",
"data": map[string]string{
"order_id": "111",
"order_sn": "cccc",
},
}
data, _ := json.Marshal(msg)
ret, err := Send(exchange, key, string(data))
t.Log(ret)
t.Log(err)
}

+ 6
- 1
common.go View File

@ -7,7 +7,12 @@ type MsgBody struct {
Key string
Data string
}
type DelayMsgBody struct {
Exchange string
Key string
Data string
Delay int
}
type MsgSend struct {
Result string
}


+ 10
- 0
go.mod View File

@ -0,0 +1,10 @@
module git.tetele.net/tgo/rabbitmqrpc
go 1.14
require (
git.tetele.net/tgo/crypter v0.2.2 // indirect
github.com/chai2010/protorpc v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
)

+ 24
- 0
go.sum View File

@ -0,0 +1,24 @@
git.tetele.net/tgo/crypter v0.2.2 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo=
git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0=
github.com/chai2010/protorpc v1.0.0 h1:aJ45G9sl1utSKo35EqnBSTs5jqTpdJDJAuZMMYPAtFo=
github.com/chai2010/protorpc v1.0.0/go.mod h1:woR3WwjaQDqFjlzdVsFEKiK5Ur12QL8mYxVPjfr5z54=
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049 h1:K9KHZbXKpGydfDN0aZrsoHpLJlZsBrGMFWbgLDGnPZk=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

+ 66
- 0
send_delay.go View File

@ -0,0 +1,66 @@
package rabbitmqrpc
import (
"encoding/json"
"log"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
func SendDelay(exchange, key string, data interface{}, delay int, url ...string) (*MsgSend, error) {
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
arg_data, err := json.Marshal(data)
if err != nil {
log.Println("json marshal error:", err)
}
arg := DelayMsgBody{}
arg.Exchange = exchange
arg.Key = key
arg.Data = string(arg_data)
arg.Delay = delay
data_json, err := json.Marshal(arg)
if err != nil {
return nil, err
}
now_int64 := time.Now().Unix()
encryData := crypter.DesEn(string(data_json), DES_KEY)
now := strconv.FormatInt(now_int64, 10)
sign := Sign(encryData, now)
req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
res := &Response{}
err = conn.SendDelay(req, res)
if err != nil {
return nil, err
}
res_data, err := GetOrgData(res)
var res_arr MsgSend
err = json.Unmarshal([]byte(res_data), &res_arr)
if err != nil {
return nil, err
}
return &res_arr, nil
}

Loading…
Cancel
Save