gpt4 book ai didi

java - Java 中支持 SSL 的 Akka HTTP 服务器——如何创建配置?

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:44 24 4
gpt4 key购买 nike

我正在尝试创建一个支持 SSL 的 Akka HTTP 服务器。

我知道 scala Akka HTTP 2.0 to use SSL (HTTPS) 的这个问题,我正在尝试将它用于 Java 代码,但我迷路了。

DSL akka.http.javadsl.Http 类对于 Java 是不同的,需要 akka.actor.ExtendedActorSystem,当我尝试为其创建一个实例时,我需要使用 com.typesafe.config 创建一个应用程序配置。配置类,我不知道如何实例化以及在其中放入什么。

有没有更简单的方法?或者我可以使用任何类来创建所有必需的配置?

这是一段代码:

    // boot up server using the route as defined below
final ActorSystem system = ActorSystem.create();
final ActorMaterializer materializer = ActorMaterializer.create(system);

// Run the server bound to the local machine IP
String hostAddress = InetAddress.getLocalHost().getHostAddress();

// No implementation here?????
Config applicationConfig = new Config() {
}
ExtendedActorSystem extendedActorSystem = new ActorSystemImpl("HttpProxy", applicationConfig, ClassLoader.getSystemClassLoader(), Option.empty());
// todo: missing handler, settings, httpsContext and log
Flow<HttpRequest, HttpResponse, ?> handler;
ServerSettings settings;
akka.japi.Option<HttpsContext> httpsContext;
LoggingAdapter log;
new Http(extendedActorSystem).bindAndHandle(handler, hostAddress, PORT, settings, httpsContext, log, materializer);

System.out.println("Starting server on " + hostAddress + ":" + PORT);

// The server would stop if carriage return is entered in the system cosole
System.out.println("Type RETURN to exit");
System.in.read();
system.shutdown();

最佳答案

应该是这样的:

//使用下面定义的路由启动服务器

// Run the server bound to the local machine IP
String hostAddress = InetAddress.getLocalHost().getHostAddress();

// No implementation here?????
Config applicationConfig = ConfigFactory.load();
ActorSystem system = ActorSystem.create("HttpProxy", applicationConfig);
final ActorMaterializer materializer = ActorMaterializer.create(system);


// todo: missing handler, settings, httpsContext and log
Flow<HttpRequest, HttpResponse, ?> handler;
ServerSettings settings;
akka.japi.Option<HttpsContext> httpsContext;
LoggingAdapter log;
Http.get(system).bindAndHandle(handler, hostAddress, 9000, settings, httpsContext, log, materializer);

System.out.println("Starting server on " + hostAddress + ":" + 9000);

关于java - Java 中支持 SSL 的 Akka HTTP 服务器——如何创建配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36204523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com