springboot學(xué)習(xí)之集成redis

這篇文章主要是要實(shí)現(xiàn)在項(xiàng)目中集成redis。
redis的安裝可以查看我的另一篇文章linux學(xué)習(xí)之centos7 安裝redis

1.引入redis依賴

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

我的springboot版本用的是1.5.4.RELEASE
而redis最新是1.4.7.RELEASE:


Paste_Image.png

因此在pom代碼中設(shè)置了版本號(hào)

<version>1.3.2.RELEASE</version>

spring-boot-starter-redis給我們提供了RedisTemplate,方便我們對(duì)redis的配置和操作。

2.設(shè)置redis配置

在application.properties或者是yaml文件中設(shè)置:


# REDIS (RedisProperties)

# Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0)

spring.redis.database=0

# Redis服務(wù)器地址

spring.redis.host=127.0.0.1

# Redis服務(wù)器連接端口,生產(chǎn)服務(wù)端口要改,如果不改容易被感染 ,同時(shí)也最好設(shè)置好密碼

spring.redis.port=6379

# Redis服務(wù)器連接密碼(默認(rèn)為空)

spring.redis.password=

# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)

spring.redis.pool.max-active=8

# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)

spring.redis.pool.max-wait=-1

# 連接池中的最大空閑連接

spring.redis.pool.max-idle=8

# 連接池中的最小空閑連接

spring.redis.pool.min-idle=0

# 連接超時(shí)時(shí)間(毫秒)

spring.redis.timeout=20000

注意 如果設(shè)置的密碼和redis中設(shè)置的不一樣會(huì)出現(xiàn)權(quán)限異常:

NOAUTH Authentication required.; nested exception is redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required."

我們可以在redis.conf配置文件中找到requirepass ,這是redis訪問密碼設(shè)置的值。

3.讓spring 管理我們的RedisTemplate

創(chuàng)建RedisConfig,代碼如下:


@Configuration
public class RedisConfig  {
    
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {

        RedisTemplate template = new RedisTemplate<>();

        template.setConnectionFactory(connectionFactory);

        //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值

        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper mapper = new ObjectMapper();

        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        serializer.setObjectMapper(mapper);

        template.setValueSerializer(serializer);

        //使用StringRedisSerializer來序列化和反序列化redis的key值

        template.setKeySerializer(new StringRedisSerializer());

        template.afterPropertiesSet();

        return template;

    }

}

4.通過RedisTemplate 來操作我們的redis


@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {


    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void t(){


       ValueOperations valueOperations =  redisTemplate.opsForValue();
        SimpleLoginInfo simpleLoginInfo = SimpleLoginInfo.builder()
                .gender(1).role(2).nickName("xiaohua").build();
       //設(shè)置超時(shí)時(shí)間
        valueOperations.set("xiaohua",simpleLoginInfo,2, TimeUnit.SECONDS);

        //不設(shè)置超時(shí)
        valueOperations.set("xiaoxiao",simpleLoginInfo);

        SimpleLoginInfo xiaohua = (SimpleLoginInfo) valueOperations.get("xiaohua");
        System.out.println(xiaohua.toString());

        try{
            Thread.sleep(3000);
            SimpleLoginInfo xiaoxiao = (SimpleLoginInfo) valueOperations.get("xiaoxiao");
            SimpleLoginInfo xiaohua2 = (SimpleLoginInfo) valueOperations.get("xiaohua");
            System.out.println(xiaoxiao.toString());
            System.out.println(xiaohua2.toString());


        }catch (Exception e){
            e.printStackTrace();
        }

    }

}

這樣我們的springboot就可以使用redis了。

總結(jié):

本篇文章主要寫了springboot集成redis.而我們可以通過redis做很多事情,比如cache,key-value 保存等等..

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

相關(guān)閱讀更多精彩內(nèi)容

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