SpringBoot整合NoSql--(一)Redis
简介:
Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
1.首先在虚拟机上的Centos上安装完成redis,并且完成redis.conf文件的配置(后台启动,密码,IP等等),启动redis-server redis.conf

2. 查看是否启动:


3.使用win下的RedisDesktopManager连接虚拟机上的redis

4.创建项目:
spring-boot-starter-data-redis默认使用的Redis工具是Lettuce,我在这里使用的是Jedis,所以exclusion他
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置文件:application.properties,其中需要配置哪些看自己需求,前面几个是需要的
spring.redis.database=0
spring.redis.host=192.168.205.100
spring.redis.port=6379
spring.redis.password=123456
#spring.redis.lettuce.pool.max-active=
#spring.redis.lettuce.pool.max-idle=
#spring.redis.lettuce.pool.max-wait=
#spring.redis.lettuce.pool.min-idle=
#spring.redis.lettuce.shutdown-timeout=
#连接池最大连接数 spring.redis.jedis.pool.max-active=8 #连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 #连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1ms #连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0
注意:
SpringBoot的自动配置类中提供了RedisAutoConfiguration进行Redis配置,源码如下,application.properties中的配置文件将被注入到RedisProperties中,如果开发者没有提供RedisTemplate和StringRedisTemplate这两个类,SpringBoot会默认提供这两个实例。
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
} @Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
} @Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
Controller:
@RestController
public class BookController {
@Autowired
RedisTemplate redisTemplate; @Autowired
StringRedisTemplate stringRedisTemplate; @GetMapping("/test1")
public void test1() {
//使用stringRedisTemplate
ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
ops1.set("name", "三国演义");
String name = ops1.get("name");
System.out.println(name); // 使用redisTemplate
ValueOperations ops2 = redisTemplate.opsForValue();
Book b1 = new Book();
b1.setId(1);
b1.setName("红楼梦");
b1.setAuthor("曹雪芹");
ops2.set("b1", b1);
Book book = (Book) ops2.get("b1");
System.out.println(book);
}
}
启动,访问http://localhost:8080/test1
控制台表示,已经成功存储并且可以获取到redis中的数据了:

redis中:

SpringBoot整合NoSql--(一)Redis的更多相关文章
- springboot整合mybatis,mongodb,redis
springboot整合常用的第三方框架,mybatis,mongodb,redis mybatis,采用xml编写sql语句 mongodb,对MongoTemplate进行了封装 redis,对r ...
- SpringBoot 整合NoSql
通用配置 maven依赖 添加Spring-Web和Spring-Security依赖,使用Spring-Security是因为使用SpringBoot的Redis依赖时,必须添加Spring-Sec ...
- 【转载】Springboot整合 一 集成 redis
原文:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html https://blog.csdn.net/plei_ ...
- springboot(七).springboot整合jedis实现redis缓存
我们在使用springboot搭建微服务的时候,在很多时候还是需要redis的高速缓存来缓存一些数据,存储一些高频率访问的数据,如果直接使用redis的话又比较麻烦,在这里,我们使用jedis来实现r ...
- springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows
源码地址:https://gitee.com/ytfs-dtx/SpringBoot Redis-Desktop-Manager2020地址: https://ytsf.lanzous.com/b01 ...
- SpringBoot整合Shiro自定义Redis存储
Shiro Shiro 主要分为 安全认证 和 接口授权 两个部分,其中的核心组件为 Subject. SecurityManager. Realms,公共部分 Shiro 都已经为我们封装好了,我们 ...
- 三:Springboot整合Redis
一:springboot整合redis redis版本:3.0.0 运行环境:linux 1.安装redis 1.1安装gcc yum install gcc-c++ 1.2解压redis.3.0.0 ...
- SpringBoot整合开发
1.SpringBoot分模块 分模块就是将一个项目分成多个模块,即maven项目. 1)首先创建一个springboot的项目: 第一步:选择springboot的项目 第二步:填写项目的相关信息, ...
- SpringBoot整合Shiro完成验证码校验
SpringBoot整合Shiro完成验证码校验 上一篇:SpringBoot整合Shiro使用Redis作为缓存 首先编写生成验证码的工具类 package club.qy.datao.utils; ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
随机推荐
- 性能优于JDK代理,CGLib如何实现动态代理
按照代理的创建时期,代理类可以分为两种. 静态代理:由程序员创建或特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了. 动态代理:在程序运行时,运用反射机制动态创建 ...
- 1751: n个素数构成等差数列
#include <stdio.h>int fill(char *map,int *primes) { for (int i = 2; i < 1001; i++) { map[i] ...
- Java12可用新特性一览,了解一下没有错
你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it well enough ...
- Linux系统之LAMP实现
1.部署分离的LAMP,部署到二台服务器上,php加载xcache模块 首先准备LAMP环境,准备两台Linux主机,一台是192.168.0.10 ,这台上面主要跑apache httpd ,mar ...
- java架构之路-(微服务专题)nacos集群精讲实战
上次回顾: 上次博客,我们主要说了微服务的发展历程和nacos集群单机的搭建,单机需要-m standalone启动,集群建议使用nginx做一下反向代理,自行保证mysql和ngxin的高可用. 本 ...
- IP multicast IP多播
https://networklessons.com/multicast/multicast-routing/ IP多播有两种模式,密集模式和稀疏模式: Dense Mode Sparse Mode ...
- Kvm 简介 安装 使用 桥接网络
KVM 全称是 基于内核的虚拟机(Kernel-based Virtual Machine),它是一个 Linux 的一个内核模块,该内核模块使得 Linux 变成了一个 Hypervisor: 它由 ...
- asp.net mvc项目实战遇见问题及解决方式----ajax请求500错误,请求多表数据
ajax请求出现500错误——但是想实现的功能是,把一个页面分成了两份,点击右边导航栏,利用ajax请求,请求数据,在右边出现相应页面,当时使用的是partialAction然后出现了这个500错误, ...
- Java 添加、读取、删除Excel图片
本文介绍在Java程序中如何添加图片到excel表格,添加图片时可设置图片大小.位置.旋转.超链接.可选文本等,以及如何读取.删除excel表格中已有的图片. 工具:Free Spire.XLS fo ...
- To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongoose报错:DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and wil ...