Springboot整合redis

單機版,簡單配置?!拘“准墑e】
【Springboot+redis】
springboot 2.x與 redis5.x
確保redis安裝成功并能成功啟動,我用的是自己在虛擬機docker容器安裝配置的redis 版本應(yīng)該是5.0.3的。
Docker 配置安裝Redis可以參考我的Docker配置Redis
本文用的開發(fā)工具是IntelliJ IDEA
springboot 整合redis
1.新建springboot項目
File-->New--Module

filenewmodule.png

選擇spring initializr
Spring initializr.png

點擊next,基本不用改動
image.png

點擊next,選擇web 然后選中Web ,在這里我們可以選擇springboot 版本,我選擇的是2.1.3版本的。
image.png

點擊next 最后點擊finish。springboot項目創(chuàng)建完成。
2.在pom.xml文件中引入redis

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

3.maven導(dǎo)入相關(guān)jar包后,我們需要修改application.properties配置文件
以下是redis 2.x的相關(guān)配置

Redis數(shù)據(jù)庫索引(默認為0)
spring.redis.database=0
Redis服務(wù)器地址
spring.redis.host=localhost
Redis服務(wù)器連接端口
spring.redis.port=6379
Redis服務(wù)器連接密碼(默認為空)
spring.redis.password=root
連接池最大連接數(shù)(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
連接池最大阻塞等待時間(使用負值表示沒有限制)【我配置這個的時候報紅色標,最后我就直接 spring.redis.jedis.pool.max-wait=,因為在RedisProperties類里面有默認的配置,所以可以直接=空,max-wait、max-idle、以及min-idle也均有默認配置值】
spring.redis.jedis.pool.max-wait=-1
連接池中的最大空閑連接
spring.redis.jedis.pool.max-idle=8
連接池中的最小空閑連接
spring.redis.jedis.pool.min-idle=0
連接超時時間(毫秒)
spring.redis.timeout=10000ms

server.port=8004
spring.redis.database=1
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=10000s
image.png

4.新建一個Redis配置類
其實Springboot自動在容器內(nèi)幫我們生成了一個RedisTemplate和一個StringRedisTemplate,我們在項目內(nèi)直接可以引入使用。
但是這個RedisTemplate的泛型是<Object,Object>我們用起來不是很方便,要各種轉(zhuǎn)換類型,我們需要新建一個泛型<String,Object>的RedisTemplate。
并且,這個RedisTemplate沒有設(shè)置數(shù)據(jù)存在Redis時,key及value的序列化方式。
代碼如下:[代碼來源于https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html]

package com.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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;

@Configuration
public class RedisConfig {
    @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();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

建好后,我們可以直接在項目中引用泛型<String,Object>的RedisTemplate了。
我做的一個示例Controller:

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class RedisController {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @RequestMapping(value = "/hello")
    public Object hello(){
        redisTemplate.opsForValue().set("www","1111111");
        return redisTemplate.opsForValue().get("www");
    }
}

其實,我們可以做一個工具類來使用redis。對于各位來說應(yīng)該很簡單吧。

遇到的問題:

1.在啟動springboot的過程中,我遇到了端口沖突,所以我在配置文件中指定了項目啟動端口號
server.port=8004
2.項目啟動后,發(fā)起請求,出現(xiàn)了404。在網(wǎng)上看了一下,是因為Spring Boot默認只會掃描啟動類當前包和以下的包,我寫的controller和springboot啟動類未在一個包或者包下面。所以在
解決方案有兩種:
一、以啟動類的包路徑作為頂層包路徑,例如啟動類包為com.mySpringBoot,那么Controller包路徑就為com.mySpringBoot.controller。
二、在啟動類上面加注解@ComponentScan,此注解為指定掃描路徑,例如:@ComponentScan(basePackages = {"com.mySpringBoot.,com.mySpringBoot.work."}) 多個不同的以逗號分割?!疚矣玫牡诙N方法】

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容