redis bind多个ip_Spring Boot 中 Redis 的使用
整合 Redis 哨兵模式
引入依赖
在 pox.xml
中引入 org.apache.commons:commons-pool2
和 org.springframework.boot:spring-boot-starter-data-redis
依赖
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
相关配置
在 application.yml
中添加 redis
及哨兵集群的配置
spring:redis:lettuce:pool:# 最大连接数max-active: 8# 最大空闲连接数max-idle: 8# 最大阻塞等待时间(使用负值表示没有限制)max-wait: -1ms# 最小空闲连接数min-idle: 0sentinel:# 哨兵集群主节点名master: mymaster# 哨兵集群各节点nodes: {ip}:{port}, {ip}:{port}, {ip}:{port}
使用 Redis
注意:写入缓存的数据(如实体类)需要实现序列化,否则向 Redis 中存取数据会抛出异常
这里只简单实现对 Redis
的增删改查操作
创建 RedisService
public interface RedisService {/*** 存储缓存* @param key* @param value* @param seconds*/void set(String key, Object value, long seconds);/*** 获取缓存* @param key* @return*/Object get(String key);/*** 删除缓存* @param key*/boolean del(String key);
}
创建 RedisServiceImpl
@Service
public class RedisServiceImpl implements RedisService {@Autowiredprivate RedisTemplate redisTemplate;@Overridepublic void set(String key, Object value, long seconds) {redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);}@Overridepublic Object get(String key) {return redisTemplate.opsForValue().get(key);}@Overridepublic boolean del(String key) {return redisTemplate.delete(key);}
}
解决序列化 Redis key-value 乱码
spring-data-redis
的 RedisTemplate<K, V>
模板类在操作 redis
时默认使用 JdkSerializationRedisSerializer
来进行序列化,如下:
private boolean enableDefaultSerializer = true;
private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();
private RedisSerializer keySerializer = null;
private RedisSerializer valueSerializer = null;
private RedisSerializer hashKeySerializer = null;
private RedisSerializer hashValueSerializer = null;
创建 RedisListenerConfig
配置类
@Configuration
public class RedisListenerConfig {@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);return container;}@Bean(name="redisTemplate")public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, String> template = new RedisTemplate<>();RedisSerializer<String> redisSerializer = new StringRedisSerializer();template.setConnectionFactory(factory);template.setKeySerializer(redisSerializer);template.setValueSerializer(redisSerializer);template.setHashValueSerializer(redisSerializer);template.setHashKeySerializer(redisSerializer);return template;}
}
实现共享 Session
引入依赖
在 pom.xml
中添加 spring-session-data-redis
依赖
<dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId>
</dependency>
相关配置
创建 RedisConfiguration
配置类
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60 * 60 * 24)
public class RedisConfiguration {}
Controller
创建存取 session
的接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpSession;@RestController
public class IndexController {@GetMapping(value = "put")public String putSession(HttpSession session) {session.setAttribute("userId", "2384783434");return "ok";}@GetMapping(value = "get")public String getSession(HttpSession session) {return (String) session.getAttribute("userId");}}
测试共享 Session
分别启动两次项目,第一次启动项目设置端口为 80,第二次启动项目设置端口为 81
默认情况下,Intellij IDEA
不允许同时启动两个相同的项目,需要在 Run/Debug Configurations
页面中设置允许同时运行多个相同的项目,如图所示,勾选 Allow parallel run
选项:
最后在浏览器中先后访问 http://localhost/put 和 http://localhost:81/get
- 文章作者:彭超
- 本文首发于个人博客:https://antoniopeng.com/2019/08/18/springboot/SpringBoot%E4%B8%ADRedis%E7%9A%84%E4%BD%BF%E7%94%A8/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 彭超 | Blog!
redis bind多个ip_Spring Boot 中 Redis 的使用相关推荐
- springboot(三):Spring boot中Redis的使用
转自www.ityouknow.com spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memc ...
- Spring boot(3):Spring boot中Redis 的使用
Spring boot除了常用的数据库支持外,对nosql数据库也进行了封装自动化. 1 Redis介绍 Redis 是目前业界使用最广泛的内存数据存储.相比memcached, (1)Redis支持 ...
- (转)Spring Boot(三):Spring Boot 中 Redis 的使用
http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html Spring Boot 对常用的数据库支持外,对 Nosql ...
- Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster
文章目录 Pre Jedis操作Redis Cluster 添加依赖 Code Spring Boot 操作Redis Cluster 引入 依赖 application.yml Code Pre R ...
- redis setnx 分布式锁_Spring Boot 整合 Redis 正确的实现分布式锁
前言 最近在做分块上传的业务,使用到了Redis来维护上传过程中的分块编号. 每上传完成一个分块就获取一下文件的分块集合,加入新上传的编号,手动接口测试下是没有问题的,前端通过并发上传调用就出现问题了 ...
- linux redis数据库安装配置,Linux系统中redis的安装配置步骤
Linux系统中redis的安装配置步骤 发布时间:2020-06-23 10:13:36 来源:亿速云 阅读:87 作者:Leah 这篇文章将为大家详细讲解有关Linux系统中redis的安装配置步 ...
- 《spring-boot学习》-05-spring boot中redis应用
转载http://www.cnblogs.com/ityouknow/p/5748830.html
- redis 支持 json_Spring Boot 中集成 Redis
阅读本文约需要5分钟 大家好,我是你们的导师,我每天都会在这里给大家分享一些干货内容(当然了,周末也要允许老师休息一下哈).上次老师跟大家分享了 Spring Boot 中的拦截器,今天跟大家分享下 ...
- Spring Boot 操作 Redis教程
Spring Boot 操作 Redis教程 在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了 使用 Java 操作 Redis 的方案很多,Jedis 是 ...
最新文章
- 《Docker容器:利用Kubernetes、Flannel、Cockpit和Atomic构建和部署》——2.2 容器式Linux系统的Docker配置...
- Ubuntu 17.10安装phpMyAdmin数据库管理工具
- 深入了解Token认证的来龙去脉
- Linq to xml:使用 XSLT 转换 XML 树
- 1021. Remove Outermost Parentheses删除最外层的括号
- Shell(bash) 介绍
- 你是个有魅力的人吗?人格魅力这样修养而成
- jq在html中添加dom元素,使用jQuery添加DOM元素的最佳方法
- java字符串转json_java 字符串转成 json 数组并且遍历
- python怎么读写文件-python3 excle(python怎么读写excel文件)
- 问题二十四:怎么模拟ray tracing图形中介质材料的颜色(dielectric)
- 用有道ip地址查询接口的详细方法
- 集成电路可测性设计DFT技术入门概论--MBIST技术报告
- LaTeX Studio下载安装
- 计算机在课程中的应用,计算机科学技术在计算机教育中的应用
- dnmap分布式集群扫描
- Browsers简介
- 大地水准面 地球椭球体 大地基准面 地图投影理解
- C# Dictionary多线程安全访问问题
- k8s 1.24.0版本使用nfs-provisioner4.0.0动态创建PV