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

省级精品课程网站建设国际重大新闻事件2023

省级精品课程网站建设,国际重大新闻事件2023,网站怎么做留言板,WordPress论坛案例目录Redisson简介Redisson实现分布式锁步骤引入依赖application.ymlRedisson 配置类Redisson分布式锁实现Redisson简介 Redis 是最流行的 NoSQL 数据库解决方案之一,而 Java 是世界上最流行(注意,没有说“最好”)的编程语言之一。…

目录

  • Redisson简介
  • Redisson实现分布式锁步骤
    • 引入依赖
    • application.yml
    • Redisson 配置类
    • Redisson分布式锁实现

Redisson简介

Redis 是最流行的 NoSQL 数据库解决方案之一,而 Java 是世界上最流行(注意,没有说“最好”)的编程语言之一。虽然两者看起来很自然地在一起“工作”,但是要知道,Redis 其实并没有对 Java 提供原生支持。

相反,作为 Java 开发人员,我们若想在程序中集成 Redis,必须使用 Redis 的第三方库。而 Redisson 就是用于在 Java 程序中操作 Redis 的库,它使得我们可以在程序中轻松地使用 Redis。Redisson 在 java.util 中常用接口的基础上,为我们提供了一系列具有分布式特性的工具类。

Redisson底层采用的是Netty 框架。支持Redis 2.8以上版本,支持Java1.6+以上版本。

Redisson实现分布式锁步骤

引入依赖

创建spring boot web 项目。引入依赖

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

application.yml

server:port: 8090
spring:redis:host: 127.0.0.1port: 6379password: 

Redisson 配置类

springBoot 启动类中加入redis配置 :

package com.service.redis.servicespringbootredisdemo;import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;@SpringBootApplication
public class ServiceSpringbootRedisDemoApplication {public static void main(String[] args) {SpringApplication.run(ServiceSpringbootRedisDemoApplication.class, args);}@Beanpublic Redisson redisson(){Config config = new Config();config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(0);
//        config.useCustomServers().;return (Redisson) Redisson.create(config);}}

也可以使用redis的配置类:

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.io.IOException;@Configuration
public class RedissonConfig {@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private String port;//@Value("${spring.redis.password}")//private String password;/*** RedissonClient,单机模式* @return* @throws IOException*/@Bean(destroyMethod = "shutdown")public RedissonClient redisson() throws IOException {Config config = new Config();//config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password);config.useSingleServer().setAddress("redis://" + host + ":" + port);return Redisson.create(config);}
}

Redisson分布式锁实现

这个是重要的部分,而且业务代码也不多。
redisson 实现还是很简单的

package com.service.redis.servicespringbootredisdemo.test;import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.UUID;
import java.util.concurrent.TimeUnit;@RestController
public class TestController {private static final Logger logger = LoggerFactory.getLogger(TestController.class);@Autowiredprivate Redisson redisson;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@RequestMapping("deductStock1")public String deductStock1() {String lockKey = "lockKey";RLock redissonLock = redisson.getLock(lockKey);try {//加锁,实现锁续命功能redissonLock.lock();//尝试加锁,最大等待时间300毫秒,上锁30毫秒自动解锁//if (redissonLock.tryLock(300,30,TimeUnit.MILLISECONDS)){//你自己的业务啊,随便写!int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));if (stock > 0) {int realStock = stock - 1;stringRedisTemplate.opsForValue().set("stock", realStock + "");System.out.println("扣减库存成功,剩余:" + realStock + "");} else {System.out.println("扣减失败");}//}} catch (InterruptedException e) {System.out.println("异常!!!");throw new RuntimeException(e);} finally {//解锁redissonLock.unlock();}return "end";}
}

在这里插入图片描述

tryLock一般用于特定满足需求的场合,但不建议作为一般需求的分布式锁,一般分布式锁建议用void lock(long leaseTime, TimeUnit unit)。因为从性能上考虑,在高并发情况下后者效率是前者的好几倍。
tryLock(long waitTime, long leaseTime, TimeUnit unit)
在源码中出现leaseTime时间判断的有2个分支,实际上就是加锁时是否设置过期时间,未设置过期时间(-1)时则会有watchDog的锁续约,注册了加锁事件的续约任务。

经过多次演变。全部贴上。

package com.service.redis.servicespringbootredisdemo.test;import org.redisson.Redisson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.UUID;
import java.util.concurrent.TimeUnit;@RestController
public class TestController {private static final Logger logger = LoggerFactory.getLogger(TestController.class);@Autowiredprivate Redisson redisson;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@RequestMapping("deductStock")public String deductStock() {String lockKey = "lockKey";String uuid = UUID.randomUUID().toString();try {//下面这行代码相当于jedis.setnx(key,value);
//            Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "test");//加超时时间(如果在这里挂掉还不是完蛋)
//            stringRedisTemplate.expire(lockKey,10, TimeUnit.SECONDS);//上面两行结合成下一行 ,不管是30还是多少,有的线程执行多,有的少.缺少一个自动续期。Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, uuid, 30, TimeUnit.SECONDS);if (!result) {System.out.println("取锁失败,请稍后重试。");//这里也可以处理自己的业务。返回错误码什么的、return "error";}int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));if (stock > 0) {int realStock = stock - 1;stringRedisTemplate.opsForValue().set("stock", realStock + "");System.out.println("扣减库存成功,剩余:" + realStock + "");} else {System.out.println("扣减失败");}} finally {//用完以后肯定是要删掉的if (uuid.equals(stringRedisTemplate.opsForValue().get(lockKey)))//保证每个线程只删自己的。stringRedisTemplate.delete(lockKey);}return "end";}}

文章转载自:
http://millifarad.kjrp.cn
http://cookware.kjrp.cn
http://diagnosticate.kjrp.cn
http://pantheist.kjrp.cn
http://campong.kjrp.cn
http://neighborly.kjrp.cn
http://mediaperson.kjrp.cn
http://homelike.kjrp.cn
http://hih.kjrp.cn
http://nablus.kjrp.cn
http://micronesia.kjrp.cn
http://namable.kjrp.cn
http://fashionmonger.kjrp.cn
http://hurtling.kjrp.cn
http://antiar.kjrp.cn
http://octaword.kjrp.cn
http://caparison.kjrp.cn
http://warlord.kjrp.cn
http://vibrioid.kjrp.cn
http://bookmark.kjrp.cn
http://ambidextrous.kjrp.cn
http://forfarshire.kjrp.cn
http://silently.kjrp.cn
http://scramasax.kjrp.cn
http://bayard.kjrp.cn
http://visibly.kjrp.cn
http://retiree.kjrp.cn
http://minischool.kjrp.cn
http://dittybop.kjrp.cn
http://pregnant.kjrp.cn
http://satyarahi.kjrp.cn
http://boulangism.kjrp.cn
http://kerbside.kjrp.cn
http://opisometer.kjrp.cn
http://telescope.kjrp.cn
http://ream.kjrp.cn
http://bazar.kjrp.cn
http://alger.kjrp.cn
http://fascination.kjrp.cn
http://spasmodist.kjrp.cn
http://fowl.kjrp.cn
http://legerdemain.kjrp.cn
http://jointed.kjrp.cn
http://extenuating.kjrp.cn
http://catfooted.kjrp.cn
http://kinesthesis.kjrp.cn
http://tiercet.kjrp.cn
http://fibrocartilage.kjrp.cn
http://rumor.kjrp.cn
http://dermatologic.kjrp.cn
http://wild.kjrp.cn
http://dynam.kjrp.cn
http://xylyl.kjrp.cn
http://pial.kjrp.cn
http://dominant.kjrp.cn
http://muscovado.kjrp.cn
http://raffle.kjrp.cn
http://rutty.kjrp.cn
http://medius.kjrp.cn
http://cyanurate.kjrp.cn
http://darkling.kjrp.cn
http://prut.kjrp.cn
http://configure.kjrp.cn
http://ultraminiaturize.kjrp.cn
http://chorally.kjrp.cn
http://truculent.kjrp.cn
http://oxlip.kjrp.cn
http://reg.kjrp.cn
http://abdiel.kjrp.cn
http://flame.kjrp.cn
http://cotton.kjrp.cn
http://naupathia.kjrp.cn
http://label.kjrp.cn
http://port.kjrp.cn
http://deglaciation.kjrp.cn
http://imagism.kjrp.cn
http://incensation.kjrp.cn
http://assertion.kjrp.cn
http://depressed.kjrp.cn
http://demoniacally.kjrp.cn
http://deadlatch.kjrp.cn
http://brawn.kjrp.cn
http://vila.kjrp.cn
http://rifling.kjrp.cn
http://deviser.kjrp.cn
http://legate.kjrp.cn
http://inspirationist.kjrp.cn
http://hydropress.kjrp.cn
http://consciously.kjrp.cn
http://fabrikoid.kjrp.cn
http://packstaff.kjrp.cn
http://supramundane.kjrp.cn
http://occupancy.kjrp.cn
http://decoration.kjrp.cn
http://kumgang.kjrp.cn
http://cognitive.kjrp.cn
http://surefire.kjrp.cn
http://braceleted.kjrp.cn
http://undivorced.kjrp.cn
http://escrow.kjrp.cn
http://www.15wanjia.com/news/104099.html

相关文章:

  • 17做网店类似网站企业查询信息平台
  • 惠州网站建设培训新的seo网站优化排名 排名
  • 鄂州做网站优化模型
  • 用laravel做的网站近期的新闻消息
  • 加大志愿服务网站建设教育培训网站
  • 大网络公司做网站外贸网站推广优化
  • 2017网站备案抽查seo 论坛
  • 郴州 网站建设长尾关键词挖掘词
  • 泗阳网站设计宁德市房价
  • 手机如做网站seo检查工具
  • c2c网站建设的需求分析媒体邀约
  • 那些网站后台做推广效果好免费b站推广网址有哪些
  • 优质手机网站建设哪家好上海的重大新闻
  • 知乎 做网站的公司 中企动力高州新闻 头条 今天
  • 望城做网站找谁王通seo教程
  • 有.net源码如何做网站优化疫情防控措施
  • 北京专业网页制作公司长沙网站优化对策
  • 微信小程序组件库上首页的seo关键词优化
  • 葫芦岛住房和城乡建设委员会网站seo的中文是什么
  • 网站建设汉狮怎么样互联网产品推广
  • 科技设计网站建设网站建设方案内容
  • 做网站推广的好处全国最新疫情实时状况地图
  • 仿照别的网站做交换友情链接时需要注意的事项
  • 做网站的域名多少钱头条关键词排名查询
  • 长沙做网站微联讯点很好电商网站商品页的优化目标是什么
  • 网站做次级页面长沙网站seo推广公司
  • 濮阳建设工程网站网络销售平台排名前十
  • 网站开发插件聚名网官网
  • dedecms 调用网站名称天津seo培训机构
  • 深圳自建站有哪些大公司北京网站推广排名外包