我在 docker 上启动了 RabbitMq。这是
docker ps
的输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93dcba1c9b74 rabbitmq:3 "docker-entrypoint.s…" About a minute ago Up 9 seconds 4369/tcp, 5671-5672/tcp, 25672/tcp rabbitTestQueue
然后我使用了op-rabbit的Scala代码,并在application.conf中进行了以下配置:
-rabbit {
topic-exchange-name = "rabbitTestQueue"
channel-dispatcher = "op-rabbit.default-channel-dispatcher"
default-channel-dispatcher {
type = Dispatcher
executor = "fork-join-executor"
fork-join-executor {
parallelism-min = 2
parallelism-factor = 2.0
parallelism-max = 4
}
throughput = 100
}
connection {
virtual-host = "/"
hosts = ["rabbitTestHost"]
#username = "guest"
#password = "guest"
port = 5672
ssl = false
connection-timeout = 3s
}
}
这是我的代码:
import akka.actor.{ActorSystem, Props}
import com.spingo.op_rabbit._
import scala.concurrent.ExecutionContext
import play.api.libs.json._
case class Query(query: String)
object MainApp extends App {
import PlayJsonSupport._
implicit val actorSystem = ActorSystem("KaliumApp")
implicit val dataFormat = Json.format[Query]
val rabbitControl = actorSystem.actorOf(Props(new RabbitControl))
implicit val recoveryStrategy = RecoveryStrategy.nack(false)
import ExecutionContext.Implicits.global
val queryQueue = Queue("rabbitTestQueue", durable = false, autoDelete = true)
val subscription = Subscription.run(rabbitControl) {
import Directives._
channel(qos=3) {
consume(queryQueue) {
body(as[Query]) { queryObj =>
println(s"received ${queryObj.query}")
ack
}
}
}
}
}
当我运行代码时,出现以下错误:
akka://MainApp/user/$a/connection in Disconnected received Connect: can't connect to amqp://guest@{rabbitTestHost:5672}:5672//, retrying in 10 seconds
如何解决这个问题?我应该更改
application.conf
中的某些配置吗?
amqp://guest@{rabbitTestHost:5672}:5672//
5672 在这里没有重复,它只是 op-rabbit 打印连接日志的方式。
基于此消息:
Connect: can't connect to amqp://guest@{rabbitTestHost:5672}:5672//
看起来
["rabbitTestHost"]
的值是 rabbitTestHost:5672
,而它应该只是 rabbitTestHost
。请注意,连接消息中重复了 5672
。