在akka项目中引入akka-http模块时出现异常报错的解决

2017-08-23

由于项目需要在akka系统中使用http服务,当引入akka-http模块时,启动系统会出现如下报错

1
Detected java.lang.NoSuchMethodError error, which MAY be caused by incompatible Akka versions on the classpath. Please note that a given Akka version MUST be the same across all modules of Akka that you are using, e.g. if you use akka-actor [2.5.4 (resolved from current classpath)] all other core Akka modules MUST be of the same version. External projects like Alpakka, Persistence plugins or Akka HTTP etc. have their own version numbers - please make sure you're using a compatible set of libraries.

错误log指明该错误的原因为akka-actor版本出现冲突。

项目使用maven管理依赖。配置akka-http模块时直接引入了该模块,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http_2.11</artifactId>
<version>10.0.9</version>
</dependency>
</dependencies>

而分析akka-http的依赖得到

1
2
3
4
5
6
7
com.typesafe.akka-http_2.11:10.0.9
|
--com.typesafe.akka-http-core_2.11:10.0.9
|
--com.typesafe.akka-stream_2.11:2.4.19
|
--com.typesafe.akka-actor_2.11:2.4.19

akka-actor模块上出现了冲突,本项目使用了2.5.3版本而akka-http依赖的akka-stream包却依赖2.4.19版本。问题可能出现在这里。因此,针对该问题,将pom.xml改为如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_2.11</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http_2.11</artifactId>
<version>10.0.9</version>
</dependency>
</dependencies>

重新启动应用,查日志无该异常打印。至此,此问题得到解决。