Spring Boot + Sentinel + Redisson 集成

由于项目中需要使用spring boot、redis、redisson,现将自己的配置记录下。

软件版本

spring-boot-starter-parent 2.3.4.RELEASE
spring-boot-starter-web 2.3.4.RELEASE
spring-boot-starter-data-redis 2.3.4.RELEASE
redisson-spring-boot-starter 3.17.3
pom.xml文件如下:



    4.0.0
    com.edu.redisson
    redisson-test
    1.0-SNAPSHOT
    
        8
        8
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.4.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.redisson
            redisson-spring-boot-starter
            3.17.3
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
    
    
        
            
                src/main/resources
                
                    **/*
                
            
        
    

配置文件

配置文件有两种形式,Spring boot 和redisson的形式
spring boot 形式:

spring:
  redis:
    database: 0
    port: 6379
    password: password
    sentinel:
      master: mymaster
      nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
      password: password

redisson 形式:

spring:
  redis:
    redisson:
      config: |-
        sentinelServersConfig:
          idleConnectionTimeout: 10000
          connectTimeout: 10000
          timeout: 3000
          retryAttempts: 3
          retryInterval: 1500
          failedSlaveReconnectionInterval: 3000
          failedSlaveCheckInterval: 60000
          password: password
          subscriptionsPerConnection: 5
          loadBalancer: ! {}
          subscriptionConnectionMinimumIdleSize: 1
          subscriptionConnectionPoolSize: 50
          slaveConnectionMinimumIdleSize: 24
          slaveConnectionPoolSize: 64
          masterConnectionMinimumIdleSize: 24
          masterConnectionPoolSize: 64
          readMode: "SLAVE"
          subscriptionMode: "SLAVE"
          sentinelAddresses:
            - "redis://127.0.0.1:26379"
            - "redis://127.0.0.1:26380"
            - "redis://127.0.0.1:26381"
          masterName: "mymaster"
          database: 0
        threads: 16
        nettyThreads: 32
        codec: ! {}
        transportMode: "NIO"

添加代码验证

添加redis配置

package com.edu.redisson.comfig;

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
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate();
        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;
    }
}

进行序列化配置

编写测试类

package com.edu.redisson.controller;

import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class RedissonController {
    @Autowired
    private RedissonClient redissonClient;

    @Resource
    @Qualifier("redisTemplate")
    private RedisTemplate template;

    @GetMapping("/read")
    public String readData(String key){
        return redissonClient.getBucket(key).get().toString();
    }

    @GetMapping("/write")
    public String writeData(String key,String value){
        redissonClient.getBucket(key).set(value);
        return "write";
    }

    @RequestMapping("redisread")
    public String redisRead(String key){
        return template.opsForValue().get(key).toString();
    }

    @RequestMapping("rediswrite")
    public String reisWrite(String key,String value){
        template.opsForValue().set(key,value);
        return "rediswrite";
    }
}

通过postman,可以测试是否可以正常写数据和读数据。
在测试的过程中,要先保证已经有这个缓存,否则会出现异常。

参考文献

redisson-spring-boot-starter
Redisson+Springboot配置(哨兵模式)
Sentinel YAML config format
Redisson最新版starter模式集成

版权声明:
作者:主机优惠
链接:https://www.techfm.club/p/60166.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>