Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

액터는 양방향 메시지를 주고받을수 있는 객체이며 하나의 장비에 수십만객체 생성이가능합니다.

네트워크에 있어서도 체계적이고 효과적인 패킷전송을위해 라우팅설계가 중요하며

액터를 사용하는 분산 어플리케이션에서도 이러한 전략이 필요하며 단순하게 사용이 가능합니다.


라우팅전략

Expand
titleConcept > 라우팅전략

Include Page
라우팅 전략
라우팅 전략



심플 라우팅 워커 설계

draw.io Diagram
bordertrue
viewerToolbartrue
fitWindowfalse
diagramNamewokersactor
simpleViewerfalse
width
diagramWidth613
revision2


Wokers구현

Code Block
languagebash
themeEmacs
titleapplication.conf
akka { 
 actor.deployment {
 /workers/router1 {
 router = round-robin-pool
 nr-of-instances = 5
 }
 } 
}

코드의 변경없이 random-pool,balancing-pool 등 

원하는 분배방식을 설정으로 통해 결정할수가 있습니다.


Code Block
languagejava
themeEmacs
@Component
@Scope("prototype")
public class Workers extends AbstractActor {
    private final LoggingAdapter log = Logging
            .getLogger(getContext().system(), "Workers");
    
    @Autowired
    private ApplicationContext context;    
    
    private ActorRef roundrobinpool = null;
        
    @Override public void preStart() {
		log.info("Workers::preStart");
		SpringExtension ext = context.getBean(SpringExtension.class);		


        //application.conf에 설정된 라우터방식으로 작동방식결정됨
		roundrobinpool = getContext().actorOf( FromConfig.getInstance().props( ext.props("testActor") ) , "router1");
	}
	
	@Override
	public Receive createReceive() {
		return receiveBuilder()
		.matchEquals("hi1",s->{  //hi1 메시지를 받으면 라운드로빈처리를함
			log.info("Workers::Received String message: {}", s);
			roundrobinpool.tell(s, getSender() );	// 입력된 잡(hi1)을 라운드 로빈라우터에게 메시지전송				
		})
		.matchAny(o -> log.info("received unknown message"))
		.build();
	}
}


테스트코드

Code Block
languagejava
themeEmacs
	protected void routerTest(ActorSystem system,SpringExtension ext) {
	    new TestKit(system) {{
	    	final ActorRef workers = system.actorOf( ext.props("workers"),"workers");  // workers 액터생성	    		    
	    	workers.tell( "hi1", getRef() );
		      // await the correct response
		    expectMsg(java.time.Duration.ofSeconds(1), "hi too");
		    
	    	workers.tell( "hi1", getRef() );
		      // await the correct response
		    expectMsg(java.time.Duration.ofSeconds(1), "hi too");					    
	    }};
	}
Expand
title수행결과

[INFO] [05/17/2018 20:14:32.578] [AkkaTestApp-akka.actor.default-dispatcher-4] [Workers] Workers::preStart
[INFO] [05/17/2018 20:14:32.590] [AkkaTestApp-akka.actor.default-dispatcher-4] [Workers] Workers::Received String message: hi
[INFO] [05/17/2018 20:14:32.596] [AkkaTestApp-akka.actor.default-dispatcher-3] [TestActor] akka://AkkaTestApp/user/workers/router1/$a::Incommessage hi
[INFO] [05/17/2018 20:14:32.603] [AkkaTestApp-akka.actor.default-dispatcher-3] [Workers] Workers::Received String message: hi
[INFO] [05/17/2018 20:14:32.603] [AkkaTestApp-akka.actor.default-dispatcher-6] [TestActor] akka://AkkaTestApp/user/workers/router1/$b::Incommessage hi