设为首页 加入收藏

TOP

Akka-Cluster(0)- 分布式应用开发的一些想法(二)
2019-08-15 00:11:27 】 浏览:188
Tags:Akka-Cluster -分布式 应用开发 一些 想法
,port,seed-nodes这些参数都可以在程序里配置,如果有需要,我们只要在配置文件里注明这是一个集群模式的程序就行了,其它参数放到程序里去定义:

akka { actor { provider = "cluster" } }

然后我们可以在程序里配置缺失的集群参数:

object EventListner { trait Messages {} case object Leave extends Messages case object Down extends Messages def props = Props(new EventListener) def create(host: String = "localhost", port: Int = 0, seednode: String = "") = { var config = ConfigFactory.parseString(s"akka.remote.netty.tcp.hostname=${host}") .withFallback(ConfigFactory.parseString(s"akka.remote.netty.tcp.port=${port}")) if (seednode.length > 0) { val strConfig = "akka.cluster.seed-nodes=[\"" + seednode + "\"]" val configSeed = ConfigFactory.parseString(strConfig) config = config.withFallback(configSeed) } config = config.withFallback(ConfigFactory.load("akka-cluster-config")) val clusterSystem = ActorSystem(name="ClusterSystem",config=config) clusterSystem.actorOf(Props[EventListener]) } }

在create函数里ConfigFactory.parseString可以把一个字符串转换成集群配置参数,多个参数可以用withFallback来补充定义。

以下是EventListener的测试程序:

import EventListner._ object EventDemo extends App { val listner1 = EventListner.create(port = 2551)  //seed node
 scala.io.StdIn.readLine() val listner2 = EventListner.create()    //port=0 random port
 scala.io.StdIn.readLine() val listner3 = EventListner.create()    //port=0 random port
 scala.io.StdIn.readLine() listner3 ! Leave scala.io.StdIn.readLine() listner2 ! Down scala.io.StdIn.readLine() listner1 ! Leave scala.io.StdIn.readLine() }

第一个运行的必须是seednode,因为每个节点在启动时都需要连接seednode。下面是每个阶段的输出结果:

[INFO] [10/22/2018 18:50:40.888] [main] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Started up successfully
[INFO] [10/22/2018 18:50:40.931] [ClusterSystem-akka.actor.default-dispatcher-3] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Node [akka.tcp://ClusterSystem@localhost:2551] is JOINING itself (with roles [dc-default]) and forming new cluster
[INFO] [10/22/2018 18:50:40.933] [ClusterSystem-akka.actor.default-dispatcher-3] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Cluster Node [akka.tcp://ClusterSystem@localhost:2551] dc [default] is the new leader
[INFO] [10/22/2018 18:50:40.943] [ClusterSystem-akka.actor.default-dispatcher-3] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Leader is moving node [akka.tcp://ClusterSystem@localhost:2551] to [Up]
[INFO] [10/22/2018 18:50:41.037] [ClusterSystem-akka.actor.default-dispatcher-4] [akka.tcp://ClusterSystem@localhost:2551/user/$a] akka.tcp://ClusterSystem@localhost:2551 is UP!
[INFO] [10/22/2018 18:50:47.363] [ClusterSystem-akka.actor.default-dispatcher-23] [akka.tcp://ClusterSystem@localhost:51679/user/$a] akka.tcp://ClusterSystem@localhost:51679 is JOINING...
[INFO] [10/22/2018 18:50:47.930] [ClusterSystem-akka.actor.default-dispatcher-4] [akka.cluster.Cluster(akka://ClusterSystem)] Cluster Node [akka.tcp://ClusterSystem@localhost:2551] - Leader is moving node [akka.tcp://ClusterSystem@localh
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇scala集合与java集合的转换应用 下一篇Scala学习笔记(一)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目