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

SpringBoot集成Redis之RedisTemplate實踐

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

1. 依賴

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

2. 配置

# ========================redis單機=====================
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. 簡單使用

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("添加訂單成功,訂單編號:" + 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. 問題

上面代碼setKey得到結果如圖所示

在redis中查看這個key

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

Redis中的key和value均是以二進制的形式存儲的,因此客戶端輸入的key和value都會經(jīng)過序列化之后才發(fā)往Redis服務端。而RedisTemplate所使用序列化方式和命令行客戶端采用序列化方式不相同,進而導致序列化之后的二進制數(shù)據(jù)不同,所以才會導致上述的現(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("添加訂單成功,訂單編號:" + key);
        System.out.println("訂單信息:" + value);
    }

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

此時啟動redis客戶端時加上 --raw 即可

 

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

RedisTemplate默認使用JDK序列化方式,StringRedisTemplate使用String序列化方式

 

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

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

當集群中的一個主機意外宕機,集群能自動感知并自動完成主備切換,但SpringBoot客戶端沒有動態(tài)感知到集群的最新信息

解決方案:

  • 調(diào)用RedisClusterClient.reloadPartitions
  • 后臺基于時間間隔的周期刷新
  • 后臺基于持續(xù)的 斷開 和 移動、重定向 的自適應更新
# ========================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
#支持集群拓撲動態(tài)感應刷新,自適應拓撲刷新是否使用所有可用的更新,默認false關閉
spring.data.redis.lettuce.cluster.refresh.adaptive=true
#定時刷新
spring.data.redis.lettuce.cluster.refresh.period=2000

總結

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

相關文章

最新評論

舞钢市| 千阳县| 三门峡市| 靖边县| 冷水江市| 大城县| 昌邑市| 饶平县| 黄大仙区| 大城县| 永清县| 青海省| 香港 | 出国| 怀宁县| 乐清市| 木兰县| 和林格尔县| 新田县| 大安市| 叶城县| 苍梧县| 宁安市| 清徐县| 米易县| 蛟河市| 宝兴县| 阳谷县| 澄江县| 桦南县| 高要市| 昌邑市| 黔江区| 离岛区| 哈尔滨市| 福贡县| 武穴市| 观塘区| 江城| 定安县| 同仁县|