美文网首页springbootalready
Spring Boot + Sentinel + Redisso

Spring Boot + Sentinel + Redisso

作者: 一生逍遥一生 | 来源:发表于2022-07-04 10:08 被阅读0次

    由于项目中需要使用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文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.edu.redisson</groupId>
        <artifactId>redisson-test</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.4.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>3.17.3</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
        </dependencies>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </project>
    

    配置文件

    配置文件有两种形式,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: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
              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: !<org.redisson.codec.MarshallingCodec> {}
            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<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;
        }
    }
    

    进行序列化配置

    编写测试类

    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<String, Object> 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模式集成

    相关文章

      网友评论

        本文标题:Spring Boot + Sentinel + Redisso

        本文链接:https://www.haomeiwen.com/subject/vwcdbrtx.html