Spring Boot在使用Docker时出现Redis错误,但使用Maven运行时没有出错。

huangapple 未分类评论46阅读模式
英文:

Spring Boot Redis error when dockerized, but not with maven run

问题

我正在使用Spring Boot和Redis(使用redisson)构建一个应用程序。

当我在开发模式下运行应用程序时,它正常工作,但是当我尝试在Docker容器中运行它时,它出现以下错误:

java.lang.ClassNotFoundException: org.springframework.data.redis.connection.ReactiveStreamCommands

我的Redis的Maven配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.redisson</groupId>
    <!-- for Spring Data Redis v.2.2.x -->
    <artifactId>redisson-spring-data-22</artifactId>
    <version>3.12.2</version>
</dependency>

用于构建Docker容器的Maven配置:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.1</version>
    <dependencies>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
    <configuration>
        <imageName>${imageName}</imageName>

        <!-- 默认用于标记镜像的属性 -->
        <image>${imageName}</image>
        <newName>${imageName}:${tagName}</newName>

        <!-- gitlab registry -->
        <serverId>gitlab-repository</serverId>
        <registryUrl>my.repository</registryUrl>

        <baseImage>adoptopenjdk/openjdk11-openj9:latest</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>

        <!-- 将服务的jar文件从target复制到镜像的根目录 -->
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

当我登录到容器并分析我的jar库(使用以下命令):

jar -tf my-app.jar | grep redis

我发现了以下内容:

BOOT-INF/lib/spring-boot-starter-data-redis-2.1.3.RELEASE.jar
BOOT-INF/lib/spring-data-redis-2.1.5.RELEASE.jar
BOOT-INF/lib/redisson-spring-data-22-3.12.2.jar
BOOT-INF/lib/redisson-3.12.2.jar

这与我在开发环境中得到的完全相同。

以下是我在docker-compose中如何启动Redis的摘录:

  redis:
    image: redis:5.0
    ports:
      - 6379:6379
    networks:
      - network
    volumes:
      - redis:/data
    entrypoint: redis-server --appendonly yes 
    restart: unless-stopped

有关容器正常运行所缺少的内容有什么线索吗?
为什么在jar中使用的spring-data-redis版本是2.1.5而不是2.2.x?
提前谢谢您的回答。

英文:

I am building an application with Spring Boot using Redis (with redisson).

When I run my application in development mode, it works fine, but when I try to run it in docker containers, it fails with the error

java.lang.ClassNotFoundException:org.springframework.data.redis.connection.ReactiveStreamCommands

My maven configuration for Redis :

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
	         &lt;groupId&gt;org.redisson&lt;/groupId&gt;
	         &lt;!-- for Spring Data Redis v.2.2.x --&gt;
	         &lt;artifactId&gt;redisson-spring-data-22&lt;/artifactId&gt;
	         &lt;version&gt;3.12.2&lt;/version&gt;
	    &lt;/dependency&gt;

My maven configuration to build docker container :

			&lt;plugin&gt;
				&lt;groupId&gt;com.spotify&lt;/groupId&gt;
				&lt;artifactId&gt;docker-maven-plugin&lt;/artifactId&gt;
				&lt;version&gt;1.2.1&lt;/version&gt;
				&lt;dependencies&gt;
					&lt;dependency&gt;
						&lt;groupId&gt;javax.activation&lt;/groupId&gt;
						&lt;artifactId&gt;activation&lt;/artifactId&gt;
            			&lt;version&gt;1.1.1&lt;/version&gt;
        			&lt;/dependency&gt;
        		&lt;/dependencies&gt;
				&lt;configuration&gt;
					&lt;imageName&gt;${imageName}&lt;/imageName&gt;

					&lt;!-- default properties to tag an image --&gt;
					&lt;image&gt;${imageName}&lt;/image&gt;
					&lt;newName&gt;${imageName}:${tagName}&lt;/newName&gt;

					&lt;!-- gitlab registry --&gt;
					&lt;serverId&gt;gitlab-repository&lt;/serverId&gt;
					&lt;registryUrl&gt;my.repository&lt;/registryUrl&gt;

					&lt;baseImage&gt;adoptopenjdk/openjdk11-openj9:latest&lt;/baseImage&gt;
					&lt;entryPoint&gt;[&quot;java&quot;, &quot;-jar&quot;, &quot;/${project.build.finalName}.jar&quot;]&lt;/entryPoint&gt;

					&lt;!-- copy the service&#39;s jar file from target into the root directory 
						of the image --&gt;
					&lt;resources&gt;
						&lt;resource&gt;
							&lt;targetPath&gt;/&lt;/targetPath&gt;
							&lt;directory&gt;${project.build.directory}&lt;/directory&gt;
							&lt;include&gt;${project.build.finalName}.jar&lt;/include&gt;
						&lt;/resource&gt;
					&lt;/resources&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;

When I login to my container and analysie my jar libraries (with the following command):

jar -tf my-app.jar | grep redis

I find this :

BOOT-INF/lib/spring-boot-starter-data-redis-2.1.3.RELEASE.jar
BOOT-INF/lib/spring-data-redis-2.1.5.RELEASE.jar
BOOT-INF/lib/redisson-spring-data-22-3.12.2.jar
BOOT-INF/lib/redisson-3.12.2.jar

Which is exactly the same as I get with my jar for development.

Here is the docker-compose extract on how I launch redis :

  redis:
    image: redis:5.0
    ports:
      - 6379:6379
    networks:
      - network
    volumes:
      - redis:/data
    entrypoint: redis-server --appendonly yes 
    restart: unless-stopped

Any clue on what is missing for my container to run properly?
Any idea why the spring-data-redis used in the jar is 2.1.5 and not 2.2.x ?
Thanks in advance

答案1

得分: 0

我已经找到了一个解决方案:

我不再使用 redisson-spring-data-22,而是降级为 redisson-spring-data-16(因为我注意到实际使用的 spring-data-redis 版本实际上是 2.1.5,低于 2.2 ...)。

我不能真正解释为什么,但是现在它可以工作了

我在 redisson-spring-data 上找不到关于在什么条件下应该使用哪个版本的任何信息,所以我只是尝试了另一个版本... 然后它就可以工作了!

希望对你有所帮助。

英文:

I have found a solution :

Instead of using redisson-spring-data-22, I downgraded to redisson-spring-data-16 (as I noticed that the effective version of spring-data-redis used was in fact 2.1.5, which is lower than 2.2 ...).

I cannot explain why really, but it works now.

I could not find any track on redisson-spring-data of what version should be used in what conditions, so I just tried another version ... and it worked!

Hope that helps.

huangapple
  • 本文由 发表于 2020年4月9日 18:18:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61118910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定