玩转 SpringBoot 2.x 之 快速集成 Jedis客户端(普通版)

前言

Java 开发 一般会选择 Jedis 客户端来进行 Redis 相关的操作,本文通过SpringBoot 项目带你快速上手 Jedis 相关的操作。

Redis 安装

请参考:带你学 Redis:Redis安装(二)

代码案例

基础配置操作

引入 jedis 客户端的依赖,具体代码如下:

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

添加 Jedis 客户端配置类,将JedisPoolConfig和 JedisPool 注入Spring的上下文中。具体代码如下:

@Configuration
public class JedisConfig {

    @Value("${redis.host}")
    String host;
    @Value("${redis.port}")
    int port;
    @Value("${redis.timeout}")
    int timeout;

    @Bean
    @ConfigurationProperties("redis")
    public JedisPoolConfig jedisPoolConfig() {
        return new JedisPoolConfig();
    }

    @Bean(destroyMethod = "close")
    public JedisPool jedisPool() {
        JedisPool jedisPool = new JedisPool(jedisPoolConfig(), host, port, timeout*1000);
        return jedisPool;
    }
}

添加 Redis 基础配置信息在 SpringBoot 配置文件 application.properties 中。


redis.host=192.168.31.150
redis.port=6379
redis.timeout=2000
  • redis.host:Redis服务器主机 ip。
  • redis.port:Redis服务器端口。
  • redis.timeout:连接超时时间时间单位是毫秒。

演示

这里通过 set key value 、get key、del key 三个命令进行演示操作。三个命令具体含义如下:

  • set key value :向指定的key中添加 string 的value 返回ok,如果有则覆盖。
  • get key:获取指定key的value值,返回值是string 的 value。
  • del key:删除指定的 key 名称的的key-value 信息,返回删除的个数。
@RunWith(SpringRunner.class)
@SpringBootTest
public class JedisTest {
    Logger log = LoggerFactory.getLogger(JedisTest.class);
    @Autowired
    private JedisPool jedisPool;

    @Test
    public void set() {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String returnVlaue = jedis.set("a","1");
            log.info("set 返回值是:{}",returnVlaue);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }
    @Test
    public void get(){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String returnVlaue = jedis.get("a");
            log.info("key 为 a的值是:{}",returnVlaue);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }

    @Test
    public void del(){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            long count = jedis.del("a");
            log.info("删除 key 为 a 的值");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(jedis != null){
                jedis.close();
            }
        }
    }
}

set 测试日志信息:

2020-02-22 18:53:09.439  INFO 18160 --- [           main] cn.lijunkui.JedisTest                    : Started JedisTest in 8.602 seconds (JVM running for 10.518)
2020-02-22 18:53:10.330  INFO 18160 --- [           main] cn.lijunkui.JedisTest                    : set 返回值是:OK
2020-02-22 18:53:10.342  INFO 18160 --- [       Thread-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

get 测试日志信息:

2020-02-22 19:38:33.285  INFO 19108 --- [           main] cn.lijunkui.JedisTest                    : Started JedisTest in 9.0 seconds (JVM running for 11.008)
2020-02-22 19:38:34.054  INFO 19108 --- [           main] cn.lijunkui.JedisTest                    : key  a的值是:1
2020-02-22 19:38:34.064  INFO 19108 --- [       Thread-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

del 测试的日志信息:

2020-02-22 19:39:32.625  INFO 3800 --- [           main] cn.lijunkui.JedisTest                    : Started JedisTest in 8.536 seconds (JVM running for 10.509)
2020-02-22 19:39:33.480  INFO 3800 --- [           main] cn.lijunkui.JedisTest                    : 删除 key  a 的值
2020-02-22 19:39:33.491  INFO 3800 --- [       Thread-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

Process finished with exit code 0

小结

是不是很简单呢,如果对 Jedis 还不熟悉的小伙伴,抓紧操作起来吧!

代码示例

我本地环境如下:

  • SpringBoot Version: 2.1.0.RELEASE
  • Apache Maven Version: 3.6.0
  • Java Version: 1.8.0_144
  • IDEA:IntellJ IDEA

整合过程如出现问题可以在我的GitHub 仓库 springbootexamples 中模块名为 spring-boot-2.x-redis-jedis 项目中进行对比查看

GitHub:https://github.com/zhuoqianmingyue/springbootexamples

发布了141 篇原创文章 · 获赞 531 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/ljk126wy/article/details/104447656
今日推荐