7. Router routing mechanism in Akka

Basic Concepts of Router

In Akka, Router is also an actor type, which is responsible for the abstraction of load balancing and routing. It dispatches messages according to routing rules and assigns them to Router to perform operations for scheduling actor tasks or load balancing strategies.

It routes incoming messages to other actors, and those other actors are called routees (routed objects).

Create method

There are two ways to create Router

  1. Actor Group:Routees are generated by other Actors outside (self-created, self-managed), and are characterized by the flexible construction and monitoring of Routee.

    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);
    
  2. Actor Pool: The Router is responsible for constructing all the routers. The router is a child actor and will be removed from the router when the child actor terminates.

    ActorSystem actorSystem = ActorSystem.create("lpSys");
            ActorRef routerActorRef = actorSystem.actorOf(
                    Props.create(ArticleParseActor.class).withRouter(new RoundRobinPool(8)), //RoundRobinPool:表示依次往复循环发送
                    "lpRouterActor");
    

Routing strategy

Earlier we used RoundRobinPool/RoundRobinGroup to specify the order in which the Router sends messages to each Actor. Akka has some built-in routing strategies:

Routing strategy Features
Round Robin Send messages to each node in the Pool/Group in turn, cyclically. Random——Send messages to each node randomly.
Smallest Mailbox Send messages to the Actor that currently contains the least number of messages. Since the mailbox size of the remote actors is unknown, it is assumed that there are already messages in their queues. Therefore, the message will be sent to the idle local actor first.
Scatter Gather Send a message to all Actors in the Group/Pool, use the first response received, and discard any other responses received afterwards. If you need to ensure that you can receive a response as soon as possible, you can use scatter/gather.
Tail Chopping Similar to Scatter/Gather, but Router does not send a message to all Actors in the Group/Pool at once, but waits for a short period of time after sending a message to an Actor. It has similar advantages as Scatter/Gather, but in comparison, it is possible to reduce network load.
Consistent Hashing Provide a key to the Router, and the Router generates a hash value based on this key. Use this hash value to decide which node to send data to. When you want to send specific data to a specific destination, you can use hashing. In the next chapter, we will discuss more issues related to consistent hashing.
BalancingPool The routing strategy of BalancingPool is a bit special. Can only be used for local actors. Multiple actors share the same mailbox and process tasks in the mailbox as soon as they are free. This strategy can ensure that all actors are busy. For local clusters, this routing strategy is often preferred.

Send messages to routees

router.tell(new akka.routing.Broadcast(msg));

Supervise routees in Pool

If the Router is created using the Pool method, and the Router is responsible for creating the Actor, then these routing objects will become the child nodes of the Router. When creating the Router, you can provide the Router with a custom supervision strategy, and call the withSupervisorStrategy method to specify the Router's supervision strategy for the routing objects in the Pool.

ActorRefworkerRouter = system.actorOf(  
			Props.create(ArticleParseActor.class)  
					 .withRouter(new RoundRobinPool(8)
					 .withSupervisorStrategy(strategy))
			);

references

  • "Akka Introduction and Practice"

Follow the Akkaofficial account and reply to receive the "Akka Introduction and Practice" book.
Follow the official account and 数据工匠记focus on the offline and real-time technical dry goods in the big data field to share regularly! Personal website www.lllpan.top
Insert picture description here

Guess you like

Origin blog.csdn.net/lp284558195/article/details/112816363