Basic Concepts of Router
In Akka, Router is also an actor type, which is responsible for the abstraction of load balancing and routing. It schedules messages according to routing rules and assigns them to Routee for calculation, so as to schedule actor tasks or implement load balancing policies
It routes incoming messages to other actors, who are called routees.
Create a way
There are two ways to create a Router
-
Actor groups: Routees are generated (self-created, self-managed) by other external actors and feature flexible Routee builds and monitoring.
List<Routee> routees = new ArrayList<>(); for (int i = 0; i < 5; i++) { ActorRef r = getContext().actorOf(Props.create(ArticleParseActor.class)); getContext().watch(r); routees.add(new ActorRefRoutee(r)); } Router router = new Router(new RoundRobinRoutingLogic(), routees); Copy the code
-
Actor Pool: The Router is responsible for building all routees, which act as child actors and remove that child Actor from the Router when it terminates.
ActorSystem actorSystem = ActorSystem.create("lpSys"); ActorRef routerActorRef = actorSystem.actorOf( Props.create(ArticleParseActor.class).withRouter(new RoundRobinPool(8)), //RoundRobinPool: indicates that the packets are sent in turn "lpRouterActor"); Copy the code
Routing strategy
In the previous RoundRobinPool/RoundRobinGroup, we use is used to specify the Router sends a message to the order of the Actor. Akka has some built-in routing policies:
Routing strategy | function |
---|---|
Round Robin | Messages are sent to each node in the Pool/Group in turn, and the cycle repeats. Random – Sends messages randomly to each node. |
Smallest Mailbox | Send messages to the Actor that currently contains the least number of messages. Since the mailbox size of remote actors is unknown, it is assumed that they already have messages queued in their queues. So messages are preferentially sent to idle local actors. |
Scatter Gather | Messages are sent to all actors in the Group/Pool, using the first response received and discarding any other responses received thereafter. Use Scatter/Gather if you need to ensure that a response is received as quickly as possible. |
Tail Chopping | Similar to Scatter/Gather, the Router does not send a message to all actors in the Group/Pool at once, but instead waits for a short period of time after sending a message to each Actor. Has similar benefits to Scatter/Gather, but potentially reduces network load. |
Consistent Hashing | The Router is given a key from which it generates a hash. Use this hash value to determine which node to send data to. When you want to send specific data to a specific destination location, you can use a hash. In the next chapter, we’ll talk more about consistent hashing. |
BalancingPool | BalancingPool This routing policy is a bit special. Can only be used with local actors. Multiple actors share the same mailbox and handle tasks in the mailbox whenever they are free. This strategy ensures that all actors are busy. For local clusters, this routing policy is often preferred. |
Send a message to Routees
router.tell(new akka.routing.Broadcast(msg));
Copy the code
Monitor routees in the Pool
If a Router is created in Pool mode and the Router is responsible for creating actors, these routing objects become children of the Router. When creating a Router, you can provide it with a custom supervision policy. The withSupervisorStrategy method is called to specify the Router’s supervision policy for routing objects in the Pool.
ActorRefworkerRouter = system.actorOf(
Props.create(ArticleParseActor.class)
.withRouter(new RoundRobinPool(8)
.withSupervisorStrategy(strategy))
);
Copy the code
reference
- Introduction and Practice of Akka
Concern public number replyAkka
Get the “Akka Introduction and Practice” book attention public accountData craftsman notes
, focus on big data field offline, real-time technology dry goods regular sharing! Personal websitewww.lllpan.top