美文网首页spring 整合使用JavaRedis
Redis系列:Spring Boot整合Redisson

Redis系列:Spring Boot整合Redisson

作者: Renaissance_ | 来源:发表于2021-06-10 16:53 被阅读0次

    Redis作为目前流行Nosql数据库,在生产中广泛被使用。其基于内存和NIO的操作,以及丰富的数据结构,为分布式业务场景提供高性能的存储服务。每个服务相当于一个客户端,需要通过客户端工具连接Redis服务,因此衍生出了一系列客户端工具,包含Jedis,Redisson, Lettuce。本文将介绍Spring Boot如何整合Redisson。

    Redisson介绍

    Redisson github : wiki
    Redisson作为一个客户端工具,能够使开发者减少对Redis的关注,从而将精力集中到业务上。Redisson除了提供基础的Redis服务外,还提供了较为可靠的分布式锁,布隆过滤器等功能。

    引入依赖

    pom.xml,引入Redisson 3.13.4版本

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.0</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.elio</groupId>
        <artifactId>redission-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>redission-demo</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
            <redission.version>3.13.4</redission.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- 操作Redis-->
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>${redission.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    配置application.properties

    spring.redission.host = 127.0.0.1
    spring.redission.port = 6379
    

    新增RedisConfig

    package com.elio.redissiondemo.config;
    
    import org.redisson.Redisson;
    import org.redisson.api.RedissonClient;
    import org.redisson.config.Config;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @ClassName : RedissionConfig
     * @Description : redission
     * @Date: 2021/06/09
     */
    @Configuration
    public class RedissonConfig {
    
        @Value("${spring.redission.host}")
        private String host;
    
        @Value("${spring.redission.port}")
        private String port;
    
        @Bean
        public RedissonClient getRedissonClient() {
            Config config = new Config();
            config.useSingleServer().setAddress("redis://"+host+":"+port+"");
    
            return Redisson.create(config);
        }
    }
    
    

    测试

     package com.elio.redissiondemo;
    
    import com.elio.redissiondemo.exception.BusinessException;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.redisson.api.RBucket;
    import org.redisson.api.RLock;
    import org.redisson.api.RedissonClient;
    
    import javax.annotation.Resource;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @ClassName : RedissionTest
     * @Description :
     * @Date: 2021/06/10
     */
    @Slf4j
    public class RedissionTest extends RedissonDemoApplicationTests {
    
        @Resource
        RedissonClient redissonClient;
    
        @Test
        public void testRedisson() {
            RBucket<String> result = redissonClient.getBucket("hello");
            result.set("world", 100, TimeUnit.SECONDS);
            log.info("result = [{}]" , result.get());
        }
    
        @Test
        public void testRedissonLock() {
            RLock lock = redissonClient.getLock("lock");
            try {
                boolean result = lock.tryLock(3, 3, TimeUnit.SECONDS);
                if(!result) {
                    throw new BusinessException(100, "出现错误");
                }
                log.info("加锁成功,处理业务逻辑");
            } catch (InterruptedException e) {
                log.error("加锁出现错误, errMsg = [{}]", e.getMessage(), e);
            } finally {
                lock.unlock();
            }
        }
    }
    
    

    测试结果

    image.png

    相关文章

      网友评论

        本文标题:Redis系列:Spring Boot整合Redisson

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