栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


 
 @Bean
 public MessageListenerAdapter listenerAdapter(ReceiverRedisMessage  receiver){
  return new MessageListenerAdapter(receiver,"receiveMessage");
 }
 
 @Bean
 public MessageListenerAdapter listenerAdapterTest2(ReceiverRedisMessage  receiver){
  return new MessageListenerAdapter(receiver,"receiveMessage2");
 }
 
 @Bean
 ReceiverRedisMessage receiver(CountDownLatch latch) {
  return new ReceiverRedisMessage(latch);
 }


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


 
 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();
 }

 
 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依赖

            org.redisson
            redisson
            3.6.5


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();
 }
 }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697153.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号