springboot引入Redis配置JedisPool连接池

目录

一、pom.xml依赖

二、application.properties配置

三、java配置类

四、单元测试


一、pom.xml依赖


    org.springframework.boot
    spring-boot-starter-data-redis




    redis.clients
    jedis

二、application.properties配置

## redis setting
spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.port=6379
## redis jedis pool setting
spring.redis.jedis.pool.max-active=20
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=5
## redis lettuce pool setting 
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.min-idle=5

三、java配置类

1.配置Redis连接池JedisPool

2.配置RedisTemplate存值取值序列化

package com.leadpms.qianlistandard.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Shaoyu Liu
 * @date 2021/5/28 15:50
 **/
@Configuration
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate template = new RedisTemplate<>();
        template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

//    @Bean
//    public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPool,
//                                                         RedisStandaloneConfiguration jedisConfig) {
//        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(jedisConfig);
//        connectionFactory.setPoolConfig(jedisPool);
//        return connectionFactory;
//    }
//
//    @Configuration
//    public static class JedisPoolConf {
//        @Value("${spring.redis.host:127.0.0.1}")
//        private String host;
//        @Value("${spring.redis.port:6379}")
//        private Integer port;
//        @Value("${spring.redis.password:}")
//        private String password;
//        @Value("${spring.redis.database:0}")
//        private Integer database;
//
//        @Value("${spring.redis.jedis.pool.max-active:8}")
//        private Integer maxActive;
//        @Value("${spring.redis.jedis.pool.max-idle:8}")
//        private Integer maxIdle;
//        @Value("${spring.redis.jedis.pool.max-wait:-1}")
//        private Long maxWait;
//        @Value("${spring.redis.jedis.pool.min-idle:0}")
//        private Integer minIdle;
//
//        @Bean
//        public JedisPoolConfig jedisPool() {
//            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//            jedisPoolConfig.setMaxIdle(maxIdle);
//            jedisPoolConfig.setMaxWaitMillis(maxWait);
//            jedisPoolConfig.setMaxTotal(maxActive);
//            jedisPoolConfig.setMinIdle(minIdle);
//            return jedisPoolConfig;
//        }
//
//        @Bean
//        public RedisStandaloneConfiguration jedisConfig() {
//            RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
//            config.setHostName(host);
//            config.setPort(port);
//            config.setDatabase(database);
//            config.setPassword(RedisPassword.of(password));
//            return config;
//        }
//    }

}

springboot引入Redis配置JedisPool连接池_第1张图片

 

四、单元测试

import com.leadpms.qianlistandard.dao.entity.auto.User;
import com.leadpms.qianlistandard.service.redis.RedisService;
import com.leadpms.qianlistandard.web.WebApplication;
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.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author Shaoyu Liu
 * @date 2021/5/28 16:57
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebApplication.class)
public class RedisTest2 {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Test
    public void put() {
        User u = new User();
        u.setName("刘邵宇");
        u.setAccount("liushaoyu");
        redisTemplate.opsForValue().set("liushaoyu", u);
        redisTemplate.opsForHash().put("user:", "1", u);

        // 可以返回map映射,并且RedisConfig需要配置template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        Object userByKey = redisTemplate.opsForValue().get("liushaoyu");

        // 只有hash类型可以强转为对象,并且RedisConfig需要配置template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        User userByHashKey = (User) redisTemplate.opsForHash().get("user:", "1");

        // 只可以转为object类型 什么也没做,只是对key value hashkey hashvalue做了默认序列化处理,stringRedisTemplate返回的是一个字符串无法转为map类型
        Object o = stringRedisTemplate.opsForHash().get("user:", "1");
        System.out.println(1);
    }

}

 

你可能感兴趣的:(spring,boot,redis,spring,boot,redis,jedis)