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的更多相关文章

  1. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  2. (一)由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  3. SpringBoot中使用Redis

    在SpringBoot中使用Redis,思路如下: 查询时先查Redis缓存,如果缓存中存在信息,就直接从缓存中获取. 如果缓存中没有相关信息,就去数据库中查找,查完顺便将信息存放进缓存里,以便下一次 ...

  4. 在springboot中使用redis缓存,将缓存序列化为json格式的数据

    背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...

  5. SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  6. 在SpringBoot中引入Redis

    前言 之前我们只是在Spring中加入Redis用于session的存放,并没有对redis进行主动的存放,这次我们需要加入redis工具类来方便我们在实际使用过程中操作redis 已经加入我的git ...

  7. SpringBoot中集成redis

    转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...

  8. 在SpringBoot中添加Redis

    前言 在实际的开发中,会有这样的场景.有一个微服务需要提供一个查询的服务,但是需要查询的数据库表的数据量十分庞大,查询所需要的时间很长. 此时就可以考虑在项目中加入缓存. 引入依赖 在maven项目中 ...

  9. 你知道如何在springboot中使用redis吗

    特别说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce ​  ...

随机推荐

  1. mkswap命令详解

    基础命令学习目录首页 原文链接:http://blog.51cto.com/arlen99/1743841 mkswap命令用于在一个文件或者设备上建立交换分区.在建立完之后要使用sawpon命令开始 ...

  2. fs - 文件系统

    fs 模块提供了一些 API,用于以一种类似标准 POSIX 函数的方式与文件系统进行交互. 用法如下: const fs = require('fs'); 所有的文件系统操作都有异步和同步两种形式. ...

  3. spring boot之配置跨域

    在启动类中配置 @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override p ...

  4. Bing词典vs有道词典比对测试报告——体验篇之成长性及用户控制权

    成长性: 会记住曾经查询过的单词或例句与有道词典实现基本一样,并无特别亮点. 用户有控制权: 必应词典和有道词典都能实现基本的查询前进和后退.以及无法查找结果,能顺利进行反馈. 我们在输入完单词按下回 ...

  5. Git基础级介绍

    这篇随笔是在学习了廖雪峰老师的git教程之后写的总结,要看详细的基础级git介绍可以去http://www.liaoxuefeng.com/wiki/0013739516305929606dd1836 ...

  6. bing词典

    一.bug寻找 bug1:点击单词挑战之后选择四级词汇,然后一直狂击答案,点到一个时候就会出现一个情况:不管点击哪一个选项都不会跳至下一题,而且屏幕上方的已做题目数 x/20中的x会乱跳. bug2: ...

  7. BETA-2

    前言 我们居然又冲刺了·二 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 了解OpenCV下的视频参数及其调用方法 初步编码 接下来的计划 文档工作 速 ...

  8. jsp 页面和 jsp标记

    一个jsp页面可由5种元素组成 html标记 变量和方法的声明 java程序片 java表达式 <%!变量和方法的声明%> 被声明的方法和变量在整个jsp页面都可以访问,为全局变量 当多个 ...

  9. node下的跨域传递cookie

    研究背景: 最近有一位朋友找工作,需要面试,涉及到面试就涉及面试题,于是我想起来鄙人之前面试被问到的一个跨域传递cookie的问题.搜索了相关资料,但自己不敲一下肯定是不足以让人信服的. 我用node ...

  10. MiniUI框架合并单元格

    在项目中遇到合并单元格的问题,所以总结一下. 用的是miniUI框架,所以只谈miniUI中的单元格合并. (1)必须添加onLoad="onLoad" (2)需要在JS中进行单元 ...