企业开发-Akka配置文件的动态更新

原创
2016/07/29 11:24
阅读数 394

一、json格式配置文件如下,细节请参考官方文档:

默认情况下是akka是会自动在类路径下搜索.conf,.properties,.json格式文件并加载为配置文件

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 2553
    }
  }
}

 

二、运行时重载加载项目路径下的application.conf部分参数

   private final ActorSystem system = ActorSystem.create("LocalSystem",ConfigFactory.parseString("loglevel = \"DEBUG\""));

三、运行时更换为新的配置文件

方法1:

   private final ActorSystem system = ActorSystem.create("LocalSystem",ConfigFactory.parseResources("applicationtest.conf"));

方法2:

    private final ActorSystem system = ActorSystem.create("LocalSystem",ConfigFactory.load("applicationtest.conf"));

四、运行时扩展配置

用Config提供的withFallback方法来来扩展默认的配置,比如说你的application.conf文件配置看起来如下

akka{  
    loggers = ["akka.event.slf4j.Slf4jLogger"]
    loglevel = DEBUG
    arun="hello"
}

你新增了部分配置

val config=ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"]""")
//主意下面这句,一定要
val system=ActorSystem("UniversityMessageSystem", 
                                         config.withFallback(ConfigFactory.load()))

并且你需要合并新的配置进去

println (system.settings.config.getValue("akka.arun"))

println (system.settings.config.getValue("akka.loggers"))

五、一文件多配置:这里主意json配置的作用域

使用时:

  final ActorSystem system = ActorSystem.create("akkademy",
            ConfigFactory.load().getConfig("akkademy"));

配置文件:

akkademy {
akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 2553
    }
  }
}
}
remoteakkademy {
  akka {
    actor {
      provider = "akka.remote.RemoteActorRefProvider"
    }
    remote {
      enabled-transports = ["akka.remote.netty.tcp"]
      netty.tcp {
        hostname = "127.0.0.1"
        port = 2553
      }
    }
  }
}

六、多文件合并

include "application"

很简单吧。

七、适配器,或者说给路径取别名,以便隐藏细节,简化引用

akka.actor.deployment {
 
  # '/user/actorA/actorB' is a remote deployed actor
  /actorA/actorB {
    remote = "akka.tcp://sampleActorSystem@127.0.0.1:2553"
  }
  
  # all direct children of '/user/actorC' have a dedicated dispatcher 
  "/actorC/*" {
    dispatcher = my-dispatcher
  }
  
  # '/user/actorD/actorE' has a special priority mailbox
  /actorD/actorE {
    mailbox = prio-mailbox
  }
  
  # '/user/actorF/actorG/actorH' is a random pool
  /actorF/actorG/actorH {
    router = random-pool
    nr-of-instances = 5
  }
}
 
my-dispatcher {
  fork-join-executor.parallelism-min = 10
  fork-join-executor.parallelism-max = 10
}
prio-mailbox {
  mailbox-type = "a.b.MyPrioMailbox"
}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
3 收藏
0
分享
返回顶部
顶部