简体   繁体   English

Java Spring Boot的Redis会话

[英]Redis Session with Java Spring boot

I am trying to save my session instances withing the Redis server ruining on local host. 我正在尝试使用Redis服务器在本地主机上破坏保存我的会话实例。 When I start the application I am getting a error massage and application wont start. 当我启动应用程序时,出现错误提示,并且应用程序无法启动。 It looks like the class can not be found or bean is wrong but I do not understand the error message well. 似乎找不到该类或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

It have used Jedis within the SessionConfiguration class to call for Jedis object. 它已在SessionConfiguration类中使用Jedis来调用Jedis对象。

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

I have tried a solution from this post: stackoverflow.com/q/33128318/11189140 我已经尝试过这篇文章的解决方案:stackoverflow.com/q/33128318/11189140

but when I did I was/am getting this error 但是当我做的时候我正在/这个错误

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 POM文件

<?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>

In your error message there is an NoClassDefFoundError: redis/clients/util/Pool which resides in redis/clients/jedis/util/Pool since Jedis 3. 在您的错误消息中,存在一个NoClassDefFoundError: redis/clients/util/Pool ,自Jedis 3起就驻留在redis/clients/jedis/util/Pool

Please check your (managed) dependencies ( mvn dependency:tree ), if you are really still using Jedis in version 2.x. 如果您确实仍在使用2.x版本的Jedis,请检查(托管)依赖项(mvndependency mvn dependency:tree )。

At least Spring Data Redis 2.1.x is using Jedis 2.9.3 where redis/clients/util/Pool is yet valid. 至少Spring Data Redis 2.1.x使用Jedis 2.9.3,其中redis/clients/util/Pool仍然有效。 Therefore I suspect conflicting dependencies in your case. 因此,我怀疑您的情况下依赖项冲突。

Addendum : 附录

Thanks for sharing also your project . 感谢您也分享您的项目 Now it's quite clear what is happening: The crucial part is your 现在很清楚发生了什么:关键部分是您的

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

Please note that this is a managed dependency of Spring Boot, however you forced it to be in version 1.2.2.RELEASE . 请注意,这是Spring Boot的托管依赖项,但是您将其强制为1.2.2.RELEASE版本。 Remove this one from your dependencies, spring-session-data-redis is doing everything you need for you. 从您的依赖项中删除此文件, spring-session-data-redis正在为您做所需的一切。

I've committed this one and also an integration test in a my forked repo and created a pull request for you. 我已经在一个叉式仓库中提交了这个请求以及一个集成测试,并为您创建了请求请求

Please verify that it also works for you! 请确认它也适合您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM