Redis——发布定义、分布式锁

Redis——发布订阅、分布式锁

1. 发布订阅

核心配置类
package com.mei.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.concurrent.CountDownLatch;

@Configuration
public class RedisMessageListenerConfig {
 /**
  * 创建连接工厂
  * @param connectionFactory
  * @param listenerAdapter
  * @return
  */
 @Bean
 public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                MessageListenerAdapter listenerAdapter, MessageListenerAdapter listenerAdapterTest2){
  RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  container.setConnectionFactory(connectionFactory);
  //接受消息的key
  container.addMessageListener(listenerAdapter,new PatternTopic("phone"));
  container.addMessageListener(listenerAdapterTest2,new PatternTopic("phoneTest2"));
  return container;
 }


 /**
  * 绑定消息监听者和接收监听的方法
  * @param receiver
  * @return
  */
 @Bean
 public MessageListenerAdapter listenerAdapter(ReceiverRedisMessage  receiver){
  return new MessageListenerAdapter(receiver,"receiveMessage");
 }
 /**
  * 绑定消息监听者和接收监听的方法
  * @param receiver
  * @return
  */
 @Bean
 public MessageListenerAdapter listenerAdapterTest2(ReceiverRedisMessage  receiver){
  return new MessageListenerAdapter(receiver,"receiveMessage2");
 }
 /**
  * 注册订阅者
  * @param latch
  * @return
  */
 @Bean
 ReceiverRedisMessage receiver(CountDownLatch latch) {
  return new ReceiverRedisMessage(latch);
 }


 /**
  * 计数器,用来控制线程
  * @return
  */
 @Bean
 public CountDownLatch latch(){
  return new CountDownLatch(1);//指定了计数的次数 1
 }
}

消息接收类
package com.mei.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.CountDownLatch;

public class ReceiverRedisMessage {
 private static final Logger log = LoggerFactory.getLogger(ReceiverRedisMessage.class);
 private CountDownLatch latch;

 @Autowired
 public ReceiverRedisMessage(CountDownLatch latch) {
  this.latch = latch;
 }


 /**
  * 队列消息接收方法
  *
  * @param jsonMsg
  */
 public void receiveMessage(String jsonMsg) {
  log.info("[开始消费REDIS消息队列phone数据...]");
  try {
   System.out.println(jsonMsg);
   log.info("[消费REDIS消息队列phone数据成功.]");
  } catch (Exception e) {
   log.error("[消费REDIS消息队列phone数据失败,失败信息:{}]", e.getMessage());
  }
  latch.countDown();
 }

 /**
  * 队列消息接收方法
  *
  * @param jsonMsg
  */
 public void receiveMessage2(String jsonMsg) {
  log.info("[开始消费REDIS消息队列phoneTest2数据...]");
  try {
   System.out.println(jsonMsg);
   /**
    *  此处执行自己代码逻辑 例如 插入 删除操作数据库等
    */

   log.info("[消费REDIS消息队列phoneTest2数据成功.]");
  } catch (Exception e) {
   log.error("[消费REDIS消息队列phoneTest2数据失败,失败信息:{}]", e.getMessage());
  }
  latch.countDown();
 }
}

接口测试类
package com.mei.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class PublisherController {
 private static final Logger log = LoggerFactory.getLogger(PublisherController.class);
 @Autowired
 private StringRedisTemplate stringRedisTemplate;
 @GetMapping(value = "pub/{id}")
 public String pubMsg(@PathVariable String id){
  stringRedisTemplate.convertAndSend("phone","223333");
  stringRedisTemplate.convertAndSend("phoneTest2","34555665");
  log.info("Publisher sendes Topic... ");
  return "success";
 }
}


2. 分布式锁

Redisson依赖
<dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.6.5</version>
</dependency>

package com.mei.controller;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ResponseBody
public class RedisLockController {
 @Autowired
 private Redisson redisson;

 @RequestMapping("/test")
 public void deductStock(){
  String lockKey = "lockKey";
  // 获取锁对象
  RLock lock = redisson.getLock(lockKey);

  // 加锁、并且有锁续命功能
 lock.lock();
 try {
  // 业务处理
 }finally {
  // 去锁
  lock.unlock();
 }
 }
}

上一篇:day 7


下一篇:《java疯狂讲义》collections中的工具类部分使用(自用)