【问题标题】:Is this a Spring Data Redis cluster performance issue?这是 Spring Data Redis 集群性能问题吗?
【发布时间】:2017-05-04 07:23:58
【问题描述】:

我有以下代码:

@SpringBootApplication
@Component
public class ProductApiApplication2  implements CommandLineRunner{

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void run(String... args) throws Exception {

        for (int i=0;i<5;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("start time:" + System.currentTimeMillis() / 1000L);
                    String value = stringRedisTemplate.opsForList().rightPop("aaa", 5, TimeUnit.SECONDS);
                    System.out.println("get value, time :" + System.currentTimeMillis() / 1000L);

                }
            }).start();
        }

    }

    public static void main(String[] args) {
        SpringApplication.run(ProductApiApplication2.class, args);
    }
}

使用 Spring Boot 和 Spring Data Redis 构建。 我希望线程将同时完成,但事实并非如此。我得到了这个输出:

start 
start 
start 
start 
start 

get value, time :1493881455
get value, time :1493881460
get value, time :1493881466
get value, time :1493881472
get value, time :1493881477

看起来处理不是并行进行的。这是为什么呢?

这是我的配置:

spring :
  profiles : dev-company
  redis :
    cluster :
      nodes :
        - 192.168.3.171:7001
        - 192.168.3.171:7002
        - 192.168.3.168:7003
        - 192.168.3.168:7004
        - 192.168.3.169:7005
        - 192.168.3.169:7006

pom.xml

<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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
    </parent>

    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

    标签: spring-boot spring-data-redis


    【解决方案1】:

    TL;DR;

    停止编写自己的微基准测试。把这些事情做好是非常困难的。

    说明

    您正在按顺序创建和启动线程。当第一个线程已经在运行时,您正在启动下一个线程,依此类推,没有同步点。此外,您的代码不会预热连接。这意味着,根据池状态,您的 Redis 客户端将创建到集群节点的新连接或重用现有连接。连接创建会影响结果时间。

    你想要的是准备好所有线程,启动它们,等到一个条件适用,同时在所有线程上开始工作。

    类似:

        final CountDownLatch latch = new CountDownLatch(1);
    
        for (int i=0;i<5;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {   
                        latch.await();
                    } catch(Exception e) {
                    }
                    System.out.println("start time:" + System.currentTimeMillis() / 1000L);
                    String value = stringRedisTemplate.opsForList().rightPop("aaa", 5, TimeUnit.SECONDS);
                    System.out.println("get value, time :" + System.currentTimeMillis() / 1000L);
    
                }
            }).start();
        }
    
        Thread.sleep(500); // give threads enough time to initialize
    
        latch.countDown();
    

    同步性仍然很差,但在这种情况下,至少线程应该在大多数情况下同时开始工作(GC,机器速度除外)。

    【讨论】:

    • 谢谢。但现在的问题是 stringRedisTemplate.opsForList().rightPop("aaa", 5, TimeUnit.SECONDS) 阻塞所有线程
    • 我完全错过了这是一个阻塞操作。在没有超时的情况下使用rightPop,最好使用位于不同集群节点上的五个密钥。 Redis 本身是单线程的,因此如果所有请求都命中同一个节点,您将不会看到太多的吞吐量提升。
    • 使用五个键结果相同,我发现org.springframework.data.redis.connection.ClusterCommandExecutor使用了一个AsyncTaskExecutor来执行redis命令,但是AsyncTaskExecutor的threadPool上只有一个线程,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2018-10-11
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    相关资源
    最近更新 更多