1.本地安装redis服务,官网下载。

2.在开发中要使用redis,首先要启动本地redis服务,启动后页面如下:

3.在spring boot项目pom.xml文件中添加Redis需要的依赖包,可在生成springboot项目选择自动引入:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

4.在application-dev.yml(spring cloud)/application.properties(spring boot)配置文件中加入Redis的配置信息:

#此为spring cloud配置文件格式
spring:
  redis:
database: 0
host: 127.0.0.1
port: 6379
password:
timeout: 500
pool:
max-active: 20 # 连接池最大连接数(使用负值表示没有限制
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制
max-idle: 8 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
##此为spring boot格式配置文件
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=500

5.创建Redis的配置类,定义序列方式;配置了配置文件,spring boot会自动加载redis

package com.yf.microservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonAutoDetect; /**
* @Description: Redis配置类
*/ @Configuration
public class RedisConfiguration { @Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}

6.定义一个简单的redis工具类,这里只给到简单的存取方法,Redis封装了很多方法可使用redisTemplate调用,具体哪些方法查看Redis文档

package com.yf.microservice.web.rest.util;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; /**
* Redis工具类
*/
@Component
public class RedisUtils { @Autowired
private RedisTemplate<String, Object> redisTemplate; /**
* 判断key是否存在
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
} /**
* 普通缓存获取
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

7.前面使用@Component注解把RedisUtils类实例化放到spring容器中了,直接使用@Autowired获取对象使用,也可以直接使用RedisTemplate进行调用其他redis封装方法,参考上面写法

 @Autowired
private RedisUtils redisUtil; @Autowired
private RedisTemplate<String, Object> redisTemplate; public String test() {
redisUtil.set("myName", "学不会丶");//存入key -value
redisTemplate.opsForHash().putAll("map1", new
HashMap<String, Object>());//存入map的方法,set,list等方法查看redis文档
return redisUtil.get("myName").toString();//通过key取值
}





Java 在spring cloud中使用Redis,spring boot同样适用的更多相关文章

  1. 译自如何将Spring Cloud应用程序从Spring Boot 1.2迁移到1.3

    前言 笔者第三个Spring Cloud(版本为Spring Boot 1.2)类项目升级最新版本时遇到不少问题,本文内容是作者翻译Spring Cloud官网一位国外友人文章产生. 原文地址: Mi ...

  2. Spring Cloud中负载均衡器概览

    在上篇文章中(RestTemplate的逆袭之路,从发送请求到负载均衡)我们完整的分析了RestTemplate的工作过程,在分析的过程中,我们遇到过一个ILoadBalancer接口,这个接口中有一 ...

  3. Spring Cloud中服务的发现与消费

    之前没注意,微信公众号的图片不能引用到其他地方,本文图片显示不正常,原图在写完博客后已经删了,,,,,,所以本文小伙伴可以移步这里https://mp.weixin.qq.com/s/GoIZdwt5 ...

  4. 服务注册发现consul之二:在Spring Cloud中使用Consul实现服务的注册和发现

    首先安装consul环境,参照之前的文章:<服务注册发现consul之一:consul介绍及安装>中的第一节介绍. Spring Cloud使用Consul的服务与发现 1.导入依赖pri ...

  5. Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?

    导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...

  6. Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)

    Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...

  7. 解决Spring Cloud中Feign第一次请求失败的问题

    在Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题 com.netflix.hystrix.exception.HystrixTimeoutE ...

  8. 详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失

    在Spring Cloud中我们用Hystrix来实现断路器,Zuul中默认是用信号量(Hystrix默认是线程)来进行隔离的,我们可以通过配置使用线程方式隔离. 在使用线程隔离的时候,有个问题是必须 ...

  9. Spring Cloud中Feign如何统一设置验证token

    代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...

随机推荐

  1. windows登陆suse虚拟机

    vmware我还是比较偏向7.1.4版本,其他版本装在win7上似乎有点问题. windows平台下,使用vmware + opensuse的网络配置过程如下: 装完vm后,会在本地连接新创建两个新连 ...

  2. 第三章.定制专属的kali

    1.更新升级 • apt-get update • apt-get upgrade • apt-get dis-upgrade   2.根据个人喜好需求安装软件包 • 库 • Apt-get命令 • ...

  3. ambari-cassandra-service

    社区:https://github.com/Symantec/ambari-cassandra-service 在HDP集群上安装和管理Cassandra服务,Apache Cassandra是一个开 ...

  4. 如何在一个项目中兼容Wepy和Taro?

    背景交待 NJ 项目启动初期,团队技术栈主要是基于 Vue,技术选择上就选择了类 Vue 的 wepy.迭代几个版本后 mpvue 出来了,简单调研了下,准备基于 mpvue-simple 开发部分页 ...

  5. Spring Boot从入门到实战(十):异步处理

    原文地址:http://blog.jboost.cn/2019/07/22/springboot-async.html 在业务开发中,有时候会遇到一些非核心的附加功能,比如短信或微信模板消息通知,或者 ...

  6. [leetcode] 51. N-Queens (递归)

    递归,经典的八皇后问题. 利用一位数组存储棋盘状态,索引表示行,值为-1表示空,否则表示列数. 对行进行搜索,对每一行的不同列数进行判断,如果可以摆放符合规则,则摆放,同时遍历下一行. 遍历过程中,若 ...

  7. 《VR入门系列教程》之15---配置Oculus的开发环境

    安装Oculus SDK     在使用类似Unity3D之类的引擎开发Oculus Rift应用之前,你必须先安装Oculus的SDK,在Oculus的官网上可以下载:http://develope ...

  8. [ PyQt入门教程 ] Qt Designer工具的使用

    Qt Designer是PyQt程序UI界面的实现工具,Qt Designer工具使用简单,可以通过拖拽和点击完成复杂界面设计,并且设计完成的.ui程序可以转换成.py文件供python程序调用.本文 ...

  9. Linu基础之权限管理

    二十二.权限管理 22.1)什么是权限 针对某些文件或者进程,对用户进行限制,权限可以理解为用于约束用户能对系统所做的操作. 22.2)权限和用户的关系   [root@centos7 ~]# ll ...

  10. sklearn学习 第一篇:knn分类

    K临近分类是一种监督式的分类方法,首先根据已标记的数据对模型进行训练,然后根据模型对新的数据点进行预测,预测新数据点的标签(label),也就是该数据所属的分类. 一,kNN算法的逻辑 kNN算法的核 ...