點擊上方藍字關注我們
list 原理說明
Redis 的 list 是按照插入順序排序的字符串鏈表。
如圖所示,可以通過 lpush 和 rpop 或者 rpush 和 lpop 實現消息隊列。
1 lpush 和 rpop
2 rpush 和 lpop
消息隊列功能實現
引入 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發送和接收接口
package com.sb.service;
public interface MQService {
void produce(String string);
void consume();
}
MQ發送和接收實現類
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發送和接收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();
}
}
}
消息隊列功能測試
調用 http://localhost:8080/api/produce 接口往隊列裏面添加 a、b、c、d元素。
調用 http://localhost:8080/api/consume 消費隊列裏面的元素。
blpop 理論說明
blpop 命令
blpop key1...keyN timeout
blpop 代碼實現
public void blockingConsume() {
List<Object> obj = redisTemplate.executePipelined(new RedisCallback<Object>() {
@Nullable
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
//隊列沒有元素會阻塞操作,直到隊列獲取新的元素或超時
return connection.bLPop(TIME_OUT, MESSAGE_KEY.getBytes());
}
},new StringRedisSerializer());
for (Object str: obj) {
log.info("blockingConsume : {}", str);
}
}
阻塞線程每隔10s超時執行一次。該方法解決了 CPU 空轉的問題。
用java做一個能賺錢的微信羣聊機器人(PC協議)
【案例】有什麼副業是可以做的?
ZooKeeper典型應用場景
無須下載,瀏覽器就能完成接口調試,快來試試
戳這兒
本文分享自微信公衆號 - 俠夢的開發筆記(xmdevnote)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。