Procházet zdrojové kódy

发消息测试通过

qdy před 3 měsíci
rodič
revize
0debdbe6b1

+ 1
- 1
functions/queue_functions.go Zobrazit soubor

@@ -55,7 +55,7 @@ func CreateQueue(rabbitFactory *rabbitmq.RabbitMQFactory, req types.QueueRequest
55 55
 }
56 56
 
57 57
 // BindQueue 绑定队列到交换机
58
-func BindQueue(rabbitFactory *rabbitmq.RabbitMQFactory, req types.BindRequest) *types.QueryResult {
58
+func BindQueue(rabbitFactory *rabbitmq.RabbitMQFactory, req types.QueueBindRequest) *types.QueryResult {
59 59
 	// 设置默认值
60 60
 	if req.ChannelName == "" {
61 61
 		req.ChannelName = "default"

+ 1
- 1
main.go Zobrazit soubor

@@ -101,7 +101,7 @@ func registerRabbitMQRoutes(webService web.Service, rabbitFactory *rabbitmq.Rabb
101 101
 	})))
102 102
 
103 103
 	// 发送原始消息(字节流)
104
-	webService.Handle("/api/rabbitmq/message/send-bytes", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
104
+	webService.Handle("/api/rabbitmq/message/send/bytes", middleware.JWTAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
105 105
 		myhandle.QueryHandlerJson(w, r, rabbitFactory, functions.SendBytesMessage)
106 106
 	})))
107 107
 

+ 70
- 16
test/my_create_exchange_test.go Zobrazit soubor

@@ -8,34 +8,49 @@ import (
8 8
 	"net/http"
9 9
 	"testing"
10 10
 	"time"
11
+
12
+	"git.x2erp.com/qdy/go-base/types"
11 13
 )
12 14
 
13
-type ExchangeRequest struct {
14
-	ChannelName  string            `json:"channel_name"`
15
-	ExchangeName string            `json:"exchange_name"`
16
-	ExchangeType string            `json:"exchange_type"`
17
-	Durable      bool              `json:"durable"`
18
-	Internal     bool              `json:"internal"`
19
-	AutoDelete   bool              `json:"auto_delete"`
20
-	NoWait       bool              `json:"no_wait"`
21
-	Arguments    map[string]string `json:"arguments"`
22
-}
15
+// type ExchangeRequest struct {
16
+// 	ChannelName  string            `json:"channel_name"`
17
+// 	ExchangeName string            `json:"exchange_name"`
18
+// 	ExchangeType string            `json:"exchange_type"`
19
+// 	Durable      bool              `json:"durable"`
20
+// 	Internal     bool              `json:"internal"`
21
+// 	AutoDelete   bool              `json:"auto_delete"`
22
+// 	NoWait       bool              `json:"no_wait"`
23
+// 	Arguments    map[string]string `json:"arguments"`
24
+// }
23 25
 
24 26
 func TestCreateexchange(t *testing.T) {
25 27
 	// 1. 准备请求数据
26
-	requestData := ExchangeRequest{
27
-		ChannelName:  "test_channel",
28
-		ExchangeName: "test_exchange",
29
-		ExchangeType: "direct", // direct, fanout, topic, headers
28
+	requestData := types.ExchangeRequest{
29
+		ChannelName:  "v_bdx_channel",
30
+		ExchangeName: "v_bdx_exchange",
31
+		ExchangeType: "topic", // direct, fanout, topic, headers
30 32
 		Durable:      true,
31 33
 		Internal:     false,
32 34
 		AutoDelete:   false,
33 35
 		NoWait:       false,
34 36
 		Arguments: map[string]string{
35
-			"x-delayed-type": "direct",
37
+			"x-delayed-type": "topic",
36 38
 		},
37 39
 	}
38 40
 
41
+	// 消息队列
42
+	queueRequest := types.QueueRequest{
43
+		QueueName:   "vbdx_queue_count",
44
+		ChannelName: requestData.ChannelName,
45
+	}
46
+
47
+	queueBindRequest := types.QueueBindRequest{
48
+		ChannelName:  requestData.ChannelName,
49
+		QueueName:    queueRequest.QueueName,
50
+		ExchangeName: requestData.ExchangeName,
51
+		RoutingKey:   "v-bdx.#",
52
+	}
53
+
39 54
 	// 2. 将数据转换为JSON
40 55
 	jsonData, err := json.Marshal(requestData)
41 56
 	if err != nil {
@@ -46,8 +61,47 @@ func TestCreateexchange(t *testing.T) {
46 61
 	// 3. 发送HTTP POST请求
47 62
 	url := "http://localhost:9090/api/rabbitmq/exchange/create"
48 63
 
64
+	POST(jsonData, url)
65
+
66
+	//建立队列
67
+	CreateQueue(queueRequest)
68
+
69
+	//绑定
70
+	CreateQueueBind(queueBindRequest)
71
+}
72
+
73
+func CreateQueue(queueRequest types.QueueRequest) {
74
+
75
+	// 2. 将数据转换为JSON
76
+	jsonData, err := json.Marshal(queueRequest)
77
+	if err != nil {
78
+		fmt.Printf("JSON编码失败: %v\n", err)
79
+		return
80
+	}
81
+	url := "http://localhost:9090/api/rabbitmq/queue/create"
82
+
83
+	POST(jsonData, url)
84
+
85
+}
86
+
87
+func CreateQueueBind(queueBindRequest types.QueueBindRequest) {
88
+
89
+	// 2. 将数据转换为JSON
90
+	jsonData, err := json.Marshal(queueBindRequest)
91
+	if err != nil {
92
+		fmt.Printf("JSON编码失败: %v\n", err)
93
+		return
94
+	}
95
+	url := "http://localhost:9090/api/rabbitmq/queue/bind"
96
+
97
+	POST(jsonData, url)
98
+
99
+}
100
+
101
+func POST(jsonData []byte, url string) {
102
+
49 103
 	// 如果服务端需要认证,添加JWT token
50
-	token := "your-jwt-token-here" // 替换为实际的JWT token
104
+	token := "123" // 替换为实际的JWT token
51 105
 
52 106
 	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
53 107
 	if err != nil {

+ 98
- 0
test/my_send_message_test.go Zobrazit soubor

@@ -0,0 +1,98 @@
1
+package test
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"fmt"
7
+	"io"
8
+	"net/http"
9
+	"testing"
10
+	"time"
11
+
12
+	"git.x2erp.com/qdy/go-base/types"
13
+)
14
+
15
+func TestSendMessage(t *testing.T) {
16
+
17
+	queryRequest := getQueyRequest()
18
+
19
+	messageRequest := types.MessageRequest{
20
+		ChannelName:  "v_bdx_channel",
21
+		ExchangeName: "v_bdx_exchange",
22
+		RoutingKey:   "v-bdx.count",
23
+		Message:      queryRequest,
24
+		ContentType:  "json",
25
+	}
26
+
27
+	// 2. 将数据转换为JSON
28
+	jsonData, err := json.Marshal(messageRequest)
29
+	if err != nil {
30
+		fmt.Printf("JSON编码失败: %v\n", err)
31
+		return
32
+	}
33
+
34
+	// 3. 发送HTTP POST请求
35
+	url := "http://localhost:9090/api/rabbitmq/message/send"
36
+
37
+	POSTMessage(jsonData, url)
38
+
39
+}
40
+
41
+func getQueyRequest() types.QueryRequest {
42
+	sql := `SELECT * FROM (SELECT a.*, ROWNUM rn FROM (SELECT CLOTHING_ID, CLOTHING_NAME FROM X6_STOCK_DEV.A3_CLOTHING ORDER BY CLOTHING_ID) a WHERE ROWNUM <= :1) WHERE rn > :2
43
+	`
44
+
45
+	// // 3个参数
46
+	params := []interface{}{
47
+		10,
48
+		0,
49
+	}
50
+
51
+	return types.QueryRequest{
52
+		SQL:              sql,
53
+		PositionalParams: params,
54
+		WriterHeader:     false,
55
+	}
56
+}
57
+
58
+func POSTMessage(jsonData []byte, url string) {
59
+
60
+	// 如果服务端需要认证,添加JWT token
61
+	token := "123" // 替换为实际的JWT token
62
+
63
+	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
64
+	if err != nil {
65
+		fmt.Printf("创建请求失败: %v\n", err)
66
+		return
67
+	}
68
+
69
+	// 添加请求头
70
+	req.Header.Set("Content-Type", "application/json")
71
+	if token != "" {
72
+		req.Header.Set("Authorization", "Bearer "+token)
73
+	}
74
+
75
+	// 4. 发送请求并获取响应
76
+	client := &http.Client{
77
+		Timeout: 10 * time.Second,
78
+	}
79
+
80
+	resp, err := client.Do(req)
81
+	if err != nil {
82
+		fmt.Printf("请求失败: %v\n", err)
83
+		return
84
+	}
85
+	defer resp.Body.Close()
86
+
87
+	// 5. 读取响应
88
+	body, err := io.ReadAll(resp.Body)
89
+	if err != nil {
90
+		fmt.Printf("读取响应失败: %v\n", err)
91
+		return
92
+	}
93
+
94
+	// 6. 输出结果
95
+	fmt.Printf("状态码: %d\n", resp.StatusCode)
96
+	fmt.Printf("响应头: %v\n", resp.Header)
97
+	fmt.Printf("响应体: %s\n", string(body))
98
+}

Loading…
Zrušit
Uložit