Redis 的两个应用描述
阻塞队列:使用 Redis 实现分布式场景下,JUC 中 BlocingQueue 的功能。
实现的理论基础,Redis 是单线程的一个系统,锁提供给我们的命令都是原子性的。我们就利用原子性。使用到的命令是 lpush
和 brpop()
两个方法,前者是从左边将数据压入到列表中,后者是从列表的右边出栈,如果超过一段时间列表中没有数据,则超时,有数据则返回。
分布式锁: 在分布式的场景下,多台服务器上的线程申请同一把锁。
实现的理论基础是 setnx 原子性。当多个请求执行 setnx 命令后,redis 是单线程处理请求的,所以可以保证执行 setnx 不存在临界区,所以当一个线程创建string 成功后,下一个线程就会创建失败。这样以来,就只能一个线程获得这把锁。当申请到锁的线程最后再将string删除,其他的线程就能申请到锁了。
阻塞队列
代码如下所示,
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;
/**
* @className: RedisDistributeLock
* @Description:
* @Author: wangyifei
* @Date: 2023/2/24 16:22
*/
public class RedisDistributeLock {
public static void main(String[] args) {
builder();
builder();
}
public static void builder(){
LockGeter lockGeter = new LockGeter();
lockGeter.connectRedis();
new Thread(lockGeter).start();
}
public static class LockGeter implements Runnable{
private Jedis jedis = null ;
public Long getGetID(){
return Thread.currentThread().getId();
}
public void connectRedis(){
jedis = new Jedis("192.168.175.112", 6379);
jedis.ping();
}
public boolean getLock(){
long rs = jedis.setnx("dist_lock", getGetID().toString());
if(rs == 0){
System.out.println(getGetID().toString() + ":get lock failed");
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return true ;
}else{
System.out.println(getGetID() + ":get lock");
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
jedis.del("dist_lock");
System.out.println(getGetID() + ":release lock");
return false;
}
}
@Override
public void run() {
while(getLock()){}
jedis.close();
}
}
}
分布锁
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
/**
* @className: RedisBlockingQueue
* @Description:
* @Author: wangyifei
* @Date: 2023/2/26 14:52
*/
public class RedisBlockingQueue {
public static String BLOCING_QUEUE_NAME = "blicking_queue";
public static String REDIS_IP = "192.168.175.112" ;
public static class Consumer implements Runnable{
private Jedis jedis = null ;
public void lanch(){
jedis = new Jedis(REDIS_IP, 6379);
}
public void poll(){
List<String> brpop = jedis.brpop(5, BLOCING_QUEUE_NAME);
String collect = brpop.stream().collect(Collectors.joining(","));
System.out.println("consumer:" + collect);
}
@Override
public void run() {
while(true){
poll();
}
}
}
public static class Producer implements Runnable{
private Jedis jedis = null ;
public void lanch(){
jedis = new Jedis(REDIS_IP, 6379);
}
public void producer(){
int i = ThreadLocalRandom.current().nextInt(10);
long lpush = jedis.lpush(BLOCING_QUEUE_NAME, "msg" + i);
System.out.println("push " + i);
}
@Override
public void run() {
while(true){
producer();
}
}
}
public static void main(String[] args) {
Producer p1 = new Producer();
Producer p2 = new Producer();
Producer p3 = new Producer();
p1.lanch();
p2.lanch();
p3.lanch();
new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
Consumer c1 = new Consumer();
Consumer c2 = new Consumer();
c1.lanch();
c2.lanch();
new Thread(c1).start();
new Thread(c2).start();
}
}