Erlang OTP: Distributed & Fault-Tolerant Systems Programming · Lekcja

Zdalne wywoływanie procedur (RPC)

Wykonuj funkcje na zdalnych węzłach za pomocą RPC, umożliwiając rozproszone obliczenia i wywoływanie usług w klastrze.

Lekcja 2 z 412 kroki

Zdalne wywoływanie procedur (RPC) to bezpłatna lekcja Erlang OTP: Distributed & Fault-Tolerant Systems Programming na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Erlang OTP: Distributed & Fault-Tolerant Systems Programming, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się Erlang dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Zdalne wywoływanie procedur (RPC)” jest bezpłatna?

Tak — pełny tekst „Zdalne wywoływanie procedur (RPC)” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Erlang OTP: Distributed & Fault-Tolerant Systems Programming, przejdź na CoddyKit PRO. Kurs Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera 4 lekcji w sumie.

Co nauczysz się w „Zdalne wywoływanie procedur (RPC)”?

Wykonuj funkcje na zdalnych węzłach za pomocą RPC, umożliwiając rozproszone obliczenia i wywoływanie usług w klastrze. Ćwiczysz Erlang OTP: Distributed & Fault-Tolerant Systems Programming z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

Nie wymagamy żadnego doświadczenia. Erlang OTP: Distributed & Fault-Tolerant Systems Programming w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Zdalne wywoływanie procedur (RPC)”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

Tak. Każda lekcja Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Konfiguracja i komunikacja węzłów
  2. Zdalne wywoływanie procedur (RPC)
  3. Globalna rejestracja procesów
  4. Bezpieczeństwo dystrybucji i cookies
← Powrót do Erlang OTP: Distributed & Fault-Tolerant Systems Programming