Home > Java > javaTutorial > SpringBoot configuration redis and distributed session-redis method (code)

SpringBoot configuration redis and distributed session-redis method (code)

不言
Release: 2018-09-20 15:36:24
Original
2890 people have browsed it

The content of this article is about the method (code) of configuring redis and distributed session-redis in SpringBoot. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The difference between springboot project and traditional project configuration redis is simpler and more convenient. In a distributed system, to solve the session sharing problem, spring session redis can be used.

1, pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
Copy after login

2, rdis configuration class

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;
    @Value("${redis.maxTotal}")
    private Integer maxTotal;
    @Value("${redis.maxIdle}")
    private Integer maxIdle;
    @Value("${redis.maxWaitMillis}")
    private Long maxWaitMillis;

    @Bean
    public ShardedJedisPool shardedJedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        List<JedisShardInfo> jedisShardInfos = new ArrayList<>();
        jedisShardInfos.add(new JedisShardInfo(host,port));
        return  new ShardedJedisPool(jedisPoolConfig, jedisShardInfos);
    }

}
Copy after login

3, session redis configuration class

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
 * session共享
 */
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60*60)
public class RedisSessionConfig {
}
Copy after login

The above is the detailed content of SpringBoot configuration redis and distributed session-redis method (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template