【问题标题】:Akka HTTP server with SSL support in Java - How to create configuration?在 Java 中支持 SSL 的 Akka HTTP 服务器 - 如何创建配置?
【发布时间】:2016-03-24 15:53:55
【问题描述】:

我正在尝试创建一个支持 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 创建应用程序配置。 Config 类,我不知道如何实例化以及在其中放入什么。

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

这是一个sn-p的代码:

    // 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();

【问题讨论】:

    标签: java ssl akka tls1.2 akka-http


    【解决方案1】:

    应该是这样的:

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

    // 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多