0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · 课时

节点通信与配置

学习设置并连接多个 Erlang 节点,了解底层分布式协议和安全机制。

节点通信与配置 是 CoddyKit 上的免费 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Welcome to Distributed Erlang

Erlang shines in building distributed systems! It allows multiple Erlang Virtual Machines (VMs), called nodes, to communicate seamlessly.

This lesson introduces how to set up and connect these nodes, forming the foundation for fault-tolerant and scalable applications.

Understanding Erlang Node Names

Every Erlang node needs a unique name to identify itself in a distributed system. There are two types of names:

  • Short names (e.g., mynode): Used for nodes on the same local machine or network segment. Started with -sname.
  • Long names (e.g., node1@example.com): Essential for nodes distributed across different hosts and networks. Requires a fully qualified domain name (FQDN) and started with -name.

We'll primarily use short names for local examples.

The Erlang Cookie: Shared Secret

For two Erlang nodes to communicate, they must share a common secret key called the Erlang cookie. Think of it like a password for node-to-node authentication.

  • If nodes have different cookies, they cannot connect.
  • The cookie is a string (e.g., mysecretcookie).
  • It's crucial for security: keep your cookies secret and generate strong ones!

Starting Your First Node

Let's start an Erlang node with a short name and a cookie. Open your terminal and run:

erl -sname alpha -setcookie myappsecret

Once the Erlang shell loads, you can check your node's name and cookie:

  • node(). (shows the node's full name)
  • erlang:get_cookie(). (shows the cookie)

EPMD: The Port Mapper Daemon

When an Erlang node starts, it registers itself with a local process called EPMD (Erlang Port Mapper Daemon).

EPMD's job is to keep track of which Erlang nodes are running on the local host and which TCP ports they are listening on. When one node wants to connect to another on the same machine, it asks EPMD for the target node's port number.

You can see registered nodes with epmd -names in a separate terminal.

Connecting Two Local Nodes

Now, start a second node in another terminal. Make sure to use the same cookie:

erl -sname beta -setcookie myappsecret

From the alpha node's shell, try to ping beta:

net_adm:ping(beta@~s).

Replace ~s with your machine's hostname (e.g., net_adm:ping(beta@localhost).). If it returns pong, they are connected! You can also use nodes(). to see connected nodes.

Demonstrating Node Identity

This simple Erlang module helps identify the current node. It's a full program that reports its own node name.

Save this as node_id.erl, compile it in your Erlang shell (c(node_id).), and then call node_id:print_name(). to see the output.

-module(node_id).
-export([print_name/0]).

print_name() ->
    io:format("Current node name: ~p~n", [node()]).

Long Node Names for Remote Hosts

For connecting nodes across different physical machines or networks, you must use long node names with the -name flag. This requires using the node's fully qualified domain name (FQDN) or IP address.

Example: erl -name node1@server.example.com -setcookie myappsecret

Using long names ensures that nodes can be uniquely identified and reached over a wider network.

Basic Security Considerations

While the Erlang cookie provides basic authentication, consider these points:

  • Strong Cookies: Use long, random strings for production environments.
  • Firewall Rules: Configure firewalls to allow EPMD (port 4369) and the dynamic Erlang node ports to communicate only between trusted hosts.
  • TLS: For sensitive data or untrusted networks, use Transport Layer Security (TLS) for encrypted communication (covered in a later lesson).

Quick Check on Node Setup

You've learned about Erlang nodes, naming, cookies, and EPMD. Let's test your understanding!

Recap: Node Communication Fundamentals

You've taken the first step into distributed Erlang! We covered:

  • Erlang Nodes: Separate Erlang VMs that can communicate.
  • Node Names: -sname for local, -name for remote (FQDN).
  • Erlang Cookie: A shared secret for node authentication.
  • EPMD: The daemon for local node discovery.
  • Connecting Nodes: Using net_adm:ping/1 to establish communication.

Next, we'll explore how these connected nodes can actually talk to each other!

常见问题解答

「节点通信与配置」课时是免费的吗?

是的 — 「节点通信与配置」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

「节点通信与配置」这节课中我会学到什么?

学习设置并连接多个 Erlang 节点,了解底层分布式协议和安全机制。 你通过在浏览器中直接运行的动手代码来练习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「节点通信与配置」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课中编写并运行代码吗?

能。每节 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 节点通信与配置
  2. 远程过程调用(RPC)
  3. 全局进程注册
  4. 分布式安全与 Cookie
← 返回 Erlang OTP: Distributed & Fault-Tolerant Systems Programming