安装 Redis
 
安装 gcc
Yum install gcc-c++
解压 redis.3.0.0.tar.gz 压缩包
tar -zxvf redis-3.0.0.tar.gz
进入解压后的目录进行编译
cd redis-3.0.0
make
Redis 安装到指定目录
make PREFIX=/usr/local/redis install
启动 Redis
./redis-server
Spring Boot 整合 Spring Data Redis
Spring Data Redis 是属于 Spring Data 下的一个模块。作用就是简化对于 redis 的操做
修改 pom 文件添加 Spring Data Redis 的坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>spring-data-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-redis</name>
<description>spring-data-redis</description> <properties>
<java.version>1.8</java.version>
<jedis>3.1.0</jedis>
</properties> <dependencies>
<!-- <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.data.redis}</version>
</dependency>
-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis}</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>
编写 Spring Data Redis 的配置类(重点)

com.bjsxt.redis.RedisConfig

package com.bjsxt.redis;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig; /**
* 完成对Redis整合的配置
*/
@Configuration
public class RedisConfig {
/**
* 1.创建 JedisPoolConfig 对象。在该对象中完成一些链接池配置
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.jedis.pool")
public JedisPoolConfig JedisPoolConfig(){
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(10);//设置最大空闲数
config.setMinIdle(5);//设置最小空闲数
config.setMaxTotal(20);//设置最大连接数
return config;
} /**
* 2.创建 JedisConnectionFactory 对象,配置Redis连接属性
* @param config
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
JedisConnectionFactory factory=new JedisConnectionFactory();
factory.setPoolConfig(config);//关联连接池的配置对象
factory.setHostName("192.168.181.133");//设置连接主机
factory.setPort(6379);//设置端口号
return factory;
} /**
* 3.创建RedisTemplate,用于执行Redis操作的方法
* @param factory
* @return
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String,Object> template =new RedisTemplate<>();
//关联JedisConnectionFactory
template.setConnectionFactory(factory);
//为key序列化器
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化器
template.setValueSerializer(new StringRedisSerializer()); return template;
} }
编写测试代码,测试整合环境
编写测试类
package com.bjsxt.test;

import com.bjsxt.SpringDataRedisApplication;
import com.bjsxt.pojo.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringDataRedisApplication.class)
public class RedisTest { @Autowired
RedisTemplate redisTemplate; /**
* 测试Redis添加
*/
@Test
public void testRedisSet(){
redisTemplate.opsForValue().set("name","yxf");
} /**
* 测试Redis查询
*/
@Test
public void testRedisGet(){
String value = (String) redisTemplate.opsForValue().get("name");
System.out.println(value);
} /**
* 测试Redis添加对象
*/
@Test
public void testRedisSetUsers(){
Users users=new Users();
users.setUname("张三");
users.setAge(18);
users.setSex("男");
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.opsForValue().set("users",users);
} /**
* 测试Redis获取对象
*/
@Test
public void testRedisGetUsers(){
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
Users users = (Users) redisTemplate.opsForValue().get("users");
System.out.println(users);
} /**
* 测试Redis添加json数据
*/
@Test
public void testRedisSetUSersJson(){
Users users=new Users();
users.setUname("李四");
users.setAge(20);
users.setSex("女");
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
redisTemplate.opsForValue().set("users-json",users);
} /**
* 测试Redis获取json数据
*/
@Test
public void testRedisGetJson(){
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
Users users = (Users) redisTemplate.opsForValue().get("users-json");
System.out.println(users);
}
}
提取 redis 的配置信息
src/main/resource/目录下新建一个配置文件:application.properties
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5
spring.redis.jedis.pool.max-total=20
spring.redis.hostName=192.168.181.133
spring.redis.port=6379
修改配置类
package com.bjsxt.redis;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig; /**
* 完成对Redis整合的配置
*/
@Configuration
public class RedisConfig {
/**
* 1.创建 JedisPoolConfig 对象。在该对象中完成一些链接池配置
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.jedis.pool")
public JedisPoolConfig JedisPoolConfig(){
JedisPoolConfig config=new JedisPoolConfig();
/*config.setMaxIdle(10);//设置最大空闲数
config.setMinIdle(5);//设置最小空闲数
config.setMaxTotal(20);//设置最大连接数*/
System.out.println("默认值:"+config.getMaxIdle());
System.out.println("默认值:"+config.getMinIdle());
System.out.println("默认值:"+config.getMaxTotal());
return config;
} /**
* 2.创建 JedisConnectionFactory 对象,配置Redis连接属性
* @param config
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
System.out.println("配置完毕:"+config.getMaxIdle());
System.out.println("配置完毕:"+config.getMinIdle());
System.out.println("配置完毕:"+config.getMaxTotal());
JedisConnectionFactory factory=new JedisConnectionFactory();
factory.setPoolConfig(config);//关联连接池的配置对象
/* factory.setHostName("192.168.181.133");//设置连接主机
factory.setPort(6379);//设置端口号*/
return factory;
} /**
* 3.创建RedisTemplate,用于执行Redis操作的方法
* @param factory
* @return
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String,Object> template =new RedisTemplate<>();
//关联JedisConnectionFactory
template.setConnectionFactory(factory);
//为key序列化器
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化器
template.setValueSerializer(new StringRedisSerializer()); return template;
} }

Springboot结合Redis的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  3. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  4. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  5. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  6. SpringBoot系列——Redis

    前言 Redis是一个缓存.消息代理和功能丰富的键值存储.StringBoot提供了基本的自动配置.本文记录一下springboot与redis的简单整合实例 官方文档:https://docs.sp ...

  7. SpringBoot整合Redis及Redis工具类撰写

            SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...

  8. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  9. 带着新人学springboot的应用04(springboot+mybatis+redis 完)

    对于缓存也说了比较多了,大家对下图这一堆配置类现在应该有些很粗略的认识了(因为我也就很粗略的认识了一下,哈哈!),咳,那么我们怎么切换这个缓存呢?(就是不用springboot提供的默认的Simple ...

  10. SpringBoot系列十:SpringBoot整合Redis

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...

随机推荐

  1. 邵国际: C 语言对象化设计实例 —— 命令解析器

    本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 邵国际 来源: 微信公众号linux阅码场(id: linuxdev) 内容简介 单片机工程师常常疑惑为什么 ...

  2. MySQL开发规范与使用技巧总结

    命名规范 1.库名.表名.字段名必须使用小写字母,并采用下划线分割. a)MySQL有配置参数lower_case_table_names,不可动态更改,Linux系统默认为 0,即库表名以实际情况存 ...

  3. Unity加载AB包

    Unity制作游戏AB包 需要注意的是在游戏场景运行的情况下,不能编译AB包,不运行的情况下编译AB包需要使用Unity的扩展菜单功能,首先需要建立菜单用来编译AB包. 1.建立AB包的名字,首先选中 ...

  4. 原生js实现简单的下拉加载

    #获取当前滚动条的高度document.documentElement.scrollTop #获取当前窗口的高度 document.documentElement.clientHeight #获取当前 ...

  5. Java IO入门

    目录 一. 数据源(流) 二. 数据传输 三. 总结 我们从两个方面来理解Java IO,数据源(流).数据传输,即IO的核心就是对数据源产生的数据进行读写并高效传输的过程. 一. 数据源(流) 数据 ...

  6. SpringBoot系列之i18n集成教程

    目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...

  7. SoapUI使用JDBC请求连接数据库及断言的使用

    SoapUI提供了用来配置JDBC数据库连接的选项,因此我们可以在测试中使用JDBC数据源.JDBC数据接收器和JDBC请求步骤. 为了能够配置数据连接,就必须有驱动程序和连接串,SoapUI中已经提 ...

  8. python:Asyncio模块处理“事件循环”中的异步进程和并发执行任务

    python模块Asynico提供了管理事件.携程.任务和线程的功能已经编写并发代码的同步原语. 组成模块: 事件循,Asyncio 每个进程都有一个事件循环. 协程,子例程概念的泛化,可以暂停任务, ...

  9. [FPGA]Verilog实现寄存器LS374

    目录 想说的话... 正文 IC介绍 电路连接图 功能表 逻辑图 实验原理 单元实现_D触发器 整体实现(完整代码) 想说的话... 不久前正式开通了博客,以后有空了会尽量把自己学习过程中的心得或者感 ...

  10. Beta阶段贡献分配

    此作业要求参见:http://edu.cnblogs.com/campus/nenu/2019fall/homework/10006 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 刘信鹏 要 ...