在阿里云上搭建redis高可用集群

  • *文件下载
  • 一、为什么要搭建集群?
  • 二、搭建Redis-Cluster
  • (一)搭建要求
  • (二)准备工作
  • (三)配置集群
  • 三、SpringBoot-demo 测试连接Redis集群
  • (一)引入依赖
  • (二)配置文件
  • (三)测试


*文件下载

——redis压缩包: https://redis.io/download ——redis-3.0.0.gem: 链接:https://pan.baidu.com/s/1IiKDQGxP4XU7wDEKxNoVkw 提取码:ignv

一、为什么要搭建集群?

       Redis是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系统的Hbase或者是MogoDB。Redis更适合处理高并发,一台设备的存储能力是很有限的,但是多台设备协同合作,就可以让内存增大很多倍,这就需要用到集群。
1.RedisCluster是redis的分布式解决方案,在3.0版本后推出的方案。
2.有效地解决了Redis分布式的需求,当遇到单机内存、并发等瓶颈时,可使用此方案来解决这些问题。

二、搭建Redis-Cluster

(一)搭建要求
  1. 需要 6 台 redis 服务器。搭建伪集群。
  2. 需要 6 个 redis 实例。
  3. 需要运行在不同的端口 7001-7006(自己指定)
(二)准备工作

      1.安装gcc :  Redis 是 c 语言开发的。安装 redis 需要 c 语言的编译环境。

yum install gcc-c++

      2.使用yum命令安装 ruby

yum install ruby
yum install rubygems

      3.将redis源码包上传到 linux 系统 ,解压redis源码包

      4.编译redis源码 ,进入redis源码文件夹,执行: make

看到以下输出结果,表示编译成功:

      

redis缓存集群 redis集群存储_redis


       5.创建目录/usr/local/redis-cluster目录, 安装6个redis实例,分别安装在以下目录:

            /usr/local/redis-cluster/redis-1

            /usr/local/redis-cluster/redis-2

            /usr/local/redis-cluster/redis-3

            /usr/local/redis-cluster/redis-4

            /usr/local/redis-cluster/redis-5

            /usr/local/redis-cluster/redis-6

   以第一个redis实例为例,命令:make install PREFIX=/usr/local/redis-cluster/redis-1

   出现下面提示表示成功,按此方法安装其余5个redis实例

      

redis缓存集群 redis集群存储_linux_02


      6.复制配置文件 将 /redis-3.0.0/redis.conf 复制到redis下的bin目录下

      

redis缓存集群 redis集群存储_linux_03

(三)配置集群

      1.删除每个bin目录以下的三个文件:

            

redis缓存集群 redis集群存储_linux_04


      2.对每个redis实例的bin目录下的redis.conf进行修改

            首先对bind进行注释:

redis缓存集群 redis集群存储_redis集群_05


            然后修改每个redis节点的配置文件redis.conf修改运行端口为7001 (7002 7003 …)

            

redis缓存集群 redis集群存储_redis缓存集群_06


            再将protected-mode yes前的注释yes改为no

redis缓存集群 redis集群存储_redis_07


            最后将注释的cluster-enabled yes放开

redis缓存集群 redis集群存储_redis缓存集群_08


      2.启动每个redis实例,以第一个实例为例,命令如下:

./redis-server redis.conf

      3.查看所有redis实例是否都启动成功:

ps -ef | grep redis

      

redis缓存集群 redis集群存储_redis_09


      3.上传redis-3.0.0.gem ,安装 ruby用于搭建redis集群的脚本。

gem install redis-3.0.0.gem

      4.使用 ruby 脚本搭建集群。
         进入redis源码目录中的src目录 执行下面的命令: ——>(127.0.0.1 为自己的阿里云服务器公网端口)

./redis-trib.rb create --replicas 1 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


——在配置端口号的时候,首先要保证阿里云已经对端口进行了开放
——在开放了端口的基础上,+10000再进行开放,如:redis端口号7001已经开放,还需要开放17001。
——redis子节点每次重启都会重新生成node.conf,如果在这之前对子节点进行了修改在想启动时,需要再次对node.conf 进行删除

三、SpringBoot-demo 测试连接Redis集群

(一)引入依赖
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0<ersion>
		</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>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	<ild>
(二)配置文件
spring:
  redis:
    cluster:
      nodes: 阿里云公网:7001, 阿里云公网:7002, 阿里云公网:7003, 阿里云公网:7004 阿里云公网:7005, 阿里云公网:7006
      max-redirects: 6
(三)测试
package com.example.rediscluster.redisclusterdemo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
public class RedisTest {

	@Autowired
	private RedisTemplate redisTemplate;


	@Test
	public void test1(){
		System.out.println(redisTemplate.hasKey("name"));
		redisTemplate.opsForValue().set("name", "123214");
		String name = (String) redisTemplate.opsForValue().get("name");
		System.out.println(name);
		redisTemplate.opsForValue().set("name2", "123214");
		String name2 = (String) redisTemplate.opsForValue().get("name");
		System.out.println(name2);
		redisTemplate.opsForValue().set("name3", "123214");
		String name3 = (String) redisTemplate.opsForValue().get("name");
		System.out.println(name3);
		redisTemplate.opsForValue().set("name4", "123214");
		String name4 = (String) redisTemplate.opsForValue().get("name");
		System.out.println(name4);
		HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
		hashOperations.put("user", "test", "测试");
		System.out.println(hashOperations.get("user", "test"));
	}

}