您的位置:首页 > 数据库 > Redis

阿里云上搭建redis高可用集群,并测试

2020-01-12 10:21 309 查看

阿里云上搭建redis高可用集群,并测试

搭建Redis-Cluster

搭建要求集群

需要 6 台 redis 服务器。搭建伪集群。
需要 6 个 redis 实例。

创建redis文件夹

mkder redis

Redis 是 c 语言开发的。安装 redis 需要 c 语言的编译环境。如果没有 gcc 需要在线安装。

yum install gcc-c++

使用yum命令安装 ruby

yum install rubygems

下载redis

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

解压

tar -zxvf redis-4.0.8.tar.gz

进入redis

make

创建目录/user/redis/rediscluster目录, 安装6个redis实例,分别安装在以下目录

注意这里需要创建文件夹rediscluster

make install PREFIX=/user/redis/rediscluster/redis1

/user/redis/rediscluster/redis1
/user/redis/rediscluster/redis2
/user/redis/rediscluster/redis3
/user/redis/rediscluster/redis4
/user/redis/rediscluster/redis5
/user/redis/rediscluster/redis6

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

[root@localhost redis-4.0.8]# cp redis.conf
/user/redis/rediscluster/redis1/bin

[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis2/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis3/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis4/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis5/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis6/bin

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

修改ip端口号

将cluster-enabled yes 前的注释去掉

启动每个实列

在redis1/bin下启动

./redis-server redis.conf

查看下是否起来

ps -ef | grep redis

上传redis-3.0.0.gem ,安装 ruby用于搭建redis集群的脚本 这里自己下载一个 上传上去

gem install redis-3.0.0\ .gem

进入redis源码目录中的src目录 执行下面的命令

./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
不清楚的看图

启动成功

在阿里云连接需改两个地方

先查询进程
ps -ef | grep redis

这里需要注意删除redis进程

kill -9 ****

然后在自己的每个/user/redis/rediscluster/redis2/bin/redis.conf把这个端口号#掉
在阿里云上面7001到7006添加到安全组

yes改成no

再次启动redis1到redis6 在bin目录下启动

./redis-server redis.conf

再次查询进程

ps -ef | grep redis


注意阿里云端口号放开

redis下然后再去src下重新启动 注意这里是阿里云端口号

./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
成功如下

测试连接Redis集群

//引入依赖 <?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
com.example.rediscluster
redisclusterdemo
0.0.1-SNAPSHOT redisclusterdemo
Demo project for Spring Boot

<properties>
<java.version>1.8</java.version>
</properties>

<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</version>
</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>
</build>

boot配置文件

自己的端口号

spring: redis:
cluster:
nodes: 127.0.0.1:7001,127.0.0.1.250:7002,127.0.0.1.250:7003,127.0.0.1.250:7004,127.0.0.1:7005,127.0.0.1.250:7006
max-redirects: 6

Test测试

// 测试 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"));
}

}

于晓磊

项目交互部

  • 点赞
  • 收藏
  • 分享
  • 文章举报
li_mao 发布了2 篇原创文章 · 获赞 0 · 访问量 82 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: