0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · 강의

원격 프로시저 호출(RPC)

RPC를 사용하여 원격 노드에서 함수를 실행하고 클러스터 전반에서 분산 계산과 서비스 호출을 가능하게 합니다.

원격 프로시저 호출(RPC)은(는) CoddyKit의 무료 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What is Remote Procedure Call?

Imagine you have two Erlang programs running on different machines, or even on the same machine but in separate Erlang "nodes." How do they talk?

Remote Procedure Call (RPC) is a way for one program (the client) to ask another program (the server) to execute a function, just like it would call a local function. The client doesn't need to know the details of network communication.

Erlang's Built-in RPC Module

Erlang makes distributed computing easy with its built-in rpc module. This module provides functions to call or cast computations on remote Erlang nodes.

  • rpc:call/4: For synchronous calls, meaning the client waits for a reply.
  • rpc:cast/4: For asynchronous calls, where the client doesn't wait for a reply.

These are powerful tools for building distributed applications.

Setting Up for RPC

Before we can use RPC, we need at least two Erlang nodes connected. Each node needs a name and a shared cookie for authentication.

Open two separate terminal windows and start Erlang nodes like this:

  • Node 1: erl -sname server_node -setcookie mysecret
  • Node 2: erl -sname client_node -setcookie mysecret

Make sure they can ping each other: net_adm:ping(server_node@yourhostname).

Synchronous RPC with `rpc:call/4`

The rpc:call/4 function is used when you need an immediate answer from the remote node. It takes four arguments:

  • Node: The name of the remote Erlang node.
  • Module: The module on the remote node where the function resides.
  • Function: The name of the function to execute.
  • Args: A list of arguments for the function.

The calling process will block until it receives a reply or an error.

RPC Call Example: Server Module

Let's create a simple module math_server.erl. Save this code and compile it on server_node:

1. Save as math_server.erl

2. Compile on server_node: c(math_server).

-module(math_server).
-export([add/2, multiply/2]).

add(A, B) ->
    A + B.

multiply(A, B) ->
    A * B.

RPC Call Example: Client Interaction

Now, from client_node, we can call the math_server:add/2 function remotely! Remember to replace yourhostname with your actual machine's hostname.

Type this into client_node's Erlang shell:

rpc:call(server_node@yourhostname, math_server, add, [10, 5]).

Understanding RPC Results

When you execute rpc:call/4, Erlang handles the network communication for you. The result of the remote function call is returned directly to the calling process.

If the remote node doesn't exist, the module isn't loaded, or the function crashes, rpc:call/4 will return an error tuple like {badrpc, Reason}. This helps you manage potential issues in your distributed system.

Asynchronous RPC with `rpc:cast/4`

Sometimes you don't need an immediate response from a remote call. You just want to send a request and continue your work. This is where rpc:cast/4 comes in.

rpc:cast/4 works similarly to rpc:call/4 but it's a "fire and forget" operation. The client sends the message and does not wait for a reply, making it non-blocking.

RPC Cast Example: Logger Module

Let's create a simple logger module. Save this as logger.erl and compile it on server_node:

1. Save as logger.erl

2. Compile on server_node: c(logger).

-module(logger).
-export([log_message/1]).

log_message(Message) ->
    io:format("~p: Remote log: ~p~n", [self(), Message]).

RPC Cast Example: Client Interaction

Now, from client_node, use rpc:cast/4 to send a log message to server_node. Notice that client_node doesn't wait for a response.

Type this into client_node's Erlang shell (replace yourhostname):

You'll see the log message appear on server_node's console!

rpc:cast(server_node@yourhostname, logger, log_message, ["Hello from client!"]).

Quick RPC Check

You've learned about synchronous and asynchronous remote procedure calls in Erlang.

Recap: RPC for Distributed Erlang

In this lesson, you explored how Erlang's built-in rpc module enables seamless function execution across different nodes.

  • We learned about rpc:call/4 for synchronous, blocking calls when a result is needed.
  • We also covered rpc:cast/4 for asynchronous, non-blocking calls when no immediate reply is required.
  • Setting up connected nodes with shared cookies is crucial for RPC to work.

RPC simplifies distributed programming, allowing you to build applications that leverage multiple Erlang nodes easily.

자주 묻는 질문

“원격 프로시저 호출(RPC)” 강의는 무료인가요?

네 — “원격 프로시저 호출(RPC)” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“원격 프로시저 호출(RPC)”에서 뭘 배우나요?

RPC를 사용하여 원격 노드에서 함수를 실행하고 클러스터 전반에서 분산 계산과 서비스 호출을 가능하게 합니다. 브라우저에서 직접 실행하는 실습 코드로 Erlang OTP: Distributed & Fault-Tolerant Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Erlang OTP: Distributed & Fault-Tolerant Systems Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Erlang OTP: Distributed & Fault-Tolerant Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“원격 프로시저 호출(RPC)” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 노드 통신 및 설정
  2. 원격 프로시저 호출(RPC)
  3. 전역 프로세스 등록
  4. 분산 보안과 쿠키
← Erlang OTP: Distributed & Fault-Tolerant Systems Programming(으)로 돌아가기