Redis集群redis-cluster的搭建及集成springboot

前言

有兴趣的童鞋可以浏览一下:Redis介绍和使用场景详解Redis数据类型及使用场景

知其然,也应知其所以然。

一、安装Redis

1、安装wget

yum install wget

2、安装redis

wget http://download.redis.io/releases/redis-4.0.6.tar.gz

此时已下载redis到当前目录,移到另一个目录下(大家都放在这了,统一吧,以后方便排查问题):

mkdir /usr/local/redis
cd  /usr/local/redis
tar -zxvf redis-4.0.6.tar.gz

执行完上面三步,解压完成。(注意自定移动jar包到/usr/local/redis目录啊,用mv命令)

3、安装gcc依赖

yum install gcc

4、编译安装

首先进入解压后的目录:

cd redis-4.0.6

然后编译:

make MALLOC=libc

下一步将/usr/local/redis-4.0.6/src目录下的文件加到/usr/local/bin目录:

5、安装成功,尝试启动

按照步骤来的话,此时的目录是这样的。
在这里插入图片描述

./redis-server

在这里插入图片描述


二、配置Redis集群

搭集群找到一个大佬:Redis 集群搭建详细指南
(大佬文章里面全局替换有点问题,望注意:他的截图是对的,但是命令少了一个/)
在这里插入图片描述

三、整合springboot

ps:新建springboot项目就不说了,直接聊配置吧,下面是我的项目架构。
在这里插入图片描述

1、添加maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
    <version>1.3.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

2、配置application.yml

在这里插入图片描述

spring:
  redis:
    cluster:
      #设置key的生存时间,当key过期时,它会被自动删除;
      expire-seconds: 120
      #设置命令的执行时间,如果超过这个时间,则报错;
      command-timeout: 5000
      #设置redis集群的节点信息,其中namenode为域名解析,通过解析域名来获取相应的地址;
      nodes: 192.168.233.11:9001,192.168.233.11:9002,192.168.233.11:9003,192.168.233.11:9004,192.168.233.11:9005,192.168.233.11:9006

server:
  port: 8085
  servlet:
    context-path: /redis

3、读取配置生成entity

package com.gzky.study.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Redis实体类(由配置文件读取生成)
 *
 * @author zhaohualuo
 * @date 2019/12/17
 **/
//依赖注入
@Component
//该注解用于读取配置文件中的属性,其中prefix表示前缀;
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisEntity {
    private int expireSeconds;
    private String nodes;
    private int commandTimeout;

    public int getExpireSeconds() {
        return expireSeconds;
    }

    public void setExpireSeconds(int expireSeconds) {
        this.expireSeconds = expireSeconds;
    }

    public String getNodes() {
        return nodes;
    }

    public void setNodes(String nodes) {
        this.nodes = nodes;
    }

    public int getCommandTimeout() {
        return commandTimeout;
    }

    public void setCommandTimeout(int commandTimeout) {
        this.commandTimeout = commandTimeout;
    }

    @Override
    public String toString() {
        return "RedisEntity{" +
                "expireSeconds=" + expireSeconds +
                ", nodes='" + nodes + '\'' +
                ", commandTimeout=" + commandTimeout +
                '}';
    }
}

4、获取JedisCluster对象

package com.gzky.study.utils;

import com.gzky.study.entity.RedisEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * 读取配置文件,生成JedisCluster对象
 *
 * @author zhaohualuo
 * @date 2019/12/17
 **/
@Configuration
public class RedisConfig {

    @Autowired
    private RedisEntity redisEntity;

    @Bean
    public JedisCluster getJedisCluster(){
        //获取redis集群的ip及端口号等相关信息;
        String[] serverArray = redisEntity.getNodes().split(",");
        System.out.println(serverArray.toString());
        Set<HostAndPort> nodes = new HashSet<>();

        //遍历add到HostAndPort中;
        for (String ipPort : serverArray) {
            String[] ipPortPair = ipPort.split(":");
            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
        }
        //构建对象并返回;
        return new JedisCluster(nodes, redisEntity.getCommandTimeout());
    }
}

5、编写Redis接口

package com.gzky.study.service;

/**
 * Redis集群接口
 *
 * @author zhaohualuo
 * @date 2019/12/17
 **/
public interface RedisService {
    String set(String key, String value);

    String get(String key);

    Boolean exists(String key);

    Long expire(String key, int seconds);

    Long ttl(String key);

    Long incr(String key);

    Long hset(String key, String field, String value);

    String hget(String key, String field);

    Long hdel(String key, String... field);
}

6、编写Redis接口实现类

package com.gzky.study.service.impl;

import com.gzky.study.service.RedisService;
import com.gzky.study.utils.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

/**
 * Redis集群实现类
 *
 * @author zhaohualuo
 * @date 2019/12/17
 **/
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    JedisCluster jedisCluster;

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    @Override
    public String get(String key) {
        return jedisCluster.get(key);
    }

    @Override
    public Boolean exists(String key) {
        return jedisCluster.exists(key);
    }

    @Override
    public Long expire(String key, int seconds) {
        return jedisCluster.expire(key, seconds);
    }

    @Override
    public Long ttl(String key) {
        return jedisCluster.ttl(key);
    }

    @Override
    public Long incr(String key) {
        return jedisCluster.incr(key);
    }

    @Override
    public Long hset(String key, String field, String value) {
        return jedisCluster.hset(key, field, value);
    }

    @Override
    public String hget(String key, String field) {
        return jedisCluster.hget(key, field);
    }

    @Override
    public Long hdel(String key, String... field) {
        return jedisCluster.hdel(key, field);
    }
}

7、编写对外接口

package com.gzky.study.controller;

import com.gzky.study.entity.RedisEntity;
import com.gzky.study.service.RedisService;
import com.gzky.study.utils.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Redis对外接口
 *
 * @author zhaohualuo
 * @date 2019/12/17
 **/
@RestController
public class RedisController {

    /**
     * 获取集群对象
     */
    @Autowired
    RedisConfig redisConfig;

    /**
     * 获取基础配置
     */
    @Autowired
    RedisEntity redisEntity;

    /**
     * 接口
     */
    @Autowired
    RedisService redisService;

    @RequestMapping(value = "/getRedisValue", method = RequestMethod.GET)
    @ResponseBody
    public String getRedisValue(String key) {
        System.out.println("配置:" + redisEntity.toString());
        System.out.println("节点:" + redisConfig.getJedisCluster().getClusterNodes());
        redisService.set("name", "赵");
        redisService.set("schollo", "qust");
        System.out.println(redisService.get("name") +":"+ redisService.get("schollo"));
        return redisService.get(key);
    }

    @RequestMapping(value = "/setRedisValue", method = RequestMethod.GET)
    @ResponseBody
    public String setRedisValue(String key,String value) {
        return redisService.set(key,value);
    }
}

8、用postman测试功能

在这里插入图片描述
简单测试完成,接下来可以自行set、get测试了。

猜你喜欢

转载自blog.csdn.net/qq_33247435/article/details/103563160
今日推荐