问题: 如何指定一个节点在启动后自动连接到别的节点上?

这个我们要使用到sys.config,这是erlang的配置文件,这个文件一般都是$ROOT/releases/Vsn下

1. 首先我们要先启动一个master节点,Node.list可以看到当前节点并没有连接到任何节点

iex --cookie secret --name master@127.0.0.1
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.2.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(master@127.0.0.1)1>Node.list
[]

2. 我们再启动一个slave节点,让它启动时直接连接到master上

新建sys.config

#sys.config
[{kernel,
[
{sync_nodes_optional, ['master@127.0.0.1']},
{sync_nodes_timeout, 150000}
]}
].

这个配置可以表明,启动后会主动连接到'master@127.0.0.1' ,如果没有连接成功会每150秒再重连。

iex --cookie secret --name slave@127.0.0.1 --erl "-config sys.config"
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.2.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(slave@127.0.0.1)1> Node.list
[:"master@127.0.0.1"]

这时在2个节点上用Node.list都可以看到对方啦。 注意: 一定要保持2个节点的cookie是一样的

再验证一下:

iex(slave@127.0.0.1)2>:rpc.call(:"master@127.0.0.1", :io, :format, [:user, "~ts pid: ~p~n", ["我是在master节点上运行的", self()]])
:ok
iex(slave@127.0.0.1)2>self
#PID<0.69.0>

这个format只是会在原运行节点上输出,因为使用了:user, 所以我们在master上看到结果

iex(master@127.0.0.1)2> 我是在master节点上运行的 pid: <9547.69.0>

可以看到master上输入的pid是slaver上的进程pid, 其中9547是标记它来自slaver@127.0.0.1节点, 即: <0.69.0> == <9547.0.0>

3. sys.config实现了发布新的release后会根据新的sys.config再加载配置

When traversing the contents of sys.config and a filename is encountered, its contents are read and merged with the result so far. When an application configuration tuple{Application, Env} is found, it is merged with the result so far. Merging means that new parameters are added and existing parameter values overwritten.

官方的例子给得太简单啦,演示个完整的例子看看:)

假如我们想在上面的slaver节点上的kernel再加一个配置database的地址, 那么我们新建一个region.config

#File: /etc/region.config
[{kernel, [
{database_for_example_purposes, "db://prod-host-omg:8089"}
]}].

那么我们要做的就是把region.config加到sys.config

File: sys.config
[{kernel, [
{sync_nodes_optional, ['master@127.0.0.1']},
{sync_nodes_timeout, 150000}
]},
"/etc/region.config"].

我们再验证一下是否改变

iex --cookie secret --name slave@127.0.0.1 --erl "-config ./sys.config"
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.2.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(slave@127.0.0.1)1> Application.get_all_env(:kernel)
[database_for_example_purposes: 'db://prod-host-omg:8089',
sync_nodes_timeout: 150000, included_applications: [],
sync_nodes_optional: [:"master@127.0.0.1"], error_logger: :tty]

有兴趣可以看看extrm release如何生成sys.config的

参照资料

1. Distributed-otp-applications

2. Design_principles about distributed_applications

3. sys.config


A day in the life of an Erlang developer

[Elixir002]节点启动后自动连接其它节点的更多相关文章

  1. web容器启动后自动执行程序的几种方式比较

    1.       背景 1.1.       背景介绍 在web项目中我们有时会遇到这种需求,在web项目启动后需要开启线程去完成一些重要的工作,例如:往数据库中初始化一些数据,开启线程,初始化消息队 ...

  2. springboot启动后自动退出

    有时新建的springboot启动后自动退出运行,如图所示: 此种情况大都数是因为pom文件加入了tomcat的依赖,与springboot内嵌的tomcat冲突导致,所以只需将pom文件中的tomc ...

  3. hadoop运行故障问题解决1——datanode节点启动后自动关闭

    ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: java.io.IOException: Incompatible namespaceID ...

  4. Springboot项目启动后自动创建多表关联的数据库与表的方案

    文/朱季谦 在一些项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据 ...

  5. 禁止Visual Studio启动时自动连接TFS服务器

    在默认设置情况下,Visual Studio启动时,会自动连接上次打开过的TFS服务器.这种设计能够提高开发人员的工作效率,避免每次手动连接TFS服务器. 但是在某些情景中,也会给人造成不必要的麻烦, ...

  6. linux启动后自动登录并运行自定义图形界面程序

    在<Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法>一文中提到linux启动在以后运行一个独占显示器的图形程序的两种办法. 1.不启动xserver,使 ...

  7. SpringBoot启动后自动打开浏览器访问项目

    之前我们用SSM或者SSH进行JAVA WEB开发的时候,IDEA 需要配置Tomcat然后把项目放到tomcat运行,tomcat启动的时候会自动打开浏览器去访问项目,但是SpringBoot是内嵌 ...

  8. kali linux 启动无法自动连接网络问题i

    kali 有一个很大的问题:无法自动连接网咯. 而且,按照网上的方法修改/etc/Network-manager/Network*.conf和/etc/network/interfaces也没有效果. ...

  9. 使用systemd将iptables规则在docker启动后自动导入

    编写systemd文件 $ sudo vi /etc/systemd/system/iptables-import.service # /etc/systemd/system/iptables-imp ...

随机推荐

  1. Oracle数据库LOGGING&NOLOGGING模式概述

    1.日志记录模式(LOGGING .FORCE LOGGING .NOLOGGING) 1.1三者的含义 LOGGING:当创建一个数据库对象时将记录日志信息到联机重做日志文件.LOGGING实际上是 ...

  2. 蚂蚁社招Java-第四轮电话面试【技术终面】

    作者:听着歌过面试链接:https://www.nowcoder.com/discuss/64708来源:牛客网 蚂蚁社招Java-第四轮电话面试[技术终面] 转载   (耗时22分钟,其实聊得东西挺 ...

  3. win8 机器硬盘异响

    win8系统安装在ssd上,挂了一块希捷2T的机器硬盘做数据存储用,经常发生异响.类似通电断电的声音.因为硬盘的APM节能使磁头归位造成的声音. 使用CrystalDiskInfo软件禁用APM,异响 ...

  4. JAVA_02

    class Test2_Extents{ public static void main(String[] args){ System.out.println("Hello World&qu ...

  5. xdebug php

    sudo apt-get install php5-dev php5-cli #其中php5-dev为了安装xdebug所以必须安装. sudo apt-get install php5-xsl #X ...

  6. ansible Developing Plugins

    Action plugins是模块的前端,可以在调用模块本身之前对控制器执行操作. Cache plugins用于保存“facts”的缓存,以避免代价高昂的fact-gathering操作. Call ...

  7. 第4章 类与对象 UML简介

  8. jsp页面拨打电话和QQ聊天

    拨打电话: <a href="tel:手机号">拨打电话</a> 这种方式塞班.安卓与iphone都支持. 参考文章:https://blog.csdn.n ...

  9. 二叉树翻转 · binary tree flipping

    [抄题]: 给定一个二叉树,其中所有右节点要么是具有兄弟节点的叶节点(有一个共享相同父节点的左节点)或空白,将其倒置并将其转换为树,其中原来的右节点变为左叶子节点.返回新的根节点. 您在真实的面试中是 ...

  10. 8-linux 安装 requests 时 pip install 安装不了

    安装提示更新:但是必须要sudo才行: sudo pip install --upgrade pip 安装 requests时有报错:这样写可以: sudo python -m pip install ...