Versions Compared

Key

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

Remote 는, 기존 작성했던 로컬에서 작동했던 액터를 그대로 재활용하여 , 액터설계의 코드변경없이

원격지에서 사용이 가능합니다. 어떻게 이게 가능한지 실습을 해보겠습니다. 이것은 아주 큰장점입니다. 대부분의 비동기처리에 있어서

로컬에서도 휼륭한 역활을 하지만 , 코드 변경없이 원격 배치가 가능합니다.

Panel
title설정
akka {
    actor {
        provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote"
    }

    remote {

helios.tcp {
port = 8001 #bound to a specific port
hostname = 127.0.0.1
}

    }
}

...

Code Block
languagec#
themeEmacs
title원격 처리를 위해 메시지정의 공용 DLL로 집합
linenumberstrue
    public class Hello
    {
        public Hello(string message)
        {
            Message = message;
        }

        public string Message { get; private set; }
    }

    public class EchoActorEchoActor2 : ReceiveActor
    {
        public EchoActorEchoActor2()
        {
            Receive<Hello>(hello =>
            {
                Console.WriteLine("[{0}]: {1}", Sender, hello.Message);
                Sender.Tell(hello);
        });
    }
}

public class Hello
{
    public Hello(string message)
    {
        Message = message;
);
        }

    public string Message { get; private set; }
}


전송에 사용될 Class는 공용 DLL에 정의 되어 , 공용 DLL에 위 내용을 추가한후

...

ServiceB가 ServiceA의 EcshoActor에게 메시지를 보낸 상황을 가정해보겠습니다.

Expand
title데이터 참조를위한 공통라이브러리추가

1.Data공용 ClassLib 프로젝트추가하기(DLL)

Image Added

2.프로젝트에서 CommonActor 프로젝트 참조추가

Image Added

Image Added


공용 DLL이 필요한 이유는 간단합니다. 원격에 있는 SeviceA , ServiceB가 동일하게

정의된 Data구조를 참조하여 같은의미로 해석하게 하기위함입니다.

Actor는 같이 사용되지 않으면 공유될 필요는 없습니다. 학습편의상 동일한 곳에

집합하고 다른 프로젝트에서 동일한 액터가 필요할때 공용액터를 정의할수 있으나

프로젝트마다 DLL의존성이 생기기때문에 신중해야합니다.

Data 설계는 의존성을 피할수없으며 변경된것을 해당 Data를 다른액터에게 전달할수 있지만,

해석을 못할수 있으니 유연한 데이터설계가 필요합니다.

내부적으로 지정된 JsonSerialization이 자동으로 작동이됩니다.

원격간 Data설계 변경에 따른 DLL의존성은 AKKA.net 에서 어떻게 해결을 할지

관련 자료를 좀더 찾아볼 예정입니다.

Code Block
languagec#
themeEmacs
titleServiceA
linenumberstrue
			using (ActorSystem actorSystem = ActorSystem.Create("ServiceA"))
            {
                IActorRef myActor = actorSystem.ActorOf<EchoActor>("myactor");
                //콘솔 종료 방지 코드 생략...
            } //port 8001

...

Code Block
languagec#
themeEmacs
titleServiceB
linenumberstrue
			using (ActorSystem actorSystem = ActorSystem.Create("ServiceB"))
            {
				//자신이 가진액터가 아닌, 원격지의 액터 선택이기 때문에 ActorSelection  사용합니다.
       IActorRef myActor         ActorSelection otherActor = actorSystem.ActorSelection("akka.tcp://ServiceA@127.0.0.1:8001/user/myactor");
				Hello msg = new Hello("Hello");
               myActor otherActor.Tell("Hello"msg);
                //콘솔 종료 방지 코드 생략...
            } //동일 컴퓨터이니 ServiceB의 설정포트를 8002로 변경

...

로컬에서 작동과 차이점은 주소체계를 앞부분에 넣은준것외에 다른점이 없습니다.


Warning
title액터 메시지 설계시 주의점

두 액터간 메시지를 주고 받을때, 인사(Hello)에 항상 반응 하는 두 액터가 있다고 가정해봅시다.

두 액터중 아무나 먼저 상대 액터에게 인사를 시작하면 두액터는 무한대로 인사를 주고 받을것입니다.

네트워크에서 발생할수 있는 무한루프이며, 네트워크 지연이없을수록 하드웨어 자원(CPU,메모리)를 빠르게 고갈시킬것입니다.

이러한 실수를 방지하기위해 전통적인 방법으로는 전송 오브젝트에대해 요청(Req)과 응답(Res)을 분리하여 네이밍을 할수 있습니다.