springboot使用jedis(jedispool)、lettuce

本文介绍了如何在Spring Boot项目中配置并使用Jedis,包括pom.xml和application.properties的设置,以及Jedis单机和JedisPool连接池的详细使用方法。此外,还提及了Jedis与Lettuce的对比分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、pom.xml配置

<!-- redis lettuce客户端(已集成)、RedisTemplate客户端(已集成) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- jedis客户端(需要额外引入) -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.2.0</version>
</dependency>

二、application.properties配置

spring.redis.host=10.1.1.100
spring.redis.password=Abcd@1234
spring.redis.port=6379
# jedis pool setting
spring.redis.jedis.pool.max-active=20
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5

三、Jedis单机使用

package com.leadpms.qianlistandard.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

/**
 * Jedis单机调用
 *
 * @author Shaoyu Liu
 * @date 2021/6/3 15:01
 **/
@Configuration
public class JedisConfig {

    private static final Jedis jedis = new Jedis("10.1.1.100", 6379);

    static {
        jedis.auth("Abcd@1234");
        jedis.connect();
    }

    @Bean
    public Jedis jedisFactory() {
        return jedis;
    }
}
package com.leadpms.qianlistandard.web.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

/**
 * @author Shaoyu Liu
 * @date 2021/6/3 13:43
 **/
@RestController
@RequestMapping("/redis")
public class RedisController {
    
    @Autowired
    private Jedis jedis;
    
    @GetMapping("/testRedis")
    public void testRedis() {
        jedis.set("jedis_k3", "v3");
    }
    
}

四、JedisPool使用(Jedis连接池)

package com.leadpms.qianlistandard.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Jedis线程池配置类
 *
 * @author Shaoyu Liu
 * @date 2021/6/3 14:07
 **/
@Configuration
public class JedisPoolConfiguration {

    @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;

    @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, 100, password);
        return jedisPool;
    }
}
package com.leadpms.qianlistandard.web.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author Shaoyu Liu
 * @date 2021/6/3 13:43
 **/
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private JedisPool jedisPool;

    @GetMapping("/testRedis")
    public void testRedis() {
        Jedis jedis = jedisPool.getResource();
        jedis.set("jedisPool_v1", "k1");
    }
}

文章参考

jedis和lettuce的对比 - 缘在此季 - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值