7.6 案例:异步并发分布式编程框架 akka

用户头像
张荣召
关注
发布于: 2020 年 11 月 09 日

7.6案例:异步并发分布式编程框架akka

1.Akka's vision

  • Simpler concrrency(scale up)---更简并发(垂直伸缩,更好利用CPU核心)

  • Simpler distribution(scale out)---更简分布式(水平伸缩)

  • Simpler fault-tolerance(self healing)---更简容错能力(自愈)

  • All of that with a single unified programming model.   -----统一编程模型实现:垂直伸缩,水平伸缩,容错。



      Scale up :

    Processes(even less***)

    Threads (4096/1G)

   Actors(2.7million/1G)    

       Actors are smaller.

2.The Akka toolkit

    Akka runs on the JVM

    Akka can be used from Java and Scala

    Akka can be integrated with common infrastructure,e.g. Spring,etc. 

3.Core concept:Actor

   Carl Hewitt(1973): Fundamental unit of computation(计算基本单元)

  • Behavior--react on messages it receives(基于消息响应)

  • State--shielded form the rest of the world ,no need for synchronization(改变状态,不需要同步阻塞)

  • Communication--interact with other actors exclusively via message (actor互相通信,完成交互)  

4.The Akka Actor

  线程栈方法调用对比:f() 调用g(),线程栈情况

  Actor并行执行:消息队列。消息入队,dispatcher取出消息,分发给Actor. Actor每次只处理一个消息。Actor之间并发执行。

  Dispatcher:线程池(上百线程可以支撑百万级Actor)。

5.Receive message

         One at a time

   

         public class Hello extends UntypedActor{

                 @Override

                 public void onReceive(Object message){

                       System.out.println("Hello,world");

                 }

         }

6.Send message

       Asynchronous and nonblocking



       ActorRef hello=...;

       hello.tell("hi");

7.ActorRef path

    Local: (本地同一个进程中,垂直伸缩)    

   Remote:(远程--水平伸缩:)

  • Akka.tcp://Master@sr148:2052/user/master           

8.The Akka Actor System

9.Create an actor system

   ActorSystem system=ActorSystem.create("hello");

10.Create a top-level actor

  ActorRef hello=system.actorOf(new Props(new Hello()));//创建根Actor

11.Create a child actor

  ActorRef child=context().actorOf(new Props(new Child()));

12.Router for cluster

   val   router=

      system.actorOf(

           Props[SomeActor].withRouter(

               RoundRobinRouter(nrOfInstances)=5);//5个实例,使用router负载均衡

13.Router for cluster

   Let it crash!

   Supervision:Like in read life,parents supervise their children (manage children's failures) 

   Depending on the parents supervisor strategy,failing actorss can get stopped,restarted or resumed.



  private SupervisorStrategy strategy=new OneForOneStrategy(

   10,

   Duration.parse("1 minute"),  

   new Function<Throwable,Directive>{

         @Override public Derective apply(Throwsable t){

               if(t instanceof ArithmeticException){ return resume();}//继续

              else if(t instanceof NullPointerException){ return restart();}//重启

              else   { return escalate();}//丢弃

         }

   }

); 



14.The HakkyHour bar

 Our drinks: Akkarita,MaiPlay,PinaScalada

 Our Actors:guests,waiters,head waiter,barkeepers.

 Our message:Order,drink served,complaint.

 Our failures:Guest drunk,waiter frustrated.



15.Benefits of using Akka actors

 You don't have to deal with concurreny details

 You can manage failures easily

 Distribution is just a deployment decision (not conered here)//部署决策,分布式高并发。



16.分布式Actor实现水平伸缩

 akka{

        actor {

                 deployment {//远程部署

                           "/creationActor/"{

                                 remote="akka.tcp://calculatorWorkerSystem@127.0.0.1:2552"

                            }

                 }

        }

 }



用户头像

张荣召

关注

还未添加个人签名 2018.05.02 加入

还未添加个人简介

评论

发布
暂无评论
7.6案例:异步并发分布式编程框架akka