欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成Redis實(shí)現(xiàn)消息隊(duì)列的方法

 更新時(shí)間:2021年02月10日 09:31:20   作者:新猿一馬  
這篇文章主要介紹了SpringBoot集成Redis實(shí)現(xiàn)消息隊(duì)列的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

list 原理說(shuō)明

Redis 的 list 是按照插入順序排序的字符串鏈表。

如圖所示,可以通過(guò) lpush 和 rpop 或者 rpush 和 lpop 實(shí)現(xiàn)消息隊(duì)列。

1 lpush 和 rpop

2 rpush 和 lpop

消息隊(duì)列功能實(shí)現(xiàn)

引入 Redis 依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

applicat.yml添加Redis配置

spring:
 redis:
  host: 127.0.0.1
  database: 0
  port: 6379
  jedis:
   pool:
    max-active: 256
    max-idle: 8
    min-idle: 1

Redis配置類

package com.sb.config;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
@Configuration
public class RedisConfig {
 
  @Autowired
  private RedisConnectionFactory redisConnectionFactory;
 
  @Bean
  public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
  }
 
}

MQ發(fā)送和接收接口

package com.sb.service;
 
public interface MQService {
 
  void produce(String string);
 
  void consume();
}
 

MQ發(fā)送和接收實(shí)現(xiàn)類

package com.sb.service.impl;
 
import com.sb.service.MQService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
 
@Service
public class MQServiceImpl implements MQService {
 
  private static Logger log = LoggerFactory.getLogger(MQServiceImpl.class);
 
  private static final String MESSAGE_KEY = "message:queue";
 
  @Resource
  private RedisTemplate redisTemplate;
 
  @Override
  public void produce(String string) {
    redisTemplate.opsForList().leftPush(MESSAGE_KEY, string);
  }
 
  @Override
  public void consume() {
    String string = (String) redisTemplate.opsForList().rightPop(MESSAGE_KEY);
    log.info("consume : {}", string);
  }
 
}

MQ發(fā)送和接收API接口

package com.sb.controller;
 
import com.sb.service.MQService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
 
@RestController
@RequestMapping(value="/api")
public class MQController {
 
  @Resource
  private MQService mQService;
 
  @RequestMapping(value = "/produce", method=RequestMethod.GET)
  public void produce(@RequestParam(name = "key") String key) {
    mQService.produce(key);
  }
 
  @RequestMapping(value="/consume", method=RequestMethod.GET)
  public void consume() {
    while (true) {
      mQService.consume();
    }
  }
 
}

消息隊(duì)列功能測(cè)試

調(diào)用 http://localhost:8080/api/produce 接口往隊(duì)列里面添加 a、b、c、d元素。

調(diào)用 http://localhost:8080/api/consume 消費(fèi)隊(duì)列里面的元素。

從截圖我們可以看到,即使當(dāng)隊(duì)列為空,消費(fèi)者依然在不停的 pop 數(shù)據(jù),這就是浪費(fèi)生命的空輪詢。

那如何解決這個(gè)空輪詢的問(wèn)題呢?

你也許會(huì)想使用 Thread.sleep() 讓消費(fèi)者線程隔一段時(shí)間再消費(fèi)。

使用 Thread.sleep() 會(huì)有什么問(wèn)題么?

A 如果生產(chǎn)者速度大于消費(fèi)者消費(fèi)速度,消息隊(duì)列長(zhǎng)度會(huì)一直增大,時(shí)間久了會(huì)占用大量?jī)?nèi)存空間。

B 如果睡眠時(shí)間過(guò)長(zhǎng),這樣不能處理一些時(shí)效性的消息,睡眠時(shí)間過(guò)短,也會(huì)在連接上造成比較大的開銷。

有沒(méi)有更優(yōu)雅和更合適的方式呢?

brpop 和 blpop 實(shí)現(xiàn)阻塞讀取,下面以 blpop 為例來(lái)說(shuō)明問(wèn)題。

blpop 理論說(shuō)明

blpop 命令

blpop key1...keyN timeout

blpop 說(shuō)明

blpop 是阻塞式列表的彈出原語(yǔ)。 當(dāng)給定列表內(nèi)沒(méi)有任何元素可供彈出的時(shí)候, 連接將被 blpop 命令阻塞。直到有另一個(gè)客戶端對(duì)給定的這些 key 的任意一個(gè)執(zhí)行 lpush 或 rpush 命令為止。 

當(dāng)給定多個(gè) key 參數(shù)時(shí),按參數(shù) key 的先后順序依次檢查各個(gè)列表,彈出第一個(gè)非空列表的頭元素。

key1...keyN:表示不同的隊(duì)列名。

timeout:阻塞隊(duì)列超時(shí)時(shí)間。

blpop 代碼實(shí)現(xiàn)

public void blockingConsume() {
  List<Object> obj = redisTemplate.executePipelined(new RedisCallback<Object>() {
    @Nullable
    @Override
    public Object doInRedis(RedisConnection connection) throws DataAccessException {
      //隊(duì)列沒(méi)有元素會(huì)阻塞操作,直到隊(duì)列獲取新的元素或超時(shí)
      return connection.bLPop(TIME_OUT, MESSAGE_KEY.getBytes());
    }
  },new StringRedisSerializer());
 
  for (Object str: obj) {
    log.info("blockingConsume : {}", str);
  }
}

阻塞線程每隔10s超時(shí)執(zhí)行一次。該方法解決了 CPU 空轉(zhuǎn)的問(wèn)題。

到此這篇關(guān)于SpringBoot集成Redis實(shí)現(xiàn)消息隊(duì)列的方法的文章就介紹到這了,更多相關(guān)SpringBoot Redis消息隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis的@SelectProvider注解構(gòu)建動(dòng)態(tài)SQL方式

    MyBatis的@SelectProvider注解構(gòu)建動(dòng)態(tài)SQL方式

    這篇文章主要介紹了MyBatis的@SelectProvider注解構(gòu)建動(dòng)態(tài)SQL方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer解讀

    SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer解讀

    這篇文章主要介紹了SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 手把手寫Spring框架

    手把手寫Spring框架

    Spring是于2003 年興起的一個(gè)輕量級(jí)的Java 開發(fā)框架,由Rod Johnson創(chuàng)建。簡(jiǎn)單來(lái)說(shuō),Spring是一個(gè)分層的JavaSE/EE full-stack(一站式) 輕量級(jí)開源框架
    2021-08-08
  • Java IO流 File類的常用API實(shí)例

    Java IO流 File類的常用API實(shí)例

    這篇文章主要介紹了Java IO流 File類的常用API實(shí)例的相關(guān)資料,需要的朋友參考下吧
    2017-05-05
  • SpringBoot+MyBatis實(shí)現(xiàn)動(dòng)態(tài)字段更新的三種方法

    SpringBoot+MyBatis實(shí)現(xiàn)動(dòng)態(tài)字段更新的三種方法

    字段更新是指在數(shù)據(jù)庫(kù)表中修改特定列的值的操作,這種操作可以通過(guò)多種方式進(jìn)行,具體取決于業(yè)務(wù)需求和技術(shù)環(huán)境,本文給大家介紹了在Spring Boot和MyBatis中,實(shí)現(xiàn)動(dòng)態(tài)更新不固定字段的三種方法,需要的朋友可以參考下
    2025-04-04
  • Java中List與數(shù)組相互轉(zhuǎn)換實(shí)例分析

    Java中List與數(shù)組相互轉(zhuǎn)換實(shí)例分析

    這篇文章主要介紹了Java中List與數(shù)組相互轉(zhuǎn)換的方法,實(shí)例分析了Java中List與數(shù)組相互轉(zhuǎn)換中容易出現(xiàn)的問(wèn)題與相關(guān)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-05-05
  • Java中的枚舉enum詳細(xì)解讀

    Java中的枚舉enum詳細(xì)解讀

    這篇文章主要介紹了Java中的枚舉enum詳細(xì)解讀,當(dāng)我們使用enum關(guān)鍵字開發(fā)一個(gè)枚舉類時(shí),默認(rèn)會(huì)繼承Enum類,而且是一個(gè)final類,當(dāng)有多個(gè)枚舉對(duì)象時(shí),使用逗號(hào) ,隔開,最后一個(gè)用分號(hào);結(jié)尾,需要的朋友可以參考下
    2024-01-01
  • springMVC中的view視圖詳細(xì)解析

    springMVC中的view視圖詳細(xì)解析

    這篇文章主要介紹了springMVC中的view視圖,springMVC視圖的種類很多,默認(rèn)有轉(zhuǎn)發(fā)視圖和重定向視圖,本文就每一種視圖給大家詳細(xì)介紹,需要的朋友可以參考下
    2022-03-03
  • Mybatis批處理、Mysql深分頁(yè)操作

    Mybatis批處理、Mysql深分頁(yè)操作

    這篇文章主要介紹了Mybatis批處理、Mysql深分頁(yè)操作,Mybatis批量操作包括Foreach方式和ExecutorType.BATCH插入操作,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • javaWeb傳收參數(shù)方式總結(jié)示例分析

    javaWeb傳收參數(shù)方式總結(jié)示例分析

    這篇文章主要為大家介紹了javaWeb傳收參數(shù)方式總結(jié)示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08

最新評(píng)論