1. 程式人生 > >Spring Boot筆記之Redis

Spring Boot筆記之Redis

摘要:Spring Boot整合redis,jedis。使用Redis服務實現SpringSession。

pom新增

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> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <
version
>
2.9.0</version> </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 :-)
    }
}