`
sillycat
  • 浏览: 2527609 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Redis Cluster 2019(2)Reactive Connector Template in Spring Boot

 
阅读更多
Redis Cluster 2019(2)Reactive Connector Template in Spring Boot

Start 3 masters and 3 replicas on 2 Node Machines.

Start monitor on one machines
> redis-stat ubuntu-dev2:7001 ubuntu-dev2:7002 ubuntu-dev2:7003 ubuntu-master:7001 ubuntu-master:7002 ubuntu-master:7003 --server=7000 --daemon

Visit page http://ubuntu-dev2:7000/

Check and fix the node if it says the cluster is down.

> redis-cli --cluster check ubuntu-master:7001

> redis-cli --cluster fix ubuntu-master:7001

Normal Driver in project sillycat-spring-cloud/mvc-with-latency
Add the dependencies in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>


RedisClusterConfig.java to load all the nodes configuration
package com.sillycat.mvclatency.config;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("spring.redis.cluster")
public class RedisClusterConfig {
   
    /*
     * spring.redis.cluster.nodes[0] = 127.0.0.1:7379
     * spring.redis.cluster.nodes[1] = 127.0.0.1:7380
     * ...
     */
    List<String> nodes;
   
    @Bean
    public RedisConnectionFactory clusterConnectionFactory() {
        return new JedisConnectionFactory(
            new RedisClusterConfiguration(this.getNodes()));
    }

}

Write a simple RedisController.java to support my WRK testing
package com.sillycat.mvclatency.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(value = "/redis/")
@RestController
public class RedisController {

    @ApiOperation(value = "Redis Get", response = String.class)
    @GetMapping("/redis/get/{key}")
    public String get(@PathVariable String key) {
        String response = redisTemplateString.opsForValue().get(key);
        return response;
    }

    @ApiOperation(value = "Redis Set")
    @PostMapping(path = "/redis/set/{key}")
    public void set(@PathVariable String key, @RequestBody String request) {
        log.info("Set to Redis " + key + " = " + request);
        redisTemplateString.opsForValue().set(key, request);
    }

    @Autowired
    RedisTemplate<String, String> redisTemplateString;

}

Add my Redis Cluster information in application.yaml
spring:
# data:
#  cassandra:
#   keyspace-name: jobsmonitor
#   contact-points: ubuntu-master, ubuntu-dev2
redis:
   cluster:
    nodes:
     - ubuntu-master:7001
     - ubuntu-master:7002
     - ubuntu-master:7003
     - ubuntu-dev2:7001
     - ubuntu-dev2:7002
     - ubuntu-dev2:7003
    maxRedirects: 6

Unit test case for my RedisTemplate
package com.sillycat.mvclatency.repository;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTemplateTest {

    @Autowired
    RedisTemplate<String, String> redisTemplateString;

    @Test
    public void testBeanNotNull() {
        log.info("init the bean redisTemplateString");
        Assert.assertNotNull(redisTemplateString);
    }
   
    @Test
    public void testStringCRUD() {
        String key = "language";
        String value = "java";
        this.redisTemplateString.opsForValue().set(key, value);
        String result1 = redisTemplateString.opsForValue().get(key);
        Assert.assertEquals(result1, value);
        this.redisTemplateString.delete(key);
        String result2 = redisTemplateString.opsForValue().get(key);
        Assert.assertNull(result2);
    }

}

Reactive Driver and project web flux-with-latency-netty
Dependencies in pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

The Reactive Template Configuration
package com.sillycat.webfluxlatency.config;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("spring.redis.cluster")
public class RedisClusterConfig {

    /*
     * spring.redis.cluster.nodes[0] = 127.0.0.1:7379
     * spring.redis.cluster.nodes[1] = 127.0.0.1:7380 ...
     */
    List<String> nodes;

    @Bean
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
        return new LettuceConnectionFactory(new RedisClusterConfiguration(this.getNodes()));
    }

    @Bean
    public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString(
            ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
        return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
    }

}

The Redis Controller which helps my WRK testing
package com.sillycat.webfluxlatency.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

@Slf4j
@Api(value = "/redis/")
@RestController
public class RedisController {

    @ApiOperation(value = "Redis Get", response = String.class)
    @GetMapping("/redis/get/{key}")
    public Mono<String> get(@PathVariable String key) {
        Mono<String> response = reactiveRedisTemplateString.opsForValue().get(key);
        return response;
    }

    @ApiOperation(value = "Redis Set")
    @PostMapping(path = "/redis/set/{key}")
    public Mono<Boolean> set(@PathVariable String key, @RequestBody String request) {
        log.info("Set to Redis " + key + " = " + request);
        Mono<Boolean> response = reactiveRedisTemplateString.opsForValue().set(key, request);
        return response;
    }

    @Autowired
    ReactiveRedisTemplate<String, String> reactiveRedisTemplateString;

}

The application.yaml configuration will be similar, but the unit test will be different because of the Mono
package com.sillycat.webfluxlatency.repository;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTemplateTest {

    @Autowired
    ReactiveRedisTemplate<String, String> reactiveRedisTemplateString;

    @Test
    public void testBeanNotNull() {
        log.info("init the bean reactiveRedisTemplate");
        Assert.assertNotNull(reactiveRedisTemplateString);
    }

    @Test
    public void testStringCRUD() {
        String key = "language";
        String value = "java";
        Mono<Boolean> result1 = this.reactiveRedisTemplateString.opsForValue().set(key, value);
        StepVerifier.create(result1).expectNext(true).verifyComplete();
       
        Mono<String> result2 = reactiveRedisTemplateString.opsForValue().get(key);
        StepVerifier.create(result2).expectNext(value).verifyComplete();
       
        Mono<Long> result3 = reactiveRedisTemplateString.delete(key);
        StepVerifier.create(result3).expectNext(1l).verifyComplete();
       
        Mono<String> result4 = reactiveRedisTemplateString.opsForValue().get(key);
        StepVerifier.create(result4).expectNext().verifyComplete();
    }

}

Finally, I verified the performance from WRK, reactive driver is about 2 times faster in this case.


References:
https://github.com/eugenp/tutorials/tree/master/persistence-modules/spring-data-redis
https://www.baeldung.com/spring-data-redis-tutorial
https://www.baeldung.com/spring-data-redis-pub-sub
https://www.baeldung.com/spring-data-redis-reactive

Spring-boot-starter-data-redis
https://www.baeldung.com/spring-session
https://piotrminkowski.wordpress.com/2019/03/05/introduction-to-spring-data-redis/
Cluster
https://my.oschina.net/u/3786091/blog/1632316




分享到:
评论

相关推荐

    RedisCluster集群(Spring访问Redis)

    **RedisCluster集群与Spring访问Redis详解** Redis是一个高性能的键值数据库,广泛应用于缓存、消息中间件等场景。在大型分布式系统中,为了提供高可用性和数据冗余,我们通常会采用Redis Cluster来构建集群。本文...

    redis cluster spring整合代码

    2. **RedisConnectionFactory**:Spring会使用这些配置创建`RedisConnectionFactory`,它是连接到Redis服务器的工厂类,可以配置为使用Jedis或Lettuce客户端。 3. **RedisTemplate**或`StringRedisTemplate`:基于`...

    rediscluster.rar

    Redis Cluster是Redis官方提供的分布式解决方案,它允许用户在多个节点之间分发数据,从而实现高可用性和水平扩展。本文将详细介绍Redis Cluster的工作原理、配置、使用以及与MySQL数据库的配合。 **一、Redis ...

    redisCluster集群demo

    Redis Cluster是Redis的分布式解决方案,它允许用户在多个节点间分散数据,以实现高可用性和水平扩展。在Java中操作Redis Cluster,我们需要了解以下几个关键知识点: 1. **Redis Cluster架构**: Redis Cluster...

    codis/redis 迁移redis cluster工具

    2. **工具安装**:下载并安装迁移工具,通常是`redis-migrate-tool`。根据官方文档或README文件进行配置,设置源和目标系统的连接参数。 3. **迁移执行**:运行迁移命令,指定源和目标地址,以及需要迁移的键空间...

    rediscluster配置文件.zip

    Redis Cluster是Redis官方提供的分布式集群解决方案,用于实现数据的分布式存储和高可用性。这个压缩包文件"rediscluster配置文件.zip"包含了一系列配置文件,用于搭建一个三主三从的Redis Cluster架构。在这个架构...

    Redis高可用集群Redis Cluster搭建

    2. Redis Cluster 的优点 Redis Cluster 的主要优点是可以实现动态扩容或缩容,提高了系统的灵活性和可扩展性。此外,Redis Cluster 也可以减少冗余,提高内存利用率。 3. Redis Cluster 的实现细节 Redis ...

    jfinal redis cluster plugin-JFinal redis cluster集群插件

    使用JFinal Redis Cluster插件时,首先需要将其引入到项目中,这里我们看到有一个名为 "jfinal-rediscluster-plugin-by-shixiaotian-0.0.1.jar" 的文件,这应该是该插件的可执行版本。通常,开发者会将这个JAR文件...

    38. Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

    本文将基于Redis来探讨如何在Spring Boot中实现这一目标。 首先,让我们理解什么是Session。Session是Web应用中用于跟踪用户状态的一种机制,它存储在服务器端,避免了Cookie存储大量数据带来的安全风险。但在...

    redis-cluster和spring集成,基于cache注解

    综上所述,"redis-cluster和spring集成,基于cache注解" 的项目是一个使用 Spring Cache 集成 Redis 集群的实例,旨在通过注解的方式简化缓存管理,提高应用性能。开发者可以通过导入提供的项目,快速了解和实践这一...

    25_你能聊聊redis cluster集群模式的原理吗?.zip

    Redis Cluster是Redis官方提供的分布式集群解决方案,它允许用户在多台服务器上部署Redis实例,形成一个高可用、可扩展的数据存储集群。Redis Cluster通过数据分片(Sharding)和槽分区(Slot Partitioning)策略来...

    RedisCluster.zip

    RedisCluster是Redis的一种分布式集群解决方案,它允许将数据分散存储在多个节点上,以实现高可用性和可扩展性。在RedisCluster中,每个节点都存储一部分数据,并且负责处理一部分客户端请求,这样可以分摊服务器...

    基于Spring支持的redis线程池,(redis Cluster,主从Redis,sentenl)

    redis命令实践,基于Spring支持的redis线程池,(redis Cluster,主从Redis,sentenl)。适用人群:计算机,软件工程、人工智能,网络安全,电子信息等专业的大学生课程设计、期末大作业或毕业设计,作为“参考资料...

    redis_cluster实战栗子.rar

    Redis Cluster是Redis官方提供的分布式解决方案,它通过将数据分片(sharding)到多个节点来实现高可用性和可扩展性。在本实战栗子中,我们将深入探讨如何搭建Redis Cluster,学习其基本概念、操作指令以及如何处理...

    完整搭建redis-cluster

    **Redis Cluster 搭建全攻略** Redis 是一个高性能的键值存储系统,而 Redis Cluster 是它的分布式解决方案,提供数据的自动分片、故障转移和高可用性。本指南将详细讲解如何完整地搭建一个 Redis Cluster,同时...

    redis cluster配置文件

    redis cluster配置文件,配置后的参考; 创建目录: mkdir -p /etc/redis-cluster mkdir -p /var/log/redis mkdir -p /var/redis/7001 mkdir -p /var/redis/7002 拷贝配置文件: cp /usr/local/redis-3.2.8/redis....

    Tomcat 8+ redis cluster session

    addresses: ["redis://redis1:6379", "redis://redis2:6379", "redis://redis3:6379"] password: your_password ``` 4. **重启Tomcat**:完成以上配置后,重启Tomcat服务,使其生效。现在,当新的session被创建...

    一种高效的Redis Cluster的分布式缓存系统.pdf

    实验结果表明,在高并发访问数(例如10000以上)的场景下,RedisCluster的响应时间明显优于Codis系统,验证了RedisCluster分布式缓存系统在处理高并发访问时的高效率和优越性能。 关键词“分布式缓存”指的是分布式...

    Spring Boot 2.X 实战教程.pdf

    本课程内容包括Spring简介、Spring Boot简介、安装JDK、安装Maven、第...Redis消息、测试Spring Boot应用程序(Demo应用测试、城市模块测试)、Spring Boot Actuator(启用生产功能、终点)、阿里云服务器、Xshell、...

Global site tag (gtag.js) - Google Analytics