springboot集成redisson

pom引入

    
      org.redisson
      redisson-spring-boot-starter
      3.13.3
    
redisson-spring-boot-starter集成了spring data redis和redisson;整合redisson配置支持spring data redis配置

配置文件增加redis配置

#redis
redis.server=ip1;ip2;ip3
redis.port=6379
#资源池中最大连接数
redis.properties.maxTotal=20
#资源池允许最大空闲的连接数
redis.properties.maxIdle=20
#资源池确保最少空闲的连接数
redis.properties.minIdle=20
#当资源池用尽后,调用者是否要等待。只有当为true时,下面的maxWaitMillis才会生效
redis.properties.blockWhenExhausted=true
#当资源池连接用尽后,调用者的最大等待时间(单位为毫秒) -1:表示永不超时
redis.properties.maxWaitMillis=300
#向资源池借用连接时是否做连接有效性检测(ping),无效连接会被移除
redis.properties.testOnBorrow=true
#向资源池归还连接时是否做连接有效性检测(ping),无效连接会被移除
redis.properties.testOnReturn=false

使用示例:

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 测试redis客户端redisson(redis分布式锁测试)
     * @return
     * @throws InterruptedException
     */
    public String testRedissonClient() throws InterruptedException {
        RLock rLock = redissonClient.getLock("abc");
        boolean lock = rLock.tryLock(10, TimeUnit.SECONDS);
        System.out.println("thread1 lock."+ lock);
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread2 start.");
                RLock rLock = redissonClient.getLock("abc");
                boolean lock = false;
                try {
                    lock = rLock.tryLock(30, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread2 lock:" + lock);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (lock){
                    rLock.unlock();
                    System.out.println("thread2 unlock.");
                }

            }
        });
        thread.start();
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        rLock.unlock();
        System.out.println("thread1 unlock.");
        try {
            Thread.sleep(50000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //设置读取值
        RAtomicLong rAtomicLong = redissonClient.getAtomicLong("a");
        RBucket kv2 = redissonClient.getBucket("1");
        System.out.println(kv2.get());
        return null;
    }

    /**
     * 测试redisson延迟队列
     */
    public void testRedissonBlocking() {
        RBlockingDeque blockingDeque = redissonClient.getBlockingDeque("testBlocking");
        //延迟队列take时,需要执行该方法先获取阻塞队列(不执行此步骤,重启时,如果延迟队列中不放数据,便不会消费之前的数据)
        RDelayedQueue delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread add start.");
                RBlockingDeque blockingDeque = redissonClient.getBlockingDeque("testBlocking");
                RDelayedQueue delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
                delayedQueue.offer("1", 5, TimeUnit.SECONDS);
                delayedQueue.offer("2", 10, TimeUnit.SECONDS);
                delayedQueue.offer("3", 15, TimeUnit.SECONDS);
                delayedQueue.offer("4", 20, TimeUnit.SECONDS);
                delayedQueue.offer("5", 25, TimeUnit.SECONDS);
                delayedQueue.destroy();
                System.out.println("thread add end.");
            }
        });
        thread.start();
        while (true){
            String redPacketMessage = null;
            try {
                redPacketMessage = blockingDeque.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("红包过期。id:" + redPacketMessage);
        }
    }

引用该jar包,同时支持spring data redis;以下为spring data redis使用方法

声明bean

@Configuration
public class RedisTemplateConfig {
   
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(factory);
        return template;
    }
}

使用方法:

    @Autowired
    private RedisTemplate redisTemplate;


    public String testRedisTemplate() {
        redisTemplate.opsForValue().set("test", "testV", 20, TimeUnit.SECONDS);
        String s = (String)redisTemplate.opsForValue().get("test");
        System.out.println(s);
        return null;
    }

 

你可能感兴趣的:(随手记)