如果我有一个演员代表购物车:
class ShoppingCartActor extends PersistentActor with ActorLogging {
override def persistenceId: String = "shopping-cart-actor"
override def receiveCommand: Receive = ???
override def receiveRecover: Receive = ???
}
val system = ActorSystem("PersistenceActors")
for(i <- 1 to 100) {
val carts = system.actorOf(Props[ShoppingCartActor], s"shopping-cart-actor-$i"
}
如果我为电子商务商店的每个访客创建一个新的购物车参与者,那么所有事件消息都将存储在存储引擎的同一日记/表中吗?例如postgres或cassandra
如果日记/表是相同的,那么它将如何为正确的购物车演员重新加载事件,因为每个访客/客户都将拥有自己的购物车演员。
答案 0 :(得分:1)
persistenceId
对于每个PersistentActor
应该是唯一的。这就是持久性如何检索每个特定参与者的事件的方式(在postgres或cassandra中,它将存储在其自己的列中)。所以你想要类似的东西
class ShoppingCartActor(cartNum: Int) extends PersistentActor with ActorLogging {
override def persistenceId: String = s"shopping-cart:$cartNum"
override def receiveCommand: Receive = ???
override def receiveRecover: Receive = ???
}
object ShoppingCartActor {
def props(cartId: String): Props[ShoppingCartActor] =
Props(new ShoppingCartActor(cartId))
}
然后:
val system = ActorSystem(???)
val carts = (1 to 100).map { cartNum =>
cartNum -> system.actorOf(ShoppingCartActor.props(cartNum), s"shopping-cart-actor-$i")
}.toMap
编辑以澄清:
事件日志的Postgres模式为:
CREATE TABLE IF NOT EXISTS journal (
ordering BIGSERIAL,
persistence_id VARCHAR(255) NOT NULL,
sequence_number BIGINT NOT NULL,
deleted BOOLEAN DEFAULT FALSE NOT NULL,
tags VARCHAR(255) DEFAULT NULL,
message BYTEA NOT NULL,
PRIMARY KEY(persistence_id, sequence_number)
)
即每个事件都是日志表中的一行,其对应于persistenceId
的列。 Akka Persistence Cassandra还使用一个日记表。