SpringBoot 中使用redis以及redisTemplate
1.首先在pom.xml中添加依赖
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<version>1.5.2.RELEASE</version>
		</dependency>
<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>
<dependency> 
            <groupId>org.springframework.session</groupId>  
            <artifactId>spring-session-data-redis</artifactId>  
        </dependency>
2.在git上的配置文件中添加redis集群配置
spring.redis.cluster.nodes=10.10.10.236:7001,10.10.10.236:7002,10.10.10.236:7003,10.10.10.236:7004,10.10.10.236:7005,10.10.10.236:7006
spring.redis.cluster.host=10.10.10.236
spring.redis.cluster.port=7001
spring.redis.cluster.passwd=
spring.redis.cluster.timeOut=2000
spring.redis.cluster.max-redirects=8
3.在程序中读取配置文件中的值
package cn.lz.auth.redis;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
//@Component
@EnableRedisHttpSession
@Configuration
public class RedisConfig
{
	@Value("${spring.redis.cluster.nodes}")
	private String clusterNodes;
@Value("${spring.redis.cluster.host}")
	private String redisHost;
@Value("${spring.redis.cluster.port}")
	private int redisPort;
@Value("${spring.redis.cluster.passwd}")
	private String redisPasswd;
@Value("${spring.redis.cluster.timeOut}")
	private int timeOut = 2000;
@Value("${spring.redis.cluster.max-redirects}")
	private int redirects = 8;
@Bean
	public RedisClusterConfiguration getClusterConfiguration()
	{
		Map<String, Object> source = new HashMap<String, Object>();
		source.put("spring.redis.cluster.nodes", clusterNodes);
		source.put("spring.redis.cluster.timeout", timeOut);
		source.put("spring.redis.cluster.max-redirects", redirects);
		return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
	}
@Bean
	public RedisConnectionFactory redisConnectionFactory()
	{
		JedisConnectionFactory cf = null;
		if( clusterNodes != null && !clusterNodes.isEmpty() ){
			cf = new JedisConnectionFactory( getClusterConfiguration() );
		}
		else{
			cf = new JedisConnectionFactory();
			cf.setHostName( redisHost );
			cf.setPort( redisPort );
			cf.setPassword( redisPasswd);
			cf.setTimeout( timeOut );
		}
cf.afterPropertiesSet();
		return cf;
	}
@Bean
	public RedisTemplate<String, Object> redisTemplate()
	{
		RedisTemplate<String, Object> rt = new RedisTemplate<String, Object>();
		rt.setConnectionFactory( redisConnectionFactory() );
		return rt;
	}
public static enum StringSerializer implements RedisSerializer<String>
	{
		INSTANCE;
@Override
		public byte[] serialize(String s) throws SerializationException
		{
			return (null != s ? s.getBytes() : new byte[0]);
		}
@Override
		public String deserialize(byte[] bytes) throws SerializationException
		{
			if (bytes.length > 0){
				return new String(bytes);
			}
			else{
				return null;
			}
		}
	}
public static enum LongSerializer implements RedisSerializer<Long>
	{
		INSTANCE;
@Override
		public byte[] serialize(Long aLong) throws SerializationException
		{
			if (null != aLong){
				return aLong.toString().getBytes();
			}
			else{
				return new byte[0];
			}
		}
@Override
		public Long deserialize(byte[] bytes) throws SerializationException
		{
			if (bytes.length > 0){
				return Long.parseLong(new String(bytes));
			}
			else{
				return null;
			}
		}
	}
public static enum IntSerializer implements RedisSerializer<Integer>
	{
		INSTANCE;
@Override
		public byte[] serialize(Integer i) throws SerializationException
		{
			if (null != i){
				return i.toString().getBytes();
			}
			else{
				return new byte[0];
			}
		}
@Override
		public Integer deserialize(byte[] bytes) throws SerializationException
		{
			if (bytes.length > 0){
				return Integer.parseInt(new String(bytes));
			}
			else{
				return null;
			}
		}
	}
}
4.根据redisTemplate编写set,get,delete方法
package cn.lz.auth.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;
@Component
public class RedisService<T>
{
	@Autowired
	private RedisTemplate<String, Object> redis;
public void setObject( String uuid, T object, Class<T> type )
	{
		redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
		redis.setValueSerializer( new Jackson2JsonRedisSerializer<T>(type) );
		redis.afterPropertiesSet();
ValueOperations<String, Object> ops = redis.opsForValue();
		ops.set( uuid, object );
	}
@SuppressWarnings("unchecked")
	public T getObject( String uuid, Class<T> type )
	{
		redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
		redis.setValueSerializer( new Jackson2JsonRedisSerializer<T>(type) );
		redis.afterPropertiesSet();
ValueOperations<String, Object> ops = redis.opsForValue();
		return (T)ops.get( uuid );
	}
public void delObject( String uuid )
	{
		redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
		redis.afterPropertiesSet();
redis.delete( uuid );
	}
}
5.调用set,get,delete方法操作redis中的数据
好文链接:http://www.cnblogs.com/langtianya/p/5190201.html
SpringBoot 中使用redis以及redisTemplate的更多相关文章
- 由浅入深学习springboot中使用redis
		
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
 - (一)由浅入深学习springboot中使用redis
		
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
 - SpringBoot中使用Redis
		
在SpringBoot中使用Redis,思路如下: 查询时先查Redis缓存,如果缓存中存在信息,就直接从缓存中获取. 如果缓存中没有相关信息,就去数据库中查找,查完顺便将信息存放进缓存里,以便下一次 ...
 - 在springboot中使用redis缓存,将缓存序列化为json格式的数据
		
背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...
 - SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中
		
在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...
 - 在SpringBoot中引入Redis
		
前言 之前我们只是在Spring中加入Redis用于session的存放,并没有对redis进行主动的存放,这次我们需要加入redis工具类来方便我们在实际使用过程中操作redis 已经加入我的git ...
 - SpringBoot中集成redis
		
转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...
 - 在SpringBoot中添加Redis
		
前言 在实际的开发中,会有这样的场景.有一个微服务需要提供一个查询的服务,但是需要查询的数据库表的数据量十分庞大,查询所需要的时间很长. 此时就可以考虑在项目中加入缓存. 引入依赖 在maven项目中 ...
 - 你知道如何在springboot中使用redis吗
		
特别说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce  ...
 
随机推荐
- 【总结】Java面试题
			
部分转自 https://blog.csdn.net/junchi_/article/details/79754032 一.String特性.StringBuffer 和 StringBuilder ...
 - ovs源码阅读--netlink使用
			
netlink netlink socket是一种用于用户态进程和内核态进程之间的通信机制.它通过为内核模块提供一组特殊的API,并为用户程序提供了一组标准的socket接口的方式,实现了全双工的通讯 ...
 - TensorFlow学习之路1-TensorFlow介绍
			
TensorFlow是一个采用数据流图(data flow graphs),用于数据计算的开源软件库. 什么是数据流图? TensorFlow的数据流图是由“节点”(nodes)和“线”(edges) ...
 - 作业要求 20181127-5 Beta发布用户使用报告
			
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2450 一.用户反馈 反馈截图(部分) 三.用户反馈情况统计图
 - final文案+美工展示
			
作业要求:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1438 团队介绍:thunder 组成员及各位博客地址: 1.王航:htt ...
 - BugPhobia开发篇章:Scurm Meeting-更新至0x03
			
0x01 :目录与摘要 If you weeped for the missing sunset, you would miss all the shining stars 索引 提纲 整理与更新记录 ...
 - android随机运算器开发小结1
			
想到第一天自己写了一个简单的四则运算程序的情景:我便想起了引起我们不断迭代开发的程序背景是:二柱子接受老师安排的给孩子出题的任务,每次需要给孩子设置出题任务,生成相应的小学运算题目,所以我们面对的需求 ...
 - Task  6.2冲刺会议十  /2015-5-23
			
今天是第一个冲刺阶段的最后一天,主要把做出来的程序进行了初步的测试,在一台笔记本上运行程序,摄像头可以工作也能听到声音和麦克多的运转也还可以,两台计算机同时在一个局域网中通信的时候也可以实现.不过后续 ...
 - WebService有什么用?
			
入门之前先简单介绍下WCF.在用WebService做开发时,很多人都不知道WCF和WebService之间的关系.实际上WCF包含了WebService,这是一个很强悍的通信技术应用框架.微软把.N ...
 - 复杂PC问题——信号量与共享存储区
			
#include <stdio.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/sem.h ...