SpringBoot整合Redis和Redision锁

参考文章

1.Redis

1.导入依赖

        <!--Redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>

2.配置里面加入redis相关

spring:data:redis:# 连接地址host: 192.168.101.128# 端口port: 6379# 数据库database: 0# 用户名,如果有# username:# 密码,如果有password: 123456# 连接超时connect-timeout: 5s# 读超时timeout: 5s

3.写相关配置类

package com.example.springbootdemo3.config;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Value("${spring.data.redis.host}")private String redisHost;@Value("${spring.data.redis.port}")private int redisPort;@Value("${spring.data.redis.database}")private int redisDatabase;@Value("${spring.data.redis.password}")private String redisPassword;@Beanpublic RedisConnectionFactory redisConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);config.setDatabase(redisDatabase);config.setPassword(redisPassword);// 配置连接池GenericObjectPoolConfig<Object> poolConfig = new GenericObjectPoolConfig<>();poolConfig.setMaxTotal(10);       // 连接池中的最大连接数poolConfig.setMaxIdle(5);         // 连接池中的最大空闲连接数poolConfig.setMinIdle(1);         // 连接池中的最小空闲连接数poolConfig.setMaxWaitMillis(2000); // 连接池获取连接的最大等待时间// 创建一个带有连接池配置的 Lettuce 客户端配置LettucePoolingClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build();return new LettuceConnectionFactory(config, clientConfig);}@Beanpublic RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericToStringSerializer<>(Object.class));return template;}
}

4.可以了,测试一下试试

package com.example.springbootdemo3.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;/*** description* Redis配置类* @author PC 2025/02/20 21:03*/
@RestController
@RequestMapping("/redis-test")
public class RedisTestController {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@GetMapping("/get")public Object get(@RequestParam("key") String key) {return redisTemplate.opsForValue().get(key);}@PostMappingpublic String set(@RequestParam("key") String key, @RequestParam("value") String value) {redisTemplate.opsForValue().set(key, value);return "success";}
}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2.Redision锁

1.添加依赖

      <!--Redis锁依赖--><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.16.8</version> <!-- 请使用最新版本 --></dependency>

2.添加配置

package com.example.springbootdemo3.config;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;/*** description* Rdis锁配置类* @author PC 2025/02/20 23:48*/
@Configuration
public class RedissonClientConfig {@Value("${spring.data.redis.host}")private String redisHost;@Value("${spring.data.redis.port}")private int redisPort;@Value("${spring.data.redis.password}")private String redisPassword;@Beanpublic RedissonClient redissonClient() {Config config = new Config();String address = "redis://" + redisHost + ":" + redisPort;config.useSingleServer()// 设置你的Redis服务器地址和端口.setAddress(address)// 设置密码.setPassword(redisPassword);RedissonClient redisson = Redisson.create(config);return redisson;}
}

3.然后就可以测试用一用了

package com.example.springbootdemo3.controller;import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;/*** description** @author PC 2025/02/20 21:03*/
@RestController
@RequestMapping("/redis-test")
public class RedisTestController {@Autowiredprivate RedissonClient redissonClient;@PostMapping("/lock")public String lock() {RLock lock = redissonClient.getLock("myLock");boolean b = lock.tryLock();if(b){try {System.out.println("加锁成功");Thread.sleep(10000);return "success";} catch (InterruptedException e) {e.printStackTrace();}finally {lock.unlock();}return "success";}else {return "fail";}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/70413.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++笔记之标准库中的std::copy 和 std::assign 作用于 std::vector

C++笔记之标准库中的std::copy 和 std::assign 作用于 std::vector code review! 文章目录 C++笔记之标准库中的std::copy 和 std::assign 作用于 std::vector1. `std::copy`1.1.用法1.2.示例2.`std::vector::assign`2.1.用法2.2.示例3.区别总结4.支持assign的容器和不支持ass…

C# 背景 透明 抗锯齿 (效果完美)

主要是通过 P/Invoke 技术调用 Windows API 函数 gdi32.dll/user32.dll&#xff0c;同时定义了一些结构体来配合这些 API 函数的使用&#xff0c;常用于处理图形绘制、窗口显示等操作。 运行查看效果 局部放大&#xff0c;抗锯齿效果很不错,尾巴毛毛清晰可见。 using System; u…

前端常见面试题-2025

vue4.0 Vue.js 4.0 是在 2021 年 9 月发布。Vue.js 4.0 是 Vue.js 的一个重要版本&#xff0c;引入了许多新特性和改进&#xff0c;旨在提升开发者的体验和性能。以下是一些关键的更新和新特性&#xff1a; Composition API 重构&#xff1a;Vue 3 引入了 Composition API 作为…

【工具插件类教学】实现运行时2D物体交互的利器Runtime2DTransformInteractor

目录 ​编辑 1. 插件核心功能 1.1 基础变换操作 1.2 高级特性 2. 安装与配置 2.1 导入插件 2.2 配置控制器参数 2.3 为物体添加交互功能 3. 使用示例 3.1 基础操作演示 3.2 多选与批量操作 3.3 自定义光标与外观 4. 高级配置技巧 4.1 动态调整包围框控件尺寸 4.…

alt+tab切换导致linux桌面卡死的急救方案

环境 debian12 gnome43.9 解决办法 观察状态栏&#xff0c;其实系统是没有完全死机的&#xff0c;而且gnome也可能没有完全死机。 1. alt f4 关闭桌面上的程序&#xff0c;因为这个方案是我刚刚看到的&#xff0c;所以不确定能不能用&#xff0c;比起重启系统&#xff0c;…

mac相关命令

显示和隐藏usr等隐藏文件文件 terminal输入: defaults write com.apple.Finder AppleShowAllFiles YESdefaults write com.apple.Finder AppleShowAllFiles NO让.bashrc每次启动shell自动生效 编辑vim ~/.bash_profile 文件, 加上 if [ -f ~/.bashrc ]; then. ~/.bashrc fi注…

Lineageos 22.1(Android 15)Launcer简单调整初始化配置

一、前言 Launcer的初始化配置主要在如下的xml文件夹下&#xff0c;默认读取的5x5 这里我们把device_profiles调整一下&#xff0c;然后新建一个default_workspace_my.xml作为我们自己的配置就行。 二、配置 注意Lineageos 的Launcer是在lineageos/packages/apps/Trebuchet…

排查JVM的一些命令

查看JVM相关信息的方法 环境&#xff1a; Win10, jdk17 查看端口的Pid netstat -ano | findstr <端口号>列出当前运行的JVM进程 ## 用于输出JVM中运行的进程状态信息。通过jps&#xff0c;可以快速获取Java进程的PID&#xff08;进程标识符&#xff09;&#xff0c; …

DeepSeek 助力 Vue 开发:打造丝滑的瀑布流布局(Masonry Layout)

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…

解决弹窗中form表单中按下tab键不切换的问题

开发过程中碰到el-dialog中使用的form组件&#xff0c;按下键盘tab键不会切换到下一个&#xff0c;普通页面就正常切换。 解决办法 <ElInputref"newPWDInput"v-model"newPWD":clearable"true":maxlength"16":tabindex"2&quo…

封装一个vue3控制并行任务数量的组合式函数

一. 使用场景 使用环境&#xff1a; vue3 当需要处理多个异步任务时&#xff0c;想要控制并行异步任务的数量&#xff0c;不想所有任务同时执行导致产生性能上的问题&#xff0c; 比如当需要同时发起多个网络请求&#xff0c;但又不想一次性发出过多请求导致服务器压力过大或…

最新华为 HCIP-Datacom(H12-821)2025.2.20

最新 HCIP-Datacom&#xff08;H12-821&#xff09;&#xff0c;完整题库请扫描上方二维码访问。 如图所示为某OSPF网络&#xff0c;已知R1和R2已,成功建立邻接关系&#xff0c;现一工程师在R2上配置了图中命令。那么在R2上查看LSDB时&#xff0c;可能存在以下哪些LSA? A&…

MongoDB between ... and ... 操作

个人博客地址&#xff1a;​​​​​​​MongoDB between ... and ... 操作 | 一张假钞的真实世界 MongoDB中类似SQL的between and操作可以采用如下语法&#xff1a; db.collection.find( { field: { $gt: value1, $lt: value2 } } );

vue2和vue3的按需引入的详细对比通俗易懂

以下是 Vue2 与 Vue3 按需引入的对比详解&#xff0c;用最简单的语言和场景说明差异&#xff1a; 一、按需引入的本质 目标&#xff1a;只打包项目中实际用到的代码&#xff08;组件、API&#xff09;&#xff0c;减少最终文件体积。类比&#xff1a;去餐厅点餐&#xff0c;只…

bash+crontab充当半个守护进程的歪招

两个cpolar下的不同程序&#xff0c;都需要定时监测&#xff0c;以免程序没有再运行。有点类似半个守护进程吧。但是守护进程不会写&#xff0c;咋搞&#xff1f;就用这个办法临时当下守门员。这里主要为了备忘xpgrep -各类参数的用法。 #!/bin/bashif pgrep -fl "check_…

Web自动化之Selenium 超详细教程(python)

Selenium是一个开源的基于WebDriver实现的自动化测试工具。WebDriver提供了一套完整的API来控制浏览器&#xff0c;模拟用户的各种操作&#xff0c;如点击、输入文本、获取页面元素等。通过Selenium&#xff0c;我们可以编写自动化脚本&#xff0c;实现网页的自动化测试、数据采…

如何调用 DeepSeek API:详细教程与示例

目录 一、准备工作 二、DeepSeek API 调用步骤 1. 选择 API 端点 2. 构建 API 请求 3. 发送请求并处理响应 三、Python 示例&#xff1a;调用 DeepSeek API 1. 安装依赖 2. 编写代码 3. 运行代码 四、常见问题及解决方法 1. API 调用返回 401 错误 2. API 调用返回…

基于flask+vue的租房信息可视化系统

✔️本项目利用 python 网络爬虫抓取某租房网站的租房信息&#xff0c;完成数据清洗和结构化&#xff0c;存储到数据库中&#xff0c;搭建web系统对各个市区的租金、房源信息进行展示&#xff0c;根据各种条件对租金进行预测。 1、数据概览 ​ 将爬取到的数据进行展示&#xff…

磐维数据库双中心容灾流复制集群搭建

1. 架构 磐维数据库PanWeiDB V2.0.0基于gs_sdr工具&#xff0c;在不借助额外存储介质的情况下实现跨Region的异地容灾。提供流式容灾搭建&#xff0c;容灾升主&#xff0c;计划内主备切换&#xff0c;容灾解除、容灾状态监控等功能。 2. 部署双中心磐维集群 2.1. 主集群 角色…

wordpress企业官网建站的常用功能

WordPress 是一个功能强大的内容管理系统(CMS)&#xff0c;广泛用于企业官网的建设。以下是企业官网建站中常用的 WordPress 功能&#xff1a; 1. 页面管理 自定义页面模板&#xff1a;企业官网通常需要多种页面布局&#xff0c;如首页、关于我们、产品展示、联系我们等。Wor…