Redis作為目前流行Nosql數(shù)據(jù)庫(kù)竿裂,在生產(chǎn)中廣泛被使用。其基于內(nèi)存和NIO的操作劳坑,以及豐富的數(shù)據(jù)結(jié)構(gòu),為分布式業(yè)務(wù)場(chǎng)景提供高性能的存儲(chǔ)服務(wù)成畦。每個(gè)服務(wù)相當(dāng)于一個(gè)客戶端距芬,需要通過(guò)客戶端工具連接Redis服務(wù)涝开,因此衍生出了一系列客戶端工具,包含Jedis框仔,Redisson, Lettuce舀武。本文將介紹Spring Boot如何整合Redisson。
Redisson介紹
Redisson github : wiki
Redisson作為一個(gè)客戶端工具离斩,能夠使開(kāi)發(fā)者減少對(duì)Redis的關(guān)注银舱,從而將精力集中到業(yè)務(wù)上。Redisson除了提供基礎(chǔ)的Redis服務(wù)外跛梗,還提供了較為可靠的分布式鎖寻馏,布隆過(guò)濾器等功能。
引入依賴
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);
}
}
測(cè)試
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, "出現(xiàn)錯(cuò)誤");
}
log.info("加鎖成功诚欠,處理業(yè)務(wù)邏輯");
} catch (InterruptedException e) {
log.error("加鎖出現(xiàn)錯(cuò)誤, errMsg = [{}]", e.getMessage(), e);
} finally {
lock.unlock();
}
}
}