Redission的RSemaphore信号量有什么作用?

news/2025/2/22 6:27:56/

一、概述:

RSemaphore是Redisson基于Redis实现的分布式信号量,用于限制同时访问共享区域的线程数量。与Java的Semaphore相似,RSemaphore也有一个acquire()方法用于获取资源,但在获取资源的顺序上是不可预测的,因此它是一种非公平锁。

二、示例

下面是一个使用RSemaphore的示例代码:

  1. 添加依赖
    在pom.xml文件中添加以下依赖:
<dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.16.4</version>
</dependency>
  1. 配置Redisson
    在application.properties文件中添加以下配置:
# Redis服务器地址和端口号
spring.redis.host=localhost
spring.redis.port=6379
# Redisson配置
spring.redisson.address=redis://${spring.redis.host}:${spring.redis.port}
spring.redisson.password=your_password # 如果有密码,则设置为your_password,否则留空
  1. 配置redission
    创建MyRedissonConfig,配置端口,host
@Configuration
public class MyRedissonConfig {/*** 注入客户端实例对象*/@Bean(destroyMethod="shutdown")public RedissonClient redisson(@Value("${spring.redis.host}") String host, @Value("${spring.redis.port}")String port) throws IOException {// 1.创建配置Config config = new Config();config.useSingleServer().setAddress("redis://" + host + ":" + port);// 单节点模式
//        config.useSingleServer().setAddress("rediss://" + host + ":" + port);// 使用安全连接
//        config.useClusterServers().addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");// 集群模式// 2.创建redisson客户端实例RedissonClient redissonClient = Redisson.create(config);return redissonClient;}}
  1. 具有示例1

在这个示例中,我们创建了一个Spring Boot应用程序,并使用@Autowired注解将RedissonClient注入到应用程序中。我们使用@Scheduled注解来定期执行run()方法,该方法使用RSemaphore来限制同时访问共享区域的线程数量。与之前的示例相同,我们尝试获取信号量,执行需要限制访问数量的代码,然后释放信号量。

import org.redisson.Redisson;  
import org.redisson.api.RSemaphore;  
import org.redisson.api.RedissonClient;  
import org.redisson.config.Config;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.scheduling.annotation.EnableScheduling;  
import org.springframework.scheduling.annotation.Scheduled;  @SpringBootApplication  
@EnableScheduling  
public class RSemaphoreSpringBootExample {  @Autowired  private RedissonClient redissonClient;  public static void main(String[] args) {  SpringApplication.run(RSemaphoreSpringBootExample.class, args);  }  @Scheduled(fixedDelay = 5000)  public void run() {  RSemaphore semaphore = redissonClient.getSemaphore("mySemaphore");  // 尝试获取信号量,最多同时访问3个线程  semaphore.tryAcquire(3);  // 执行需要限制访问数量的代码  for (int i = 0; i < 10; i++) {  System.out.println("Thread " + Thread.currentThread().getId() + " is running.");  }  // 释放信号量  semaphore.release(3);  }  
}
  1. 示例2

在这个示例中,我们定义了两个服务类,ProductService和ProductSemaphoreService。ProductService类用于保存和获取商品,而ProductSemaphoreService类使用Redisson的信号量(RSemaphore)来限制同时保存商品的线程数量。

在ProductSemaphoreService中,我们获取了一个名为"productSaveSemaphore"的信号量(使用RedissonClient的getSemaphore方法),并使用tryAcquire()方法尝试获取信号量。如果信号量可用,则执行保存商品的操作,并在finally块中释放信号量。

这个示例中的商品类(Product)可以根据实际情况进行定义。请注意,这个示例中使用的是Redisson的Map来存储商品,你可以根据需要替换为其他适合的存储方式。同时,需要根据实际情况配置Redisson的连接信息和信号量名称。

@Service  public class ProductService {  @Autowired  private RedissonClient redissonClient;  public void saveProduct(Product product) {  RMap<String, Product> productMap = redissonClient.getMap("products");  productMap.put(product.getId(), product);  }  public Product getProduct(String id) {  RMap<String, Product> productMap = redissonClient.getMap("products");  return productMap.get(id);  }  }  @Service  public class ProductSemaphoreService {  @Autowired  private RedissonClient redissonClient;  public void saveProductWithSemaphore(Product product) {  RSemaphore semaphore = redissonClient.getSemaphore("productSaveSemaphore");  semaphore.tryAcquire();  try {  ProductService productService = new ProductService();  productService.saveProduct(product);  } finally {  semaphore.release();  }  }  }  

http://www.ppmy.cn/news/701011.html

相关文章

C#中ref与out语法的使用

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { private int Add(int x, int y)//值参数 { x x y;//对x进行加y操作 return x;//返回x } private int Add(ref in…

苹果Mac键盘锁住了怎么解决?

日常使用电脑的时候&#xff0c;经常会遇到一开机就发现键盘什么键都按不了的情况&#xff0c;刚开始以为是死机&#xff0c;检测后发现是键盘被锁定了&#xff0c;是按到什么不知名的快捷键导致的。那么要怎么解决mac键盘被锁住的问题&#xff1f;一起看看吧&#xff01; 解决…

ubuntu下小键盘不能用

ubuntu 14.04 系统设置”&#xff0c;在最下面一行找到“通用辅助功能”并点开 选择“指向和单击”&#xff0c;在这里有一个叫做“鼠标键”的选项&#xff0c;把这个关了就可以 了 ubuntu 20.04

你的苹果Mac键盘锁住了要怎么办呢

那么要怎么解决mac键盘被锁住的问题&#xff1f;一起看看吧&#xff01;记得收藏哦。 解决方法&#xff1a; 1.在屏幕左上角找到“苹果”图标&#xff0c;打开“系统偏好设置”。 2.打开“键盘”设置。 3.将下图中的红色勾选出除掉。 4.查看“键盘”快捷盘。 5.使用controlf1…

如果你的苹果Mac键盘锁住了要怎么办呢?快来看看此教程吧!

日常使用电脑的时候&#xff0c;经常会遇到一开机就发现键盘什么键都按不了的情况&#xff0c;刚开始以为是死机&#xff0c;检测后发现是键盘被锁定了&#xff0c;是按到什么不知名的快捷键导致的&#xff1f;那么要怎么解决mac键盘被锁住的问题&#xff1f;一起看看吧&#x…

计算机f2锁定用户,台式电脑键盘被锁住按什么键恢复 点锁定直接锁定计算机

导读:谈到锁住,大家应该都熟悉,有朋友问台式电脑键盘锁是哪个键,还有人问台式电脑键盘被锁住按什么键恢复,这到底是咋回事?事实上台式电脑键盘被锁住按什么键恢复呢,以下是小编为你精心整理的台式电脑键盘被锁住按什么键恢复,以供大家参考借鉴! 台式电脑键盘被锁住按什…

linux系统怎么禁用键盘,桌面应用|如何在 Linux 下锁住键盘和鼠标而不锁屏

我四岁的侄女是个好奇的孩子&#xff0c;她非常喜爱“阿凡达”电影&#xff0c;当阿凡达电影在播放时&#xff0c;她是如此的专注&#xff0c;好似眼睛粘在了屏幕上。但问题是当她观看电影时&#xff0c;她经常会碰到键盘上的某个键或者移动了鼠标&#xff0c;又或者是点击了鼠…