java - 如何在akka项目java中使用typesafe conf
问题描述
我有一个使用 AKKA 框架用 Java 编写的应用程序。我想在本地模式下运行应用程序。配置文件 application.conf :
akka{
actor {
provider = "akka.cluster.ClusterActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
log-remote-lifecycle-events = off
netty.tcp {
hostname = "127.0.0.1"
port =2552
maximum-frame-size = 1048576000b
send-buffer-size = 1048576000b
receive-buffer-size = 1048576000b
}
}
cluster {
failure-detector {
threshold = 12
acceptable-heartbeat-pause = 240s
heartbeat-interval = 200s
heartbeat-request {
expected-response-after = 100s
}
}
seed-nodes = [
"akka.tcp://kCoreDescompositionSystem@127.0.0.1:2552",
"akka.tcp://kCoreDescompositionSystem@127.0.0.1:2553",
"akka.tcp://kCoreDescompositionSystem@127.0.0.1:2554",
]
auto-down-unreachable-after = 10s
}
}
我想配置一个 Master 和 3 个 worker。所以读取 conf 文件的 MasterMain 类:
public class MasterMain {
public static void main(String[] args) throws IOException {
final int nbWorkers = 3 ;
final int nbPartitions = 3 ;
final String graphFile = "graph/facebook.txt" ;
final int algo = 1 ;
final int method = 1;
int port = 2552;
final Config configLocal = ConfigFactory.parseString("akka.cluster.roles = [masterRole] ").
// withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=0" )).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname = \"127.0.0.1\"")).
//withFallback(ConfigFactory.parseString("akka.cluster.role.workerRole.min-nr-of-members = " +nbWorkers)).
withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes = [\"akka.tcp://MasterMain@127.0.0.1:2552\"]")).
withFallback(ConfigFactory.load("kcore"));
System.out.println(configLocal.toString());
final ActorSystem system = ActorSystem.create("MasterMain", configLocal);
system.log().info("System will start when at least"+nbWorkers+" workers node join the cluster.");
Cluster.get(system).registerOnMemberUp(new Runnable() {
@Override
public void run() {
system.actorOf(Props.create(Master.class, nbWorkers, nbPartitions,graphFile,algo,method ),"master");
}
});
我很困惑如何正确创建 Config configLocal = ConfigFactory .....
解决方案
创建configLocal
对象时,您可以使用ConfigFactory.load()
加载文件中的配置。
final Config configLocal = ConfigFactory.parseString("akka.cluster.roles = [masterRole] ").
// withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=0" )).
withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.hostname = \"127.0.0.1\"")).
//withFallback(ConfigFactory.parseString("akka.cluster.role.workerRole.min-nr-of-members = " +nbWorkers)).
withFallback(ConfigFactory.parseString("akka.cluster.seed-nodes = [\"akka.tcp://MasterMain@127.0.0.1:2552\"]")).
withFallback(ConfigFactory.load());
推荐阅读
- c# - 在异步 EF 核心拦截器中获取堆栈跟踪
- reactjs - 我对 npx create react app 有一些问题,可以 sbd 告诉我如何解决它,请
- python - 尝试在 WSL 2(Ubuntu 20.04 LTS 和 Windows 10)中使用 PyGame 检测 MIDI 输入时出现 Alsa 错误
- javascript - 在移动浏览器中隐藏两个文本选择助手
- c# - EPPLus 公式在特定单元格中不起作用
- javascript - 单击一次后如何禁用 React Star Rating
- windows - docker-compose useradd 告诉我用户已经存在
- node.js - 如何使用 FastCSV 识别不同的输入模式?
- node.js - 何时在 javascript 中使用 Redis 而不是 Map Object
- java - Vaadin Flow 是否支持 Spring-Boot Native