Spring Boot笔记之Redis

摘要:Spring Boot整合redis,jedis。使用Redis服务实现SpringSession。

pom添加

pom.xml:

		<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
		<dependency>
            <groupId>org.springframework.sessiongroupId>
            <artifactId>spring-session-data-redisartifactId>
        dependency>
		<dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.0version>
        dependency>

application配置

application.properties

spring.redis.database=1
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456
spring.redis.timeout=10000
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=0
spring.session.store-type=redis
server.servlet.session.cookie.domain=localhost
server.servlet.session.cookie.path=/
server.servlet.session.cookie.name=JSessionID

至此项目的Spring session配置完毕,web端访问时使用localhost的Cookie,生产环境因该配置server.servlet.session.cookie.domain`为域名,仅使用域名访问时才有session。

JedisPool Bean配置

如果程序中需要使用redis存储、读取一些自定义的键值需要配置JedisPoolBean:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableRedisHttpSession
public class RedisConfiguration {
    @Autowired
    private RedisProperties properties;

    /**
     * 注入jedisPool
     * 用于手动调用jedisPool.getResource().set()/get()
     *
     * @return
     */
    @Bean
    public JedisPool getJedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(properties.getJedis().getPool().getMaxIdle());
        config.setMaxTotal(properties.getJedis().getPool().getMaxActive());
        config.setMaxWaitMillis(properties.getJedis().getPool().getMaxWait().toMillis());
        JedisPool pool = new JedisPool(config, properties.getHost(), properties.getPort(), 100, properties.getPassword(), properties.getDatabase());
        return pool;
    }
}

JedisPool的使用

import com.my.demo.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisPool;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class JedisPoolTest {
    @Autowired
    private JedisPool jedisPool;

    @Test
    public void testSet() {
        String result = jedisPool.getResource().set("hello", "Hello World :-)");
        System.out.println(result);// OK
    }

    @Test
    public void testGet() {
        String result = jedisPool.getResource().get("hello");
        System.out.println(result);// Hello World :-)
    }
}

你可能感兴趣的:(笔记,Spring,Boot笔记,spring,boot,redis,session)