Redis与Java Spring Boot的会话管理

4
我正在尝试将我的会话实例保存在本地主机上运行的Redis服务器中。当我启动应用程序时,出现错误消息并且应用程序无法启动。看起来像是找不到类或bean有误,但我不太理解错误消息。
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.739 s <<< FAILURE! - in com.kb.webkb.WebkbApplicationTests
[ERROR] contextLoads(com.kb.webkb.WebkbApplicationTests)  Time elapsed: 0.005 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Unsatisfied dependency expressed through method 'redisMessageListenerContainer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/kb/webkb/configuration/session/SessionConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/kb/webkb/configuration/session/SessionConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: java.lang.NoClassDefFoundError: redis/clients/util/Pool
Caused by: java.lang.ClassNotFoundException: redis.clients.util.Pool

在SessionConfiguration类中已使用Jedis来调用Jedis对象。

@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
    @Bean
    public JedisConnectionFactory connectionFactory() {
        return new JedisConnectionFactory();
    }
}

我尝试了这篇文章中提供的解决方案:stackoverflow.com/q/33128318/11189140
但是当我进行尝试时,出现了以下错误。
Description:

An attempt was made to call the method org.springframework.data.redis.connection.RedisConnection.getConfig(Ljava/lang/String;)Ljava/util/List; but it does not exist. Its class, org.springframework.data.redis.connection.RedisConnection, is available from the following locations:

    jar:file:/home/kbuczynski/.m2/repository/org/springframework/data/spring-data-redis/2.1.5.RELEASE/spring-data-redis-2.1.5.RELEASE.jar!/org/springframework/data/redis/connection/RedisConnection.class

It was loaded from the following location:

    file:/home/kbuczynski/.m2/repository/org/springframework/data/spring-data-redis/2.1.5.RELEASE/spring-data-redis-2.1.5.RELEASE.jar

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kb</groupId>
    <artifactId>webkb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>webkb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这个问题似乎已经在这里得到了回答: https://dev59.com/61wY5IYBdhLWcg3wAjs9 - Anirudh Simha
@AnirudhSimha,我确实找到了这个解决方案并尝试过,但是出现了不同的错误。我编辑了我的帖子,这样你就可以看到失败的原因了。 - NashPL
1个回答

6
在您的错误信息中,有一个NoClassDefFoundError: redis/clients/util/Pool,它位于redis/clients/jedis/util/Pool,自Jedis 3以来。请检查您的(托管)依赖项(mvn dependency:tree),如果您仍在使用2.x版本的Jedis,请确保检查。至少Spring Data Redis 2.1.x正在使用Jedis 2.9.3,其中redis/clients/util/Pool仍然有效。因此,我怀疑您的情况存在冲突的依赖关系。 附录
感谢分享您的项目。现在很清楚发生了什么:关键部分是您的
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

请注意,这是Spring Boot的托管依赖项,但您将其强制为版本1.2.2.RELEASE。从您的依赖项中删除此内容,spring-session-data-redis已经为您完成了所有必要的工作。
我已提交了这个和一个集成测试到我的存储库,并为您创建了一个pull request
请验证它是否也适用于您!

看起来我正在使用 [INFO] +- org.springframework.session:spring-session-data-redis:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.data:spring-data-redis:jar:2.1.5.RELEASE:compile [INFO] - org.springframework.boot:spring-boot-starter-data-redis:jar:2.1.3.RELEASE:compile如果出现问题,有什么解决方法吗?我不太明白如何解决。 - NashPL
你的 pom.xml 中的确切配置和依赖项在我的环境中可以工作。当我运行 mvn dependency:tree 命令时,它显示了以下内容:[INFO] +- redis.clients:jedis:jar:2.9.1:compile - 请确认一下这个依赖是否正确? - mle
嗨,我已经创建了一个新项目并重用了我提供的pom.xml文件。我设法摆脱了烦人的错误,但是我卡在了这里:尝试调用方法org.springframework.data.redis.connection.RedisConnection.getConfig(Ljava/lang/String;)Ljava/util/List;,但它不存在。它的类org.springframework.data.redis.connection.RedisConnection可从以下位置访问:它建议修复类路径,以便包含单个兼容版本的spring reddis。有什么建议如何做到这一点吗? - NashPL
你能否分享一下你的新项目,比如在Github上? - mle
太棒了,我会在几个小时后查看它,并且现在很有信心。因为这里已经过了午夜,我需要先睡几个小时。 :-) - mle
1
这个答案帮了我很多!我觉得下面的参考资料也会有所帮助: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-jedis-instead-of-lettuce - Diego dos Santos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接