springboot使用redis--快速上手jedis和shardedJedis

下载

这里下载的是windows

在这里插入图片描述
直接解压,打开解压目录,进入powerShell 启动服务端:
在这里插入图片描述
打开另一个powerShell启动客户端:
在这里插入图片描述
在这里插入图片描述
ping pong成功;
废话不多说:
先封装一个JedisPool 能够调出JedisPool,我把它加入common包:
简而言之就是配置好JedisPool然后用JedisPool返回Jedis类

properties:

redis1.ip=127.0.0.1
redis1.port=6379
redis.max.total=20
redis.max.idle=10
redis.min.idle=2
redis.test.borrow=true
redis.test.return=false

看下面的配置,自己改下properties文件

package com.van.mall.common;

import com.van.mall.util.PropertiesUtil;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Van
 * @date 2020/3/21 - 13:51
 */
@PropertySource(value = {
   "classpath:application.properties"})
public class RedisPool {
   
    private static JedisPool pool;//connection pool
    private static Integer maxTotal =Integer.parseInt(PropertiesUtil.getPropertity("redis.max.total")) ;//max connection
    private static Integer maxIdle =Integer.parseInt(PropertiesUtil.getPropertity("redis.max.idle"));//max idle connection
    private static Integer minIdle=Integer.parseInt(PropertiesUtil.getPropertity("redis.min.idle"));//min idle connection
    private static Boolean testOnBorrow=Boolean.parseBoolean(PropertiesUtil.getPropertity("redis.test.borrow"));//whether test when borrow a jedis if true then it's available
    private static Boolean testOnReturn=Boolean.parseBoolean(PropertiesUtil.getPropertity("redis.test.return"));//whether test when return a jedis if true then it's available
    private static String redisIp=PropertiesUtil.getPropertity("redis.ip");
    private static Integer redisPort=Integer.parseInt(PropertiesUtil.getPropertity("redis.port")) ;
    private static void initPool(){
   
        JedisPoolConfig config=new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);
        config.setMinIdle(minIdle);

        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
//whether block when resource is exhausted: false will throw a exception,true will block until timeout, default is true
        config.setBlockWhenExhausted(true);
        pool=new JedisPool(config,redisIp,redisPort,1000*2);
    }
    static {
   
        initPool();
    }
    public static Jedis get
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值