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. Python测试开发之函数

    对于初学者而言,感觉函数还是不是很好理解,尤其是当写一个脚本,或者是写一个算法,认为可能for循环就已经可以解决的问题为什么还要用函数来实现呢? 今天就来说一下函数的优点,其实函数的最大优点就是可重用 ...

  2. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  3. 关于mui header在手机上运行丢失问题

    并不需要换header, 只需要把引用的例子自带的CSS文件 app.css.里的两个样式:.mui-plus.mui-android header.mui-bar {display: none;}. ...

  4. WeChat 隐私政策

    隐私政策 本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义务对待这些信息.除本隐 ...

  5. dom4j详解

    Dom4j下载及使用Dom4j读写XML简介要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/目前最新dom4j包下载地址:htt ...

  6. Python开发工具PyCharm个性化设置

    Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧     1.设置默认PyCharm解析器: 操作如下: Pyt ...

  7. HLJU 1223: 寻找区间和 (交替推进法)

    1223: 寻找区间和 Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 13  Solved: 4 [Submit][Status][pid=1223& ...

  8. hdu5353 Average

    Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...

  9. JAVA入门[1]--安装JDK

    1.下载JDK并安装 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  10. 【SqlServer系列】表达式(expression)

    1   概述 本篇这文章主要概述SqlServer表达式. 2   具体内容 2.1  使用范围 SQL Server(2008开始) :Azure SQL数据库:Azure  SQL数据仓库:并行数 ...