spring boot2.2配置redis(Lettuce和Jedis)

Lettuce和Jedis的区别

  • Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server。

  • Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接。

  • Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

使用Lettuce连接redis

1.pom.xml中加入依赖

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
</dependency>
<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

spring2集成redis所需common-pool2

2.application.properties中加入配置

#redis
# ip
spring.redis.host=localhost
#端口号
spring.redis.port=6379
#密码
spring.redis.password=123456
#连接超时时间(单位为毫秒)
spring.redis.timeout=5000
#使用Lettuce连接redis
#最大活跃连接数 默认8
spring.redis.lettuce.pool.max-active=8
# 最大空闲连接数 默认8
spring.redis.lettuce.pool.max-idle=8
# 最小空闲连接数 默认0
spring.redis.lettuce.pool.min-idle=0

3.配置redis的key和value的序列化方式,默认使用的JdkSerializationRedisSerializer 这样的会导致通过redis desktop manager显示的key和value的时候显示不是正常字符。
创建RedisConfig

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)//在RedisAutoConfiguration之后配置
public class RedisConfig {
    /**
     * 配置自定义redisTemplate
     * @return
     */
    @Bean
    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用FastJSON的序列化方式
        template.setValueSerializer(fastJsonRedisSerializer);
        // hash的value序列化方式采用FastJSON的序列化方式
        template.setHashValueSerializer(fastJsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

使用Jedis连接redis

1.pom.xml中加入依赖

		 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 

spring boot2.0以上版本默认连接池是lettuce, 所以需要排除lettuce的jar 。

2.application.properties中加入配置

#redis
# ip
spring.redis.host=localhost
#端口号
spring.redis.port=6379
#密码
spring.redis.password=123456
#连接超时时间(单位为毫秒)
spring.redis.timeout=5000
#使用Jedis连接redis
#最大活跃连接数 默认8
spring.redis.jedis.pool.max-active=8
# 最大空闲连接数 默认8
spring.redis.jedis.pool.max-idle=8
# 最小空闲连接数 默认0
spring.redis.jedis.pool.min-idle=0

3.创建RedisConfig同上

测试

@SpringBootTest
class SpringbootdemoApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    void contextLoads() {
        User user=new User();
        user.setUserName("aaa");
        user.setPassword("bbb");
       	//十秒后过期
        redisTemplate.opsForValue().set("user",user,10, TimeUnit.SECONDS);
    }

}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值