Erlang 103 Erlang distributed programming.-Missing

Source: Internet
Author: User

Outline

Notes Series

Erlang Environment and sequential programming
Erlang Concurrent Programming
Erlang Distributed programming
Yaws
Erlang/otp

Date Change Description

2014-11-23 A Outline
A 1.1-1.2
2014-12-08 A 1.3

Agenda

0 Range

Nodes and Communications

Basic Distributed Programming Module

EMPD process

Socket programming

1 Erlang nodes and communication 1.1 nodes

An Erlang node is a named (named) running Erlang Runtime System (ERTs).

Multiple nodes can be run on a single machine or on different machines. The example in the previous sequential programming, concurrent programming is actually running on an Erlang node.

Surviving node/named node: If one node can communicate with other nodes. Any surviving nodes need to be named, and there are two ways of naming them:

(1) Short name Erl-sname <sname>

Sname name a host in the LAN, has [email protected] given, for example: [email protected]

(2) long name Erl-name <name>

Name gives the full host IP address, either [email protected], or [email protected] (DNS resolved hostname).

A long name node can only communicate with a long name node, and a short name node can only communicate with a short name node.

Node Startup and information

Erl-[s]name <nodeName>

Named node mode start

Net_kernel:start ([' [email protected] ').

Start node

Net_kernel:stop ().

Stop current node

Node ().

View current node

Elrang:is_alive ().

Whether the current node is alive

1.2 Node Communication

Connectivity testing of nodes

Net_adm:ping (' [email protected] ').

Pang: Not connected, pong: connected.

Cookies

Each node has only one cookie at any time, and nodes that share the same value can communicate.

Set Cookies at startup

Erl-sname Foo-setcookie <cookieValue>

Set cookies at run time

Erlang:set_cookie (node (), <cookieValue>).

Limitations

After a distributed node establishes a connection through a cookie with another remote node, the owner of the remote node obtains the same permissions as the local node's user. The remote node is able to perform spawn (Yournode, OS, cmd, ["Rm-rf *"]). Nobody wants to see it. Therefore, the cookie secure communication in the closed system is sufficient, but in the open system needs to integrate into other security communication mechanisms, can consider the security sockets and so on.

Connectivity Setup and Configuration

As long as distributed Erlang nodes share the same cookie value, they can communicate between them.

The Erlang Runtime System (ERTs) automatically establishes a connection when a node is referenced for the first time. Automatically establishing a connection can be done by calling NET_ADM:PING/1 or sending a message to the process on which the node is registered.

Connected nodes, information is shared and transitive.

Net_kernel process

The Net_kernel process for each node coordinates the operations between the distributed Erlang nodes, for example: SPAWN/4 will be converted by the Net_kernel process to the Net_kernel process that the message is sent to the remote node, net_ The kernel process handles cookie authentication. It's important to note that users can modify the Net_kernel process to get the desired behavior.

The overriding method for automatically establishing transitive connection behavior is to manually control the connection using the functions in the Net_kernel module, and to run Erl-connect_all false to deny the nodes global connectivity to each other.

Hide Node

You want to override the default configuration for all nodes to connect to each other, and only when necessary to establish a connection to other nodes, consider hiding the nodes.

Start the hidden node: Erl-sname ' NodeName '-hidden.

To manually establish a node connection: Net_kernel:connect (NodeName).

Nodes/0 does not return a hidden node, nodes (hidden) returns a hidden node, nodes (connected) returns a hidden node and a non-hidden node.

1.3 Differences between a remote procedure call and a local call

? SAMPLE:FACSERVER/FACCLIENT[1]

Facserver.erl

-Module(facserver).%%API-Export([start/0]).%%%===================================================================%%% API%%%===================================================================Start ()Register (Facserver, self ()), Facloop ().%%%===================================================================%%% Internal Functions%%%===================================================================Facloop ()Receive{Pid, N}-Pid!{OK, FAC (N)}End, Facloop (). FAC (0)-1; FAC (1)-1; FAC (N)-N * FAC (N-1).

Facclient.erl

-Module(facclient).%%API-Export([REMOTE_CALL/2]).%%%===================================================================%%% API%%%===================================================================Remote_call (Message, Node)-{facserver, Node}! {self (), Message},%? Send a message node remotely to a remote nodeReceive{OK, Result}-ResultEnd.%%%===================================================================%%% Internal Functions%%%===================================================================

Run the sample

A distributed system is a system that causes your computer to become unusable because of another error that you do not even know about its existence.

--lesilie Lamport

The essential difference between a remote procedure call and a local call is that the remote node may fail to invoke the desired functionality. Therefore, you need to add some "guard" statements at the time of the remote procedure call to ensure that the remote node is connected properly and the remote node fails to gracefully continue processing. [1] gives three common methods: Timeout (Receive ... after), connect to remote node process (SPAWN_LINK/4), monitor remote node process (Monitor_node (node, Bool)) For details, refer to the documentation published with the version of Erlang.

2 Basic Distributed Programming Module 2.1 ERL module

2.2 RPC Module

2.3 Erlang Module

2.4 Net_kernel Module

2.5 Net_adm Module

3 EMPD Process

4 Erlang Socket Programming

Reference documents

[1] Cesarini F., Thompson S., Munich ISAR working group CFRP confined translation.

Erlang Programming Guide .

Beijing: Mechanical industry Press. 2011.

[2] Armstrong J., into the translation of the cow.

Erlang Programming ( version 2 ). (Programming Erlang, Second edition–software for a Concurrent world).

Beijing: People's post and Telecommunications press. 2014.

Erlang 103 Erlang distributed programming.-Missing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.