Elixir & Phoenix: Scalable Backend Development · บทเรียน

Elixir แบบกระจายและการจัดกลุ่ม

เชื่อมต่อโหนด Elixir หลายโหนดเพื่อสร้างคลัสเตอร์ที่รองรับการสื่อสารระหว่างกระบวนการและการแบ่งปันทรัพยากรแบบกระจาย

บทเรียน 1 จาก 411 ขั้นตอน

Elixir แบบกระจายและการจัดกลุ่ม เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Distribute Elixir?

Welcome to distributed Elixir! This powerful feature allows your applications to run across multiple machines, forming a cluster of interconnected nodes.

Why is this important? It provides:

  • Fault Tolerance: If one node fails, others can continue or take over.
  • Scalability: Distribute work across many CPUs or servers.
  • Concurrency: Manage many concurrent operations with isolated processes.

This capability is built upon the Erlang Virtual Machine (BEAM), which Elixir runs on.

What is an Elixir Node?

In Elixir, a 'node' is a running instance of the Erlang VM, typically hosting your Elixir application. When you start iex, you're starting a single Elixir node.

For distributed systems, nodes need to be able to find and communicate with each other. This is done by giving them unique names.

Naming Your Node

To enable communication, each node must have a name. You can start iex with a name using either --sname (short name) or --name (full name).

  • --sname: Used for nodes on the same host (e.g., node1@localhost).
  • --name: Used for nodes across different hosts (e.g., node1@your_server_ip).

For this lesson, we'll use --sname as we'll simulate nodes on your local machine.

iex --sname node1 --setcookie mysecret
# In another terminal:
iex --sname node2 --setcookie mysecret

Secure Node Connections: The Cookie

Before nodes can connect, they need to authenticate each other using a 'magic cookie'. This is a shared secret string.

If two nodes have different cookies, they will not be able to connect. This is a vital security feature to prevent unauthorized nodes from joining your cluster.

You set the cookie when starting iex using the --setcookie flag, as shown in the previous example.

Making Nodes Talk: Connect

Once you have two named iex sessions running with the same cookie, you can connect them using Node.connect/1. The argument is the full atom name of the target node.

Try running this in your node1 IEx session:

# In node1's IEx session:
Node.connect(:'node2@localhost')

# This attempts to connect to 'node2@localhost'.
# Make sure 'node2' is running with a matching cookie!
IO.puts("Connected to node2: #{Node.connected?(:'node2@localhost')}")

Checking Node Status

After attempting a connection, you can verify which nodes are connected to your current node using Node.list/0. You can also get the name of your current node with Node.self/0.

Run this in any connected IEx session:

IO.puts("My node: #{inspect(Node.self())}")
IO.puts("Connected nodes: #{inspect(Node.list())}")

# If node1 and node2 are connected,
# Node.list() on node1 would show [:'node2@localhost']

Messaging Across Nodes

Once nodes are connected, processes on different nodes can send messages to each other just like local processes. You just need the remote process's PID.

Let's see a process on node2 spawn, then send a message back to node1. Run the first part in node2, then the second in node1.

# In node2's IEx session:
pid_on_node2 = spawn(fn ->
  send(self(), {:hello_from_node2, self()})
  receive do
    {:reply, msg} -> IO.puts("Node2 received reply: #{msg}")
  end
end)
IO.puts("Node2 process ready: #{inspect(pid_on_node2)}")

# In node1's IEx session:
# (assuming node1 received the pid from node2)
receive do
  {:hello_from_node2, pid_on_node2} ->
    IO.puts("Received from Node2: #{inspect(pid_on_node2)}")
    send(pid_on_node2, {:reply, "Thanks, Node2!"})
  after 1000 ->
    IO.puts("No message received from Node2 yet.")
end

Starting Processes Remotely

You can also explicitly start a new process on a remote node using Node.spawn_link/2 (or Node.spawn/2). This is a fundamental way to distribute work across your cluster.

The first argument is the remote node's name, and the second is a fun (anonymous function) to execute on that node.

# In node1's IEx session:
remote_process_pid = Node.spawn_link(:'node2@localhost', fn ->
  IO.puts("I am a process running on #{inspect(Node.self())}!")
  Process.sleep(2000) # Simulate work
  IO.puts("Process on Node2 finished.")
end)
IO.puts("Spawned process on Node2: #{inspect(remote_process_pid)}")

Global Names for Global Access

For processes to be easily found by name across all connected nodes, you can use :global.register_name/2. This makes a process globally discoverable by its registered name, regardless of which node it's on.

Contrast this with Process.register/2, which only registers a name locally on a single node.

# In node2's IEx session:
:global.register_name(:my_global_worker, self())
IO.puts("Node2 registered :my_global_worker: #{inspect(self())}")

# In node1's IEx session:
# Give a moment for registration to propagate
Process.sleep(100)
worker_pid = :global.whereis_name(:my_global_worker)
IO.puts("Found global worker from Node1: #{inspect(worker_pid)}")

# Now you can send messages to it from Node1
if worker_pid do
  send(worker_pid, "Hello from Node1 to global worker!")
end

Distributed Concepts Check

Time for a quick check on what you've learned about connecting Elixir nodes!

Distributed Elixir Recap

Great job! In this lesson, you've learned the fundamentals of distributed Elixir:

  • What Elixir nodes are and why they are named.
  • The importance of the 'magic cookie' for secure connections.
  • How to connect nodes using Node.connect/1.
  • How to check connected nodes with Node.list/0.
  • Sending messages and spawning processes across different nodes.
  • Using :global.register_name/2 for cluster-wide process discovery.

These concepts are the building blocks for creating highly available and scalable Elixir applications!

เริ่มต้นได้ฟรี

เรียนรู้ Elixir ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “Elixir แบบกระจายและการจัดกลุ่ม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Elixir แบบกระจายและการจัดกลุ่ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Elixir แบบกระจายและการจัดกลุ่ม”

เชื่อมต่อโหนด Elixir หลายโหนดเพื่อสร้างคลัสเตอร์ที่รองรับการสื่อสารระหว่างกระบวนการและการแบ่งปันทรัพยากรแบบกระจาย คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “Elixir แบบกระจายและการจัดกลุ่ม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Elixir แบบกระจายและการจัดกลุ่ม
  2. กลยุทธ์ซูเปอร์ไวเซอร์ขั้นสูง
  3. ซูเปอร์ไวเซอร์แบบไดนามิกและ Registry
  4. GenStage และสายการประมวลผลแบบควบคุมแรงดันย้อนกลับ
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development