Nats无法为没有传递组的消费者创建队列订阅。

huangapple go评论68阅读模式
英文:

Nats cannot create a queue subscription for a consumer without a deliver group

问题

我正在尝试在一个队列中创建多个订阅者,以便每个消息只被其中一个订阅者读取。当我尝试第二次调用QueueSubscribe时,我会收到一个错误:cannot create a queue subscription for a consumer without a deliver group

我尝试在一个应用程序中创建两个订阅者,并尝试运行两个实例,但是出现了相同的错误。

以下是订阅者的代码:

s1, err = js.QueueSubscribe(
	"KEYS.group",
	"queue",
	func(m *nats.Msg) {
		fmt.Printf("1: %s\n", m.Data)
	},
)

s2, err = js.QueueSubscribe(
	"KEYS.group",
	"queue",
	func(m *nats.Msg) {
		fmt.Printf("2: %s\n", m.Data)
	},
)

以及发布者的代码:

_, err := js.AddStream(&nats.StreamConfig{
		Name:     "KEYS",
		Subjects: []string{"KEYS.group"},
		Retention: nats.WorkQueuePolicy,  // 如果可能的话,尝试使用默认策略
	})

for i := 0; i < 20; i++ {
		msg := fmt.Sprintf(`{"key": "%d"}`, i)

		if _, err := js.Publish("KEYS.group", []byte(msg)); err != nil {
			print(err)
		}
	}

我还尝试添加了Durable属性,但没有帮助。

英文:

I'm trying to create several subscribers in one queue so each message is read only by one of them. When I try to QueueSubscribe second time I get an error: cannot create a queue subscription for a consumer without a deliver group.

I've tried creating two subscribers in one app and tried running two instances as well, it's the same error.

Here're subscribers:

s1, err = js.QueueSubscribe(
		&quot;KEYS.group&quot;,
		&quot;queue&quot;,
		func(m *nats.Msg) {
			fmt.Printf(&quot;1: %s\n&quot;, m.Data)
		},
	)

s2, err = js.QueueSubscribe(
		&quot;KEYS.group&quot;,
		&quot;queue&quot;,
		func(m *nats.Msg) {
			fmt.Printf(&quot;2: %s\n&quot;, m.Data)
		},
	)

And publisher:

_, err := js.AddStream(&amp;nats.StreamConfig{
		Name:     &quot;KEYS&quot;,
		Subjects: []string{&quot;KEYS.group&quot;},
		Retention: nats.WorkQueuePolicy,  // tried with default policy if it may be relevant
	})

for i := 0; i &lt; 20; i++ {
		msg := fmt.Sprintf(`{&quot;key&quot;: &quot;%d&quot;}`, i)

		if _, err := js.Publish(&quot;KEYS.group&quot;, []byte(msg)); err != nil {
			print(err)
		}
	}

I've tried to add Durable attribute as well and it didn't help.

答案1

得分: 2

问题是由于nats服务器的版本问题(2.3.6)导致的,更新到2.7.2后一切正常。该库不兼容旧版本。

英文:

The problem occured because of the version of nats server (2.3.6), after update to 2.7.2 everything works fine. Library is not compatible with old versions.

huangapple
  • 本文由 发表于 2022年2月14日 20:15:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/71111758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定