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取值 }