Springboot结合Redis
| Yum install gcc-c++ |
| tar -zxvf redis-3.0.0.tar.gz |
|
cd redis-3.0.0
make
|
|
make PREFIX=/usr/local/redis install
|
|
./redis-server
|
<?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>
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);
}
}
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的更多相关文章
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- Windows环境下springboot集成redis的安装与使用
一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...
- SpringBoot系列——Redis
前言 Redis是一个缓存.消息代理和功能丰富的键值存储.StringBoot提供了基本的自动配置.本文记录一下springboot与redis的简单整合实例 官方文档:https://docs.sp ...
- SpringBoot整合Redis及Redis工具类撰写
SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
- 带着新人学springboot的应用04(springboot+mybatis+redis 完)
对于缓存也说了比较多了,大家对下图这一堆配置类现在应该有些很粗略的认识了(因为我也就很粗略的认识了一下,哈哈!),咳,那么我们怎么切换这个缓存呢?(就是不用springboot提供的默认的Simple ...
- SpringBoot系列十:SpringBoot整合Redis
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...
随机推荐
- php编辑器notepad++ 推荐一款非常好看主题和字体
php编辑器notepad++ 推荐一款非常好看主题和字体1.主题名称:Obsidian 2.字体字号:Courier New 10 3.设置方法:设置---语言格式设置---选择主题,同时勾选“使用 ...
- 013.Kubernetes二进制部署worker节点Nginx实现高可用
一 Nginx代理实现kube-apiserver高可用 1.1 Nginx实现高可用 基于 nginx 代理的 kube-apiserver 高可用方案. 控制节点的 kube-controller ...
- JavaScript with Image:创建缩略图
当图片很大,直接把图片从Server下载到浏览器上看是一种很不明智的做法,浪费了服务器的资源,网络带宽和客户端的资源.所以,通常Server和Client之间会传输缩略图,只有当Client请求某张图 ...
- [HTML] 学HTML写的第一第二个网页
①第一个网页 <h2>英雄联盟(电子竞技类游戏)</h2> <p><b>(英雄联盟)</b>(简称lol)是由美国<i>Roit ...
- Spring Bean的定义及作用域
目录: 了解Spring的基本概念 Spring简单的示例 Bean的定义 简单地说Bean是被Spring容器管理的Java对象,Spring容器会自动完成对Bean的实例化. 那么什么是容器呢?如 ...
- WebSocket插件
;!(function(window){ "use strict"; let Event = { wsMesEvent:function(message){ console.log ...
- nyoj 20-吝啬的国度 (DFS)
20-吝啬的国度 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:12 submit:43 题目描述: 在一个吝啬的国度里有N个城市,这N个城市间只有 ...
- webpack安装与核心概念
安装webpack webpack核心概念:入口.输出.加载器.插件.模块.模式 一.安装webpack 1.安装webpack之前需要安装nodejs环境,在使用nodejs环境自带的包管理工具np ...
- 【Java】面向对象初探
前段时间经历了一段心态浮躁期,这让我想起了自己最初的计划,要提升自己知识体系的广度.前几年一直做的是web前端这一块的工作,但我希望通过自己在学习Java这样的过程来提升自己的知识广度. 面向对象概述 ...
- CSS:CSS弹性盒子布局 Flexible Box
一.简介 flexbox:全称Flexible Box, 弹性盒子布局.可以简单实现各种伸缩性的设计,它是由伸缩容器和伸缩项目组成.任何一个元素都可以指定为flexbox布局.这种新的布局方案在200 ...