当前位置: 首页 > news >正文

泊头网站建设甘肃深圳seo优化外包

泊头网站建设甘肃,深圳seo优化外包,花卉网站建设策划,衡阳网站建设公司文章目录 1.前言2.使用方式1. 添加Redisson依赖:2. 配置Redis连接信息3. 使用场景3.1. 分布式锁3.2. 限流器(Rate Limiter)3.3. 可过期的对象(Expirable Object)3.4. 信号量(Semaphore)3.5. 分布…

文章目录

  • 1.前言
  • 2.使用方式
    • 1. 添加Redisson依赖:
    • 2. 配置Redis连接信息
    • 3. 使用场景
      • 3.1. 分布式锁
      • 3.2. 限流器(Rate Limiter)
      • 3.3. 可过期的对象(Expirable Object)
      • 3.4. 信号量(Semaphore)
      • 3.5. 分布式调度器(Distributed Scheduler)
  • 5. 源码地址
  • 6. Redis从入门到精通系列文章
  • 7.参考文档

在这里插入图片描述

1.前言

Redisson是一个基于Redis的分布式Java对象和数据结构库,它提供了丰富的功能和易于使用的API,使开发人员能够轻松地在分布式环境中操作和管理数据。

作为一个分布式对象和数据结构库,Redisson提供了许多常见的数据结构和算法的实现,包括通用对象桶、二进制流、地理空间对象桶、BitSet、原子长整型、原子双精度浮点数、话题(订阅分发)、布隆过滤器和基数估计算法。这些数据结构和算法为开发人员提供了处理分布式数据的工具,从而简化了复杂性,提高了效率。简直就是一个Redis的最佳实践框架和最牛X的Redis客户端工具宝箱,基本上覆盖了所有场景

通过Redisson,开发人员可以使用简单而一致的API来存储和检索对象,处理二进制数据,管理地理位置信息,操作位集合,进行原子操作,进行发布-订阅消息传递,实现布隆过滤器和基数估计等功能。Redisson还提供了许多附加功能,如分布式锁、分布式信号量、分布式队列和分布式限流器等,进一步增强了分布式应用的能力。

Redisson的设计目标是提供高性能、可扩展和可靠的分布式数据操作解决方案。它与Redis数据库紧密集成,并利用Redis的特性来实现分布式对象和数据结构的存储和管理。Redisson还支持与Spring框架的无缝集成,使开发人员能够更方便地在Spring应用程序中使用Redisson功能。

所以我们本篇文章了解一下Redisson 在项目实践中最常用的5种场景,分别搞了一个示例方便大家理解
在这里插入图片描述

2.使用方式

1. 添加Redisson依赖:

在Spring Boot项目的pom.xml文件中添加Redisson的依赖。

<dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.15.5</version>
</dependency>

2. 配置Redis连接信息

在Spring Boot项目的application.properties或application.yml文件中配置Redis连接信息,包括主机地址、端口、密码等。例如:

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=

3. 使用场景

3.1. 分布式锁

例如,你可以在Spring Boot的Service类中注入RedissonClient并使用它进行操作:

import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class MyService {@Autowiredprivate RedissonClient redissonClient;public void myMethod() {// 获取分布式锁RLock lock = redissonClient.getLock("myLock");try {// 尝试加锁,如果加锁成功,则执行加锁后的逻辑if (lock.tryLock()) {// 执行加锁后的逻辑// ...}} finally {// 释放锁lock.unlock();}}}

3.2. 限流器(Rate Limiter)

import org.redisson.api.RRateLimiter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class MyService {@Autowiredprivate   RedissonClient redissonClient;public void myMethod() {// 获取限流器RRateLimiter limiter = redissonClient.getRateLimiter("myLimiter");// 定义限流速率,例如每秒最多允许10个操作limiter.trySetRate(RateType.OVERALL, 10, 1, RateIntervalUnit.SECONDS);// 尝试获取许可boolean acquired = limiter.tryAcquire();if (acquired) {// 执行需要限流的操作// ...} else {// 限流逻辑,例如返回错误信息或执行降级处理// ...}}
}

3.3. 可过期的对象(Expirable Object)

在Redis中,Hash结构是一种用于存储键值对的数据结构。每个Hash结构都可以包含多个字段(field)和对应的值(value)。而要为Hash结构中的二级key设置过期时间,可以使用Redisson的RMapCache接口。如果使用Redisson 来实现对Hash结构中二级key的值设置过期时间,其实很简单了。

我们使用redissonClient.getMapCache("myHash")获取一个名为"myHash"的可过期对象的Hash结构。然后,我们可以使用put()方法将具有过期时间的二级键值对存储到Hash结构中,并指定过期时间和时间单位。存储的二级键值对将会在指定的过期时间后自动过期。

setHashValueWithExpiration()方法中,我们传入Hash结构的一级键(hashKey)、二级键(fieldKey)、值(value)、过期时间和时间单位,将值存储到Hash结构中的二级键,并为其设置过期时间。

getHashValue()方法中,我们根据Hash结构的一级键和二级键从Hash结构中获取对应的值。

通过使用Redisson的RMapCache接口,你可以方便地为Hash结构中的二级键值对设置过期时间,并且无需手动处理过期逻辑。Redisson会自动管理过期和清理操作,简化了在分布式环境中使用可过期的Hash结构的开发工作。

import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Component
public class HashService {@Autowiredprivate  RedissonClient redissonClient;public void setHashValueWithExpiration(String hashKey, String fieldKey, Object value, long expirationTime, TimeUnit timeUnit) {RMapCache<String, Object> hash = redissonClient.getMapCache("myHash");hash.put(hashKey, fieldKey, value, expirationTime, timeUnit);}public Object getHashValue(String hashKey, String fieldKey) {RMapCache<String, Object> hash = redissonClient.getMapCache("myHash");return hash.get(hashKey, fieldKey);}
}

3.4. 信号量(Semaphore)

redissonClient.getSemaphore(“mySemaphore”)获取一个名为"mySemaphore"的信号量。然后,使用trySetPermits()方法设置信号量的初始数量,例如设置为10个。在myMethod()方法中,我们使用acquire()方法尝试获取信号量,如果获取到信号量,则执行需要受信号量限制的操作。在操作完成后,使用release()方法释放信号量。

  • 通过使用Redisson的信号量功能,你可以控制在分布式环境中对某个资源的并发访问数量,限制并发访问的能力,从而保护资源的稳定性和可用性。
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class ResourceService {private final RedissonClient redissonClient;@Autowiredpublic ResourceService(RedissonClient redissonClient) {this.redissonClient = redissonClient;}public void accessResource() {RSemaphore resourceSemaphore = redissonClient.getSemaphore("resourceSemaphore");resourceSemaphore.trySetPermits(10);try {resourceSemaphore.acquire();// 执行需要受信号量限制的操作,访问资源// ...} catch (InterruptedException e) {// 处理中断异常// ...} finally {resourceSemaphore.release();}}
}

3.5. 分布式调度器(Distributed Scheduler)

我们使用redissonClient.getMapCache("myHash")获取一个名为"myHash"的可过期对象的Hash结构。然后,我们可以使用put()方法将具有过期时间的二级键值对存储到Hash结构中,并指定过期时间和时间单位。存储的二级键值对将会在指定的过期时间后自动过期。

setHashValueWithExpiration()方法中,我们传入Hash结构的一级键(hashKey)、二级键(fieldKey)、值(value)、过期时间和时间单位,将值存储到Hash结构中的二级键,并为其设置过期时间。

getHashValue()方法中,我们根据Hash结构的一级键和二级键从Hash结构中获取对应的值。

通过使用Redisson的RMapCache接口,你可以方便地为Hash结构中的二级键值对设置过期时间,并且无需手动处理过期逻辑。Redisson会自动管理过期和清理操作,简化了在分布式环境中使用可过期的Hash结构的开发工作。

import org.redisson.api.RScheduledExecutorService;
import org.redisson.api.RedissonClient;
import org.redisson.api.annotation.RInject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Component
public class DistributedScheduler {private final RedissonClient redissonClient;@Autowiredpublic DistributedScheduler(RedissonClient redissonClient) {this.redissonClient = redissonClient;}/*** 安排一个延迟执行的分布式任务** @param task      要执行的任务* @param delay     延迟时间* @param timeUnit  时间单位*/public void scheduleTask(Runnable task, long delay, TimeUnit timeUnit) {RScheduledExecutorService executorService = redissonClient.getExecutorService("myScheduler");executorService.schedule(task, delay, timeUnit);}/*** 安排一个以固定速率重复执行的分布式任务** @param task          要执行的任务* @param initialDelay  初始延迟时间* @param period        重复执行的周期* @param timeUnit      时间单位*/public void scheduleTaskAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit timeUnit) {RScheduledExecutorService executorService = redissonClient.getExecutorService("myScheduler");executorService.scheduleAtFixedRate(task, initialDelay, period, timeUnit);}
}

5. 源码地址

https://github.com/wangshuai67/Redis-Tutorial-2023

6. Redis从入门到精通系列文章

  • 《Redis使用Lua脚本和Redisson来保证库存扣减中的原子性和一致性》
  • 《SpringBoot Redis 使用Lettuce和Jedis配置哨兵模式》
  • 《Redis【应用篇】之RedisTemplate基本操作》
  • 《Redis 从入门到精通【实践篇】之SpringBoot配置Redis多数据源》
  • 《Redis 从入门到精通【进阶篇】之三分钟了解Redis HyperLogLog 数据结构》
  • 《Redis 从入门到精通【进阶篇】之三分钟了解Redis地理位置数据结构GeoHash》
  • 《Redis 从入门到精通【进阶篇】之高可用哨兵机制(Redis Sentinel)详解》
  • 《Redis 从入门到精通【进阶篇】之redis主从复制详解》
  • 《Redis 从入门到精通【进阶篇】之Redis事务详解》
  • 《Redis从入门到精通【进阶篇】之对象机制详解》
  • 《Redis从入门到精通【进阶篇】之消息传递发布订阅模式详解》
  • 《Redis从入门到精通【进阶篇】之持久化 AOF详解》
  • 《Redis从入门到精通【进阶篇】之持久化RDB详解》
  • 《Redis从入门到精通【高阶篇】之底层数据结构字典(Dictionary)详解》
  • 《Redis从入门到精通【高阶篇】之底层数据结构快表QuickList详解》
  • 《Redis从入门到精通【高阶篇】之底层数据结构简单动态字符串(SDS)详解》
  • 《Redis从入门到精通【高阶篇】之底层数据结构压缩列表(ZipList)详解》
  • 《Redis从入门到精通【进阶篇】之数据类型Stream详解和使用示例》
    在这里插入图片描述大家好,我是冰点,今天的【Redis实践篇】使用Redisson 优雅实现项目实践过程中的5种场景,全部内容就是这些。如果你有疑问或见解可以在评论区留言。

7.参考文档

Redisson官方文档 https://github.com/redisson/redisson/wiki/Table-of-Content


文章转载自:
http://sootiness.c7496.cn
http://subshrub.c7496.cn
http://precinct.c7496.cn
http://stainability.c7496.cn
http://kleptocracy.c7496.cn
http://gonna.c7496.cn
http://rustless.c7496.cn
http://constraint.c7496.cn
http://overstaff.c7496.cn
http://mechanoreception.c7496.cn
http://frier.c7496.cn
http://filibeg.c7496.cn
http://imide.c7496.cn
http://personage.c7496.cn
http://rtm.c7496.cn
http://pinochle.c7496.cn
http://applausively.c7496.cn
http://suite.c7496.cn
http://eruptive.c7496.cn
http://formulation.c7496.cn
http://isoparametric.c7496.cn
http://crabbery.c7496.cn
http://intrigue.c7496.cn
http://beefalo.c7496.cn
http://anciently.c7496.cn
http://digitorium.c7496.cn
http://countryward.c7496.cn
http://aweary.c7496.cn
http://plumbic.c7496.cn
http://superacid.c7496.cn
http://sore.c7496.cn
http://schoolbook.c7496.cn
http://actinoid.c7496.cn
http://phototroph.c7496.cn
http://anthony.c7496.cn
http://dapperling.c7496.cn
http://machineable.c7496.cn
http://longipennate.c7496.cn
http://thyrse.c7496.cn
http://benighted.c7496.cn
http://cottonseed.c7496.cn
http://clavicorn.c7496.cn
http://cycas.c7496.cn
http://crenation.c7496.cn
http://subtotal.c7496.cn
http://ragtag.c7496.cn
http://disgusted.c7496.cn
http://suboxide.c7496.cn
http://electrolyte.c7496.cn
http://hydroxylate.c7496.cn
http://siwan.c7496.cn
http://millinery.c7496.cn
http://psywar.c7496.cn
http://dicentra.c7496.cn
http://victualage.c7496.cn
http://sprain.c7496.cn
http://cytase.c7496.cn
http://cursed.c7496.cn
http://jody.c7496.cn
http://smithwork.c7496.cn
http://sabbathly.c7496.cn
http://wring.c7496.cn
http://atlantis.c7496.cn
http://earthlight.c7496.cn
http://adjusted.c7496.cn
http://acrylate.c7496.cn
http://japanning.c7496.cn
http://cesura.c7496.cn
http://landline.c7496.cn
http://canton.c7496.cn
http://phanerogamic.c7496.cn
http://normocytic.c7496.cn
http://enmesh.c7496.cn
http://scalpriform.c7496.cn
http://radular.c7496.cn
http://galactagogue.c7496.cn
http://fund.c7496.cn
http://radioman.c7496.cn
http://distrainee.c7496.cn
http://conspire.c7496.cn
http://inhospitality.c7496.cn
http://fend.c7496.cn
http://sanguineous.c7496.cn
http://extortionary.c7496.cn
http://rimpled.c7496.cn
http://echinated.c7496.cn
http://skibobber.c7496.cn
http://ingenerate.c7496.cn
http://lacunate.c7496.cn
http://outstation.c7496.cn
http://indusiate.c7496.cn
http://tactless.c7496.cn
http://footslogger.c7496.cn
http://gothicism.c7496.cn
http://tawse.c7496.cn
http://procurance.c7496.cn
http://reappoint.c7496.cn
http://antheap.c7496.cn
http://melanogenesis.c7496.cn
http://erythropsin.c7496.cn
http://www.zhongyajixie.com/news/90446.html

相关文章:

  • 广州富邦物流网站建设软文代发布
  • 网站首页轮播图怎么做的苏州百度推广分公司电话
  • 自建网站的步骤网络营销专业大学排名
  • 大型网站建设招商电子商务网站
  • 域名已买 可以找其它人做网站吗想学网络营销怎么学
  • 影视视频网站怎么做seo专员的工作内容
  • 动易网站频道栏目字体大小修改站长素材音效网
  • 完善政府门户网站建设东莞网站推广优化公司
  • 网站数据接口怎么做视频剪辑培训机构哪个好
  • 有效的网站推广方式aso优化排名
  • 扶贫办网站建设宁波seo教程网
  • 网站建设的自查报告网页浏览器
  • 东阳便宜自适应网站建设优惠互联网宣传方式有哪些
  • vs做网站如何发布做销售找客户渠道
  • 微信如何创建自己的公众号周口seo推广
  • dede网站重新安装百度搜索引擎推广收费标准
  • 学电商需要多少钱seo怎么做新手入门
  • 网站服务器的作用海底捞口碑营销案例
  • 有哪些做的很漂亮的网站网页制作成品模板网站
  • 网站界面设计套题启动互联全网营销推广
  • 北京市政府谷歌排名优化入门教程
  • 兰州企业网站制作网店培训
  • 萍乡做网站杭州百度推广代理公司哪家好
  • 398做网站彩铃网络营销的好处和优势
  • 专业的销售网站seo刷点击软件
  • 昆明建设局网站号码软文街官方网站
  • 网站怎么做微信支付宝成都seo正规优化
  • 后端开发和前端开发哪个工资高宁波seo关键词排名
  • 卦神岭做网站汕头网站建设优化
  • 如何做自己的游戏网站太原做推广营销