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測試了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章