Erlang OTP: Distributed & Fault-Tolerant Systems Programming · บทเรียน

การลงทะเบียนชื่อโพรเซสส่วนกลาง

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

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

การลงทะเบียนชื่อโพรเซสส่วนกลาง เป็นบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

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

Global Names Across Nodes

In distributed Erlang, processes often need to find each other, even if they're on different machines. How do you find a process when you don't know which node it lives on?

Erlang's global module provides a solution: global process registration. It allows you to give a process a unique name that is known across all connected nodes in your cluster.

Local vs. Global Processes

Normally, when you register a process using register/2, its name is only known on the local node. If my_process is on nodeA@host, a process on nodeB@host can't find it just by its name.

Global registration solves this! It acts like a distributed directory service. Any process on any connected node can look up a globally registered name and get the corresponding Process ID (PID), regardless of where that process is running.

`global:register_name/2`

To make a process accessible globally, you use global:register_name(Name, Pid).

  • Name: An atom that will be the unique global name (e.g., my_service).
  • Pid: The Process ID of the process you want to register.

If the name is already taken, register_name returns {false, OldPid}, otherwise true. The global module ensures that a global name is unique across the entire distributed cluster.

`global:whereis_name/1`

Once a process is registered, any other process can find it using global:whereis_name(Name).

  • Name: The global name (atom) you're looking for.

This function returns the PID of the registered process if found, or the atom undefined if no process is registered under that name.

It's crucial to check for undefined before attempting to send messages to the returned PID.

Full Global Communication Demo

This runnable example brings it all together! A server process starts and registers itself globally. After a short delay, a client process then finds this global server and sends it a message, receiving a reply.

Observe how global:register_name ensures the server is known, and global:whereis_name allows the client to locate it, enabling seamless communication.

-module(global_demo).
-export([main/0]).

% Server Functions
server_init() ->
    case global:register_name(my_global_service, self()) of
        true ->
            io:format("Server (~p) registered as my_global_service~n", [self()]),
            server_loop();
        {false, OldPid} ->
            io:format("Error: my_global_service already registered by ~p~n", [OldPid])
    end.

server_loop() ->
    receive
        {From, hello} ->
            io:format("Server (~p) received 'hello' from ~p~n", [self(), From]),
            From ! {self(), "Hi from server!"},
            server_loop();
        _ ->
            io:format("Server (~p) received unknown message~n", [self()]),
            server_loop()
    end.

% Client Functions
client_task() ->
    io:format("Client (~p) trying to find my_global_service...~n", [self()]),
    case global:whereis_name(my_global_service) of
        undefined ->
            io:format("Client: my_global_service not found!~n");
        ServerPid ->
            io:format("Client: Found server ~p, sending 'hello'...~n", [ServerPid]),
            ServerPid ! {self(), hello},
            receive
                {ServerPid, Response} ->
                    io:format("Client: Received response from server: '~s'~n", [Response])
            after 5000 ->
                io:format("Client: No response from server within 5 seconds.~n")
            end
    end.

% Main entry point
main() ->
    % Start the server process
    spawn(?MODULE, server_init, []),
    timer:sleep(100), % Give server time to register

    % Start the client process
    spawn(?MODULE, client_task, []),

    timer:sleep(2000). % Allow processes to run and communicate

Removing Global Names

When a globally registered process terminates, the global module automatically unregisters its name. However, you can also explicitly unregister a name using global:unregister_name(Name).

This is useful if you want to replace a service, or if a process needs to temporarily relinquish its global name. Remember, once unregistered, other processes can no longer find it by that name.

Handling Name Collisions

What happens if two different processes (even on different nodes) try to register the same global name?

The global module ensures that a global name is unique across the entire distributed system. The first process to successfully register the name wins. Subsequent attempts to register the same name will fail and return {false, OldPid}, indicating who already holds the name.

This prevents ambiguity and ensures that whereis_name/1 always returns a single, correct PID.

Practical Applications

Global process registration is ideal for implementing:

  • Singleton Services: A single instance of a service (e.g., a configuration manager, a logger) accessible from anywhere.
  • Resource Managers: A process responsible for managing a shared resource that multiple parts of your distributed system need to access.
  • Entry Points: Providing a well-known name for the main entry point of a distributed application.

It simplifies process discovery in complex distributed architectures.

How Global Registration Works

The global module achieves its magic by maintaining a consistent view of registered names across all connected Erlang nodes.

When a name is registered, this information is broadcast to all other nodes. When a node connects or disconnects, the global module updates its internal state to reflect the current cluster topology and available global names.

This ensures that global:whereis_name/1 can quickly find the correct PID, no matter where it resides.

Global Registration Check

Consider a distributed Erlang system with two nodes, nodeA and nodeB, both connected. A process on nodeA successfully registers itself with global:register_name(my_service, self()).

What will happen if a process on nodeB then tries to call global:register_name(my_service, self())?

Global Names: A Distributed Directory

In this lesson, you learned about global process registration using Erlang's global module:

  • It provides a way to assign unique names to processes that are discoverable across an entire distributed Erlang cluster.
  • global:register_name(Name, Pid) makes a process globally accessible.
  • global:whereis_name(Name) allows any process on any connected node to find the PID associated with a global name.
  • The global module handles name uniqueness and automatically unregisters processes when they terminate, simplifying distributed process management.

Global names are fundamental for building robust, fault-tolerant, and discoverable services in distributed Erlang applications.

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

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

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

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

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

บทเรียน “การลงทะเบียนชื่อโพรเซสส่วนกลาง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การลงทะเบียนชื่อโพรเซสส่วนกลาง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Erlang OTP: Distributed & Fault-Tolerant Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การลงทะเบียนชื่อโพรเซสส่วนกลาง” ใช้เวลานานแค่ไหน

บทเรียน 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