如何使用SpringBoot整合RdeisCluster集群

环境

/**
*Windows 7
*JDK 1.8
*Spring Boot 2.0.2
*Redis 3.2.1
*/

随着企业数据量的增多,Redis不论作为数据存储或是缓存,它的数据量也会逐渐增多,虽然Redis的速度非常可观,但随着其中的数据量的庞大,并且仅仅在一个设备或是一个Redis实例中,其存取速度也会大打折扣,所以我们需要在不同的设备或服务器上,搭建多个Redis实例仓库,将原来的Redis的所有的keys分发到各个服务器的Redis上,这就是现在所谓的Redis集群(Redis Cluster)。
前面几天我们我们介绍了Redis与RedisCluster的安装与Java的操作了,今天我们就简单的用SpringBoot进行配置与测试,当然我们需要先构建Redis集群,这个我们也在前面说过了。

第一步 Maven依赖

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.dxf</groupId>
        <artifactId>myspringboot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>myspringboot-rediscluster</artifactId>
    <name>myspringboot-rediscluster</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-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

第二步 编写配置文件

application.yml文件

spring:
  redis:
    cluster:
      nodes:
      - 127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006
server:
  port: 80

第三步 编写启动类

StartSpringBootMain.java

package com.dxf.myspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching 
@SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
public class StartSpringBootMain {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(StartSpringBootMain.class, args);
    }
}

@EnableCaching //开启缓存
@EnableTransactionManagement // 开启事务管理

第四步 编写连接测试类

TestRedisCluster.java

package com.dxf.myspringboot;

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.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedisCluster {
 
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Test
    public void contextLoads() {
    }
    @Test
    public void test(){
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        opsForValue.set("wukonglai","www.wukonglai.com");
        System.out.println(opsForValue.get("wukonglai"));
    }
}

spring 封装了 RedisTemplate 对象来进行对redis的各种操作,它支持所有的 redis 原生的 api。

RedisTemplate中定义了对5种数据结构操作
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

第五步 启动Redis集群

image

第六步 测试

image

我们看的控制台已经正常输出信息了,下面我们去看下库中情况。


image

我们看到数据已经存到库中,但并不在7001库中,那么我们按照提示去7003库中查看


image

完成
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容