Redis服务器
客户端
org.example.ReactiveRedisTemplateTest
package org.example;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import reactor.core.publisher.Mono;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
public class ReactiveRedisTemplateTest {
private static long LIMIT = Runtime.getRuntime().freeMemory() / 1024 / 128;
static {
System.out.println("LIMIT: " + LIMIT);
}
public static void main(String[] args) throws InterruptedException {
int requests = args.length > 0 ? Integer.parseInt(args[0]) : 1000;
LettuceConnectionFactory factory = new LettuceConnectionFactory("192.168.253.176", 6379);
factory.setAutoStartup(true);
factory.setEagerInitialization(true);
factory.setShareNativeConnection(true);
factory.start();
ReactiveStringRedisTemplate reactiveRedisTemplate = new ReactiveStringRedisTemplate(factory);
// 存储一个字符串值
reactiveRedisTemplate.opsForValue().set("count", "0").block();
// 获取存储的值
String value = reactiveRedisTemplate.opsForValue().get("count").block();
System.out.println("获取的值: " + value);
CountDownLatch latch = new CountDownLatch(requests);
AtomicLong counter = new AtomicLong(0);
long begin = System.currentTimeMillis();
for (int i = 0; i < requests; i++)
{
Mono<Long> count = reactiveRedisTemplate.opsForValue().increment("count", 1L);
counter.incrementAndGet();
count.subscribe( r -> {
latch.countDown();
counter.decrementAndGet();
if( latch.getCount() % 100000 == 0 ) {
System.out.println("count: " + r);
}
}, e -> {
latch.countDown();
System.out.println("error: " + e);
});
while( counter.get() > LIMIT )
{
Thread.yield();
}
}
latch.await();
long end = System.currentTimeMillis();
long delta = end - begin;
System.out.println("总计" + requests+"次请求");
System.out.println("总计耗时" + delta + "ms");
System.out.println("平均耗时" + delta / requests + "ms");
System.out.println("QPS:" + requests * 1000L / delta + "次/秒");
// 检查键是否存在
// boolean exists = redisTemplate.hasKey("key1");
// System.out.println("key1 是否存在: " + exists);
System.exit(0);
}
}
pom文件
<?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>org.example</groupId>
<artifactId>RedisTemplateTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j2-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.23.1</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/com.conversantmedia/disruptor -->
<dependency>
<groupId>com.conversantmedia</groupId>
<artifactId>disruptor</artifactId>
<version>1.2.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>3.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
压测
执行结果