远程过程调用(RPC)
使用 RPC 在远程节点上执行函数,从而支持集群中的分布式计算和服务调用。
远程过程调用(RPC) 是 CoddyKit 上的免费 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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/4for synchronous, blocking calls when a result is needed. - We also covered
rpc:cast/4for 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)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。
「远程过程调用(RPC)」这节课中我会学到什么?
使用 RPC 在远程节点上执行函数,从而支持集群中的分布式计算和服务调用。 你通过在浏览器中直接运行的动手代码来练习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「远程过程调用(RPC)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课中编写并运行代码吗?
能。每节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 节点通信与配置
- 远程过程调用(RPC)
- 全局进程注册
- 分布式安全与 Cookie