最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成Redis之RedisTemplate實(shí)踐

 更新時(shí)間:2026年04月10日 15:57:47   作者:當(dāng)歸. z Z  
文章介紹了在使用SpringBoot與Redis進(jìn)行交互時(shí),遇到的中文亂碼問(wèn)題,并提供兩種解決方法:1) 使用StringRedisTemplate;2) 自定義Redis序列化器,同時(shí),還討論了在集群模式下,若主機(jī)宕機(jī),客戶(hù)端如何動(dòng)態(tài)感知集群狀態(tài)

1. 依賴(lài)

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

2. 配置

# ========================redis單機(jī)=====================
spring.data.redis.database=0
spring.data.redis.host=192.168.229.102
spring.data.redis.port=6379
spring.data.redis.lettuce.pool.max-active=8
spring.data.redis.lettuce.pool.max-wait=-1ms
spring.data.redis.lettuce.pool.max-idle=8
spring.data.redis.lettuce.pool.min-idle=0

3. 簡(jiǎn)單使用

package com.example.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.UUID;

@SpringBootTest
class RedisApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;
    private static final String ORDER_KEY = "order: ";

    @Test
    public void setKey() {
        int keyId = 1;
        String key = ORDER_KEY + keyId;

        String serialNo = UUID.randomUUID().toString();
        String value = "訂單" + serialNo;

        redisTemplate.opsForValue().set(key, value);

        System.out.println("添加訂單成功,訂單編號(hào):" + key);
        System.out.println("訂單信息:" + value);
    }

    @Test
    public void getKey() {
        int keyId = 1;
        String key = ORDER_KEY + keyId;

        String value = (String) redisTemplate.opsForValue().get(key);

        System.out.println("訂單信息:" + value);
    }

}

4. 問(wèn)題

上面代碼setKey得到結(jié)果如圖所示

在redis中查看這個(gè)key

可以看到出現(xiàn)了亂碼。

Redis中的key和value均是以二進(jìn)制的形式存儲(chǔ)的,因此客戶(hù)端輸入的key和value都會(huì)經(jīng)過(guò)序列化之后才發(fā)往Redis服務(wù)端。而RedisTemplate所使用序列化方式和命令行客戶(hù)端采用序列化方式不相同,進(jìn)而導(dǎo)致序列化之后的二進(jìn)制數(shù)據(jù)不同,所以才會(huì)導(dǎo)致上述的現(xiàn)象。

5. 解決

方法一:使用StringRedisTemplate

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void setKey1() {
        int keyId = 2;
        String key = ORDER_KEY + keyId;

        String serialNo = UUID.randomUUID().toString();
        String value = "訂單" + serialNo;

        stringRedisTemplate.opsForValue().set(key, value);

        System.out.println("添加訂單成功,訂單編號(hào):" + key);
        System.out.println("訂單信息:" + value);
    }

但此時(shí)發(fā)現(xiàn)中文仍是亂碼

此時(shí)啟動(dòng)redis客戶(hù)端時(shí)加上 --raw 即可

 

方法二:自定義配置redis序列化器

RedisTemplate默認(rèn)使用JDK序列化方式,StringRedisTemplate使用String序列化方式

 

因此我們可以仿照StringRedisTemplate自定義redis配置,將序列化方式設(shè)為合適類(lèi)型

package com.example.redis.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.RedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(RedisSerializer.string());
        template.setValueSerializer(RedisSerializer.json());
        template.setHashKeySerializer(RedisSerializer.string());
        template.setHashValueSerializer(RedisSerializer.json());
        return template;
    }
}

6. 連接集群

改變配置

# ========================redis集群=====================
# 獲取失敗 最大重定向次數(shù)
spring.data.redis.cluster.max-redirects=3
spring.data.redis.lettuce.pool.max-active=8
spring.data.redis.lettuce.pool.max-wait=-1ms
spring.data.redis.lettuce.pool.max-idle=8
spring.data.redis.lettuce.pool.min-idle=0
spring.data.redis.cluster.nodes=192.168.111.175:6381,192.168.111.175:6382,192.168.111.172:6383,192.168.111.172:6384,192.168.111.174:6385,192.168.111.174:6386

當(dāng)集群中的一個(gè)主機(jī)意外宕機(jī),集群能自動(dòng)感知并自動(dòng)完成主備切換,但SpringBoot客戶(hù)端沒(méi)有動(dòng)態(tài)感知到集群的最新信息

解決方案:

  • 調(diào)用RedisClusterClient.reloadPartitions
  • 后臺(tái)基于時(shí)間間隔的周期刷新
  • 后臺(tái)基于持續(xù)的 斷開(kāi) 和 移動(dòng)、重定向 的自適應(yīng)更新
# ========================redis集群=====================
# 獲取失敗 最大重定向次數(shù)
spring.data.redis.cluster.max-redirects=3
spring.data.redis.lettuce.pool.max-active=8
spring.data.redis.lettuce.pool.max-wait=-1ms
spring.data.redis.lettuce.pool.max-idle=8
spring.data.redis.lettuce.pool.min-idle=0
spring.data.redis.cluster.nodes=192.168.111.175:6381,192.168.111.175:6382,192.168.111.172:6383,192.168.111.172:6384,192.168.111.174:6385,192.168.111.174:6386
#支持集群拓?fù)鋭?dòng)態(tài)感應(yīng)刷新,自適應(yīng)拓?fù)渌⑿率欠袷褂盟锌捎玫母?,默認(rèn)false關(guān)閉
spring.data.redis.lettuce.cluster.refresh.adaptive=true
#定時(shí)刷新
spring.data.redis.lettuce.cluster.refresh.period=2000

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

綦江县| 武清区| 永顺县| 隆子县| 高安市| 时尚| 宜春市| 隆化县| 灯塔市| 增城市| 顺平县| 瑞昌市| 凉城县| 阿城市| 渭南市| 柳河县| 进贤县| 固安县| 东丰县| 太保市| 肇东市| 古田县| 阿尔山市| 湖州市| 彭山县| 大方县| 德钦县| 道孚县| 弥渡县| 武功县| 刚察县| 台南县| 祁东县| 神农架林区| 仪陇县| 合山市| 遂川县| 南丹县| 卢氏县| 嘉荫县| 枣强县|