Akka集群经典分布式发布订阅无法跨微服务接收消息问题排查求助
Akka集群经典分布式发布订阅无法跨微服务接收消息问题排查求助
大家好,我最近在做Akka集群的跨微服务消息发布订阅,遇到了一个棘手的问题:我在微服务A里通过SenderActor使用Akka的**经典分布式发布订阅(Cluster Distributed Pub/Sub)**机制向content主题发送消息,但微服务B里的ReceiverActor始终收不到这条消息,想请各位帮忙分析下可能的原因。
以下是我的代码和配置细节:
微服务A相关配置与代码
application.conf配置
akka { actor { provider = "cluster" } remote { artery { enabled = on canonical { hostname = "localhost" port = 2551 } } } cluster { seed-nodes = [ "akka://cluster-system@localhost:2551" ] } }
SenderActor实现
public class SenderActor extends AbstractActor { ActorRef mediator = DistributedPubSub.get(getContext().system()).mediator(); @Override public Receive createReceive() { return receiveBuilder() .match( String.class, in -> { String out = in.toUpperCase(); mediator.tell(new DistributedPubSubMediator.Publish("content", out), getSelf()); }) .build(); } }
微服务A主方法
public static void main(String[] args) { SpringApplication.run(MicroserviceAaApplication.class, args); Config config = ConfigFactory.load("application.conf"); ActorSystem system = ActorSystem.create("cluster-system", config); Cluster cluster = Cluster.get(system); ActorRef publisher = system.actorOf(Props.create(SenderActor.class), "publisher"); publisher.tell("hello from microservice A ", null); }
微服务B相关配置与代码
application.conf配置
akka { actor { provider = "cluster" } remote { artery { enabled = on canonical { hostname = "localhost" port = 2552 } } } cluster { seed-nodes = [ "akka://cluster-system@localhost:2551" ] } }
ReceiverActor实现
public class ReceiverActor extends AbstractActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public ReceiverActor() { ActorRef mediator = DistributedPubSub.get(getContext().system()).mediator(); mediator.tell(new DistributedPubSubMediator.Subscribe("content", getSelf()), getSelf()); } @Override public Receive createReceive() { return receiveBuilder() .match(String.class, msg -> { log.info("Received message in receiver-actor-b: {}", msg); }) .match(DistributedPubSubMediator.SubscribeAck.class, msg -> log.info("SubscribeAck: {}", msg)) .build(); } }
微服务B主方法
public static void main(String[] args) { SpringApplication.run(MicroserviceBbApplication.class, args); Config config = ConfigFactory.load("application.conf"); ActorSystem system = ActorSystem.create("cluster-system", config); Cluster cluster = Cluster.get(system); ActorRef subscriber = system.actorOf(Props.create(ReceiverActor.class), "subscriber"); }
问题现象
我在微服务A里发送了消息hello from microservice A ,但微服务B的日志里只显示了订阅成功的确认信息,没有收到实际的消息内容。微服务B的日志如下:
2023-08-20T15:53:14.067+02:00 INFO 7788 --- [ main] c.e.M.MicroserviceBbApplication : Started MicroserviceBbApplication in 0.909 seconds (process running for 1.723) 2023-08-20T15:53:14.552+02:00 INFO 7788 --- [lt-dispatcher-5] akka.event.slf4j.Slf4jLogger : Slf4jLogger started 2023-08-20T15:53:14.900+02:00 INFO 7788 --- [lt-dispatcher-5] akka.remote.artery.ArteryTransport : Remoting started with transport [Artery tcp]; listening on address [akka://cluster-system@localhost:2552] with UID [6730186213375081245] 2023-08-20T15:53:14.925+02:00 INFO 7788 --- [lt-dispatcher-5] akka.cluster.Cluster : Cluster Node [akka://cluster-system@localhost:2552] - Starting up, Akka version [2.8.3] ... 2023-08-20T15:53:14.980+02:00 INFO 7788 --- [lt-dispatcher-5] akka.cluster.Cluster : Cluster Node [akka://cluster-system@localhost:2552] - Registered cluster JMX MBean [akka:type=Cluster] 2023-08-20T15:53:14.981+02:00 INFO 7788 --- [lt-dispatcher-5] akka.cluster.Cluster : Cluster Node [akka://cluster-system@localhost:2552] - Started up successfully 2023-08-20T15:53:15.006+02:00 INFO 7788 --- [t-dispatcher-13] akka.cluster.Cluster : Cluster Node [akka://cluster-system@localhost:2552] - No downing-provider-class configured, manual cluster downing required, see https://doc.akka.io/docs/akka/current/typed/cluster.html#downing 2023-08-20T15:53:15.624+02:00 INFO 7788 --- [t-dispatcher-13] akka.cluster.Cluster : Cluster Node [akka://cluster-system@localhost:2552] - Received InitJoinAck message from [Actor[akka://cluster-system@localhost:2551/system/cluster/core/daemon#1745370250]] to [akka://cluster-system@localhost:2552] 2023-08-20T15:53:15.675+02:00 INFO 7788 --- [t-dispatcher-13] akka.cluster.Cluster : Cluster Node [akka://cluster-system@localhost:2552] - Welcome from [akka://cluster-system@localhost:2551] 2023-08-20T15:53:16.077+02:00 INFO 7788 --- [lt-dispatcher-5] c.example.MicroserviceBB.ReceiverActor : SubscribeAck: SubscribeAck(Subscribe(content,None,Actor[akka://cluster-system/user/subscriber1#-339648796])) 2023-08-20T16:24:19.549+02:00 INFO 7788 --- [t-dispatcher-40] akka.actor.CoordinatedShutdown : Running CoordinatedShutdown with reason [JvmExitReason] Process finished with exit code 130
从日志能看到集群已经成功建立连接,订阅也确认了,但就是收不到消息,有没有大佬能帮我找找问题出在哪?
备注:内容来源于stack exchange,提问作者Bashir Hussein
相关产品推荐
相关产品推荐

