akka.UnsupportedAkkaVersion:Akka的当前版本是[2.5.14],但是akka-http需要版本[2.5.26]

2020年3月29日 45点热度 0条评论

这是类(class):

import akka.Done;
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.Http;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.HttpEntities;
import akka.http.javadsl.model.HttpHeader;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.server.AllDirectives;
import akka.http.javadsl.server.Route;
import akka.stream.ActorMaterializer;
import akka.stream.javadsl.Flow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public class Ge extends AllDirectives
{

    private static String host = "localhost";
    private static int port = 8080;
    private static List<HttpHeader> headers;


    public static void main(String[] args) throws Exception
    {
        // boot up server using the route as defined below
        ActorSystem system = ActorSystem.create();

        final ActorMaterializer materializer = ActorMaterializer.create(system);
        final Http http = Http.get(system);
        //In order to access all directives we need an instance where the routes are define.

        Ge ge = new Ge();

        final Flow<HttpRequest, HttpResponse, NotUsed> routeFlow = gridUpdateService.createRoute().flow(system, materializer);

        http.bindAndHandle(routeFlow,
                ConnectHttp.toHost(host, port), materializer);

    }

    private Route createRoute()
    {
        return  route(
                        path(
                                "spec", () ->
                                        get(() -> complete(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, "Ok"))
                                        )
                        )
                );
    }

}

这是build.gradle中的依赖项:

compile group: 'com.typesafe.akka', name: 'akka-http_2.12', version: '10.1.5'
compile group: 'com.typesafe.akka', name: 'akka-cluster_2.12', version: '2.5.14'

我不确定为什么会发生此错误,即使我在这里提到并发现这些版本应该兼容,如下面的docs链接中所述:

Compatibility with Akka Akka HTTP 10.1.x is (binary) compatible with Akka >= 2.5.11 and future Akka 2.x versions that are released during the lifetime of Akka HTTP 10.1.x.

https://doc.akka.io/docs/akka-http/current/compatibility-guidelines.html

不幸的是,我不能将akka版本更新为2.5.14,因为它是父项目的版本,而更改可能会破坏其他子项目中的内容。无论如何,我可以使akka http与akka版本2.5.14一起使用吗?

这是完整的堆栈跟踪:

Exception in thread "main" akka.UnsupportedAkkaVersion: Current version of Akka is [2.5.14], but akka-http requires version [2.5.26]
    at akka.AkkaVersion$.require(AkkaVersion.scala:43)
    at akka.AkkaVersion$.require(AkkaVersion.scala:23)
    at akka.http.scaladsl.HttpExt.<init>(Http.scala:57)
    at akka.http.scaladsl.Http$.createExtension(Http.scala:1123)
    at akka.http.scaladsl.Http$.createExtension(Http.scala:892)
    at akka.actor.ActorSystemImpl.registerExtension(ActorSystem.scala:913)
    at akka.actor.ExtensionId.apply(Extension.scala:79)
    at akka.actor.ExtensionId.apply$(Extension.scala:78)
    at akka.http.scaladsl.Http$.apply(Http.scala:1118)
    at akka.http.scaladsl.Http$.apply(Http.scala:892)
    at akka.http.javadsl.Http.delegate$lzycompute(Http.scala:45)
    at akka.http.javadsl.Http.delegate(Http.scala:45)
    at akka.http.javadsl.Http.defaultServerHttpContext(Http.scala:852)
    at akka.http.javadsl.Http.bindAndHandle(Http.scala:232)
    at com.dummy.ui.Ge.startHttp(Ge.java:55)

由于我必须检查一些代码,因此行号将有所不同。

解决方案如下:

问题不在于akka,而是我项目中的过时依赖关系。

我之前尝试使用

compile group: 'com.typesafe.akka', name: 'akka-http_2.13', version: '10.1.5'

哪个必须已下载依赖项并已用于启动项目。

我做了
gradle --refresh-dependencies来解决问题。

确保查看文档以查看模块的兼容性并清理缓存,然后重新下载所有依赖项。