1.为了高可用,先安装redis集群 参考我的另一篇文章 http://www.cnblogs.com/xiaochangwei/p/7993065.html

2.POM中引入redis

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>

3.增加redis配置(使用集群方式)

#redis pool config
#spring.redis.hostName=192.168.0.32
#spring.redis.port=6379
#spring.redis.password=
#spring.redis.database=13
#spring.redis.pool.maxActive=8
#spring.redis.pool.maxWait=-1
#spring.redis.pool.maxIdle=8
#spring.redis.pool.minIdle=0
#spring.redis.timeout=0
#spring.redis.expire.time=20 #redis cluster config
spring.redis.cluster.nodes=192.168.0.45:7001,192.168.0.45:7002,192.168.0.45:7003,192.168.0.45:7004,192.168.0.45:7005,192.168.0.45:7006
#spring.redis.cluster.nodes=192.168.0.81:7001,192.168.0.81:7002,192.168.0.81:7003,192.168.0.81:7004,192.168.0.81:7005,192.168.0.81:7006
spring.redis.cluster.timeout=2000
spring.redis.cluster.max-redirects=1
spring.redis.expire.time=20

4.配置redis集群链接并设置缓存(部分配置及内容会在后续文章中讲解到

package com.xiao.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClusterConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.HashMap;
import java.util.Map; //链接redis集群的时候 @Configuration
@EnableCaching
@RefreshScope
public class RedisClusterConfig { @Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
@Value("${spring.redis.cluster.max-redirects}")
private int redirects;
@Value("${spring.redis.expire.time}")
private int redisExpireTime; @Bean
@RefreshScope
public RedisClusterConfiguration redisClusterConfiguration() {
Map<String, Object> source = new HashMap<>();
source.put("spring.redis.cluster.nodes", clusterNodes);
source.put("spring.redis.cluster.max-redirects", redirects);
return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
} @Bean
public JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory(redisClusterConfiguration());
} @Bean
public JedisClusterConnection jedisClusterConnection() {
return (JedisClusterConnection) jedisConnectionFactory().getConnection();
} @Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
} @Bean
public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(redisExpireTime);
return cacheManager;
}
}

5.通过下列代码进行测试

@Autowired
StringRedisTemplate stringRedisTemplate; @RequestMapping(value = "/redis/setget")
public Result redisSetGet(@RequestParam(value = "key", required = true) String key) {
stringRedisTemplate.opsForValue().set(key, UUID.randomUUID().toString());
stringRedisTemplate.expire(key, 10, TimeUnit.MINUTES);
return new Result("从redis中获取到的值为:" + stringRedisTemplate.opsForValue().get(key));
}

结果如下:

通过redis的可视化工具 查看如下

redis一般而言用string就可以了,对象可以通过JSON转换后再存储

springBoot系列教程03:redis的集成及使用的更多相关文章

  1. SpringBoot系列之Spring Data Jpa集成教程

    SpringBoot系列之Spring Data Jpa集成教程 Spring Data Jpa是属于Spring Data的一个子项目,Spring data项目是一款集成了很多数据操作的项目,其下 ...

  2. springBoot系列教程07:异常捕获

    发生异常是很正常的事,异常种类也是千奇百怪,发生异常并不可怕,只要正确的处理,并正确的返回错误信息并无大碍,如果不进行捕获或者处理,分分钟服务器宕机是很正常的事 所以处理异常时,最基本的要求就是发生异 ...

  3. Java工程师之SpringBoot系列教程前言&目录

    前言 与时俱进是每一个程序员都应该有的意识,当一个Java程序员在当代步遍布的时候,你就行该想到我能多学点什么.可观的是后端的框架是稳定的,它们能够维持更久的时间在应用中,而不用担心技术的更新换代.但 ...

  4. SpringBoot系列教程起步

    本篇学习目标 Spring Boot是什么? 构建Spring Boot应用程序 三分钟开发SpringBoot应用程序 本章源码下载 Spring Boot是什么? spring Boot是由Piv ...

  5. SpringBoot系列教程web篇之过滤器Filter使用指南

    web三大组件之一Filter,可以说是很多小伙伴学习java web时最早接触的知识点了,然而学得早不代表就用得多.基本上,如果不是让你从0到1写一个web应用(或者说即便从0到1写一个web应用) ...

  6. SpringBoot系列教程web篇之自定义异常处理HandlerExceptionResolver

    关于Web应用的全局异常处理,上一篇介绍了ControllerAdvice结合@ExceptionHandler的方式来实现web应用的全局异常管理: 本篇博文则带来另外一种并不常见的使用方式,通过实 ...

  7. SpringBoot系列教程web篇之全局异常处理

    当我们的后端应用出现异常时,通常会将异常状况包装之后再返回给调用方或者前端,在实际的项目中,不可能对每一个地方都做好异常处理,再优雅的代码也可能抛出异常,那么在 Spring 项目中,可以怎样优雅的处 ...

  8. SpringBoot系列教程web篇之404、500异常页面配置

    接着前面几篇web处理请求的博文,本文将说明,当出现异常的场景下,如404请求url不存在,,403无权,500服务器异常时,我们可以如何处理 原文友链: SpringBoot系列教程web篇之404 ...

  9. SpringBoot系列教程web篇之重定向

    原文地址: SpringBoot系列教程web篇之重定向 前面介绍了spring web篇数据返回的几种常用姿势,当我们在相应一个http请求时,除了直接返回数据之外,还有另一种常见的case -&g ...

随机推荐

  1. 多线程编程学习笔记——async和await(二)

    接上文 多线程编程学习笔记——async和await(一) 三.   对连续的异步任务使用await操作符 本示例学习如何阅读有多个await方法方法时,程序的实际流程是怎么样的,理解await的异步 ...

  2. codevs1050

    题目地址:http://codevs.cn/problem/1050/ 分析: 最開始想直接用状压做,发现怎么都想不出来.就和当年的多行多米诺骨牌(这道题至少最后还是把普通状压做法看懂了). 直到听到 ...

  3. Implement Queue using Stacks(用栈实现队列)

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. Django配置session

    在settings.py文件里加入 #配置失效时间为半个小时 SESSION_COOKIE_AGE = 60*30 #关闭浏览器清除cookie SESSION_EXPIRE_AT_BROWSER_C ...

  5. POJ 2127 最长公共上升子序列

    动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algor ...

  6. xml基本语法(2)

    本节要点: 了解XML的文档声明 了解XML的元素.命名规则.属性.元素内容.处理指令等概念 1 XML文档声明 表示该文档是一个XML文档,以及遵循哪个XML版本的规范. 规范:<?xml 版 ...

  7. 基于ASP.MVC票据FormsAuthenticationTicket身份认证

    做一个最基础的业务需求用户登录,将此用户的身份发回到客户端的Cookie,之后此用户再访问这个web应用就会连同这个身份Cookie一起发送到服务端.服务端上的授权设置就可以根据不同目录对不同用户的访 ...

  8. Learning to Rank算法介绍:GBRank

    之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...

  9. 总结一下现在关于Design Support Library的几个博客

    原文转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0713/3179.html 关于Design Support Libr ...

  10. idea和Webstorm上使用git和github,码云

    由于之前一直使用svn,现在项目使用git,顾根据网上找的学习资料,自己梳理了下,收获蛮多,这里做个记录,如果能帮助到您那是最好不过的. 1.大致步骤 使用工具:idea,github,码云 webs ...