Bläddra i källkod

拆开配置文件

qdy 2 månader sedan
förälder
incheckning
b8cbec1648
3 ändrade filer med 106 tillägg och 76 borttagningar
  1. 80
    0
      factory/rabbitmq/consumer_factory.go
  2. 9
    2
      factory/rabbitmq/rabbitmq_factory.go
  3. 17
    74
      gct.sh

+ 80
- 0
factory/rabbitmq/consumer_factory.go Visa fil

@@ -0,0 +1,80 @@
1
+package rabbitmq
2
+
3
+import (
4
+	"fmt"
5
+
6
+	"git.x2erp.com/qdy/go-base/types"
7
+	amqp "github.com/streadway/amqp"
8
+)
9
+
10
+// MessageHandler 消息处理器接口
11
+type MessageHandler interface {
12
+	Process(ueueRequest *types.QueueRequest, body []byte) types.QueryResult
13
+}
14
+
15
+// Consumer 消费者结构体
16
+type Consumer struct {
17
+	Channel      *amqp.Channel
18
+	QueueName    string
19
+	Handler      MessageHandler
20
+	QueueRequest *types.QueueRequest
21
+}
22
+
23
+// CreateConsumer 创建消费者
24
+// 返回Consumer对象,创建后自动开始消费
25
+func (f *RabbitMQFactory) CreateConsumer(queueRequest *types.QueueRequest, consumerID string, handler MessageHandler) (*Consumer, error) {
26
+	// 生成唯一的通道名称
27
+	channelName := fmt.Sprintf("%s_%s", queueRequest.QueueName, consumerID)
28
+
29
+	// 创建新通道
30
+	channel, err := f.CreateChannel(channelName)
31
+	if err != nil {
32
+		return nil, fmt.Errorf("failed to create channel: %v", err)
33
+	}
34
+
35
+	// 设置QoS(一次处理一条)
36
+	err = channel.Qos(1, 0, false)
37
+	if err != nil {
38
+		f.CloseChannel(channelName) // 清理
39
+		return nil, fmt.Errorf("failed to set qos: %v", err)
40
+	}
41
+
42
+	// 开始消费
43
+	deliveries, err := channel.Consume(
44
+		queueRequest.QueueName,
45
+		channelName, // 使用通道名称作为消费者标签
46
+		false,       // 手动确认
47
+		false,       // 非独占
48
+		false,       // 不排除本地连接
49
+		false,       // 阻塞等待
50
+		nil,         // 额外参数
51
+	)
52
+
53
+	if err != nil {
54
+		f.CloseChannel(channelName) // 清理
55
+		return nil, fmt.Errorf("failed to start consumer: %v", err)
56
+	}
57
+
58
+	// 创建Consumer对象
59
+	consumer := &Consumer{
60
+		Channel:      channel,
61
+		QueueName:    queueRequest.QueueName,
62
+		Handler:      handler,
63
+		QueueRequest: queueRequest,
64
+	}
65
+
66
+	// 启动goroutine处理消息
67
+	go func() {
68
+		for delivery := range deliveries {
69
+			queryResult := handler.Process(queueRequest, delivery.Body)
70
+			if queryResult.Success {
71
+				delivery.Ack(false)
72
+			} else {
73
+				delivery.Reject(true)
74
+			}
75
+		}
76
+		// deliveries通道关闭时,goroutine自动结束
77
+	}()
78
+
79
+	return consumer, nil
80
+}

+ 9
- 2
factory/rabbitmq/rabbitmq_factory.go Visa fil

@@ -6,6 +6,7 @@ import (
6 6
 	"sync"
7 7
 
8 8
 	"git.x2erp.com/qdy/go-base/config"
9
+	"git.x2erp.com/qdy/go-base/config/subconfigs"
9 10
 	amqp "github.com/streadway/amqp"
10 11
 )
11 12
 
@@ -14,7 +15,7 @@ type RabbitMQFactory struct {
14 15
 	mu      sync.RWMutex
15 16
 	clients map[string]*amqp.Channel
16 17
 	conn    *amqp.Connection
17
-	config  config.RabbitMQConfig
18
+	config  *subconfigs.RabbitMQConfig
18 19
 }
19 20
 
20 21
 // NewRabbitMQFactory 创建RabbitMQ工厂
@@ -38,8 +39,14 @@ func NewRabbitMQFactory() (*RabbitMQFactory, error) {
38 39
 		rabbitConfig.Port,
39 40
 		rabbitConfig.Vhost)
40 41
 
42
+	// 简单的连接配置
43
+	mqConfig := amqp.Config{
44
+		Properties: amqp.Table{
45
+			"connection_name": cfg.GetService().InstanceName,
46
+		}}
47
+
41 48
 	// 建立连接
42
-	conn, err := amqp.Dial(url)
49
+	conn, err := amqp.DialConfig(url, mqConfig)
43 50
 	if err != nil {
44 51
 		return nil, fmt.Errorf("failed to connect to rabbitmq: %v", err)
45 52
 	}

+ 17
- 74
gct.sh Visa fil

@@ -1,82 +1,25 @@
1 1
 #!/bin/bash
2 2
 
3
-# 脚本用法:./git-commit-and-tag.sh "你的提交描述" "版本号"
3
+# 使用命令行参数,如果没有提供则使用默认值
4
+COMMIT_MSG="${1:-初始提交}"
5
+TAG_VERSION="${2:-v0.1.0}"
4 6
 
5
-# 检查参数数量
6
-if [ $# -ne 2 ]; then
7
-    echo "错误: 脚本需要2个参数。"
8
-    echo "用法: $0 \"提交描述\" \"版本号\""
9
-    echo "示例: $0 \"修复了登录问题\" \"v1.2.3\""
10
-    exit 1
11
-fi
7
+echo "提交信息: $COMMIT_MSG"
8
+echo "标签版本: $TAG_VERSION"
12 9
 
13
-# 分配参数
14
-COMMIT_MESSAGE="$1"
15
-VERSION_TAG="$2"
10
+# 提交和推送
11
+git add .
12
+git commit -m "$COMMIT_MSG"
13
+git push -u origin master
16 14
 
17
-# 检查当前目录是否为Git仓库
18
-if ! git rev-parse --git-dir > /dev/null 2>&1; then
19
-    echo "错误: 当前目录不是一个Git仓库。"
20
-    exit 1
21
-fi
15
+# 创建标签(关键修改在这里)
16
+# 方法1:使用注释标签(推荐,不会打开编辑器)
17
+git tag -a "$TAG_VERSION" -m "Release $TAG_VERSION"
22 18
 
23
-echo "开始处理提交和版本标签..."
24
-echo "提交描述: $COMMIT_MESSAGE"
25
-echo "版本标签: $VERSION_TAG"
19
+# 或者方法2:如果你需要GPG签名,使用这个
20
+# GIT_EDITOR=true git tag -s "$TAG_VERSION" -m "Release $TAG_VERSION"
26 21
 
27
-# 检查是否有未提交的更改
28
-if [ -n "$(git status --porcelain)" ]; then
29
-    echo "检测到未提交的更改,正在提交..."
30
-    
31
-    # 添加所有更改到暂存区
32
-    git add .
33
-    
34
-    # 进行提交
35
-    git commit -m "$COMMIT_MESSAGE"
36
-    if [ $? -ne 0 ]; then
37
-        echo "错误: 提交失败。"
38
-        exit 1
39
-    fi
40
-    echo "✅ 更改已提交"
41
-else
42
-    echo "提示: 没有未提交的更改,跳过提交步骤"
43
-    
44
-    # 检查是否有未提交的commit但未推送
45
-    LOCAL_COMMITS=$(git log @{u}..HEAD --oneline 2>/dev/null | wc -l)
46
-    if [ $LOCAL_COMMITS -eq 0 ]; then
47
-        echo "错误: 没有需要推送的提交。"
48
-        exit 1
49
-    else
50
-        echo "检测到 $LOCAL_COMMITS 个本地提交等待推送"
51
-    fi
52
-fi
22
+# 推送标签
23
+git push origin "$TAG_VERSION"
53 24
 
54
-# 检查标签是否已存在
55
-#if git rev-parse "$VERSION_TAG" >/dev/null 2>&1; then
56
- #   echo "错误: 标签 '$VERSION_TAG' 已经存在。"
57
-  #  exit 1
58
-#fi
59
-
60
-# 创建标签
61
-#git tag "$VERSION_TAG"
62
-#if [ $? -ne 0 ]; then
63
-#    echo "错误: 创建标签失败。"
64
-#    exit 1
65
-#fi
66
-#echo "✅ 标签 '$VERSION_TAG' 已创建"
67
-
68
-# 推送到远程仓库并推送标签
69
-echo "正在推送到远程仓库..."
70
-git push
71
-if [ $? -ne 0 ]; then
72
-    echo "错误: 推送提交失败。"
73
-    exit 1
74
-fi
75
-
76
-#git push origin "$VERSION_TAG"
77
-#if [ $? -ne 0 ]; then
78
-#    echo "错误: 推送标签失败。"
79
-#    exit 1
80
-#fi
81
-
82
-#echo "✅ 完成!提交已推送,版本标签 $VERSION_TAG 已创建并推送。"
25
+echo "完成!"

Loading…
Avbryt
Spara