CentOS搭建Redis集群

       这里集群机器三主三从。也就是三个master,三个slave,每个master对应一个salve。所有节点的端口分别为:7001,7002,7003,7004,7005,7006。其中,主节点7001,7003,7005,对应的从节点分别为:7002,7004,7006。分别在192.168.0.106,190.168.0.1.101,192.168.0.107三台机器中进行搭建(本次搭建只用了三台机器)。
注意:必须要3个或3个以上的主节点,否则在创建集群时会失败。

一、创建redis集群

1、安装redis
解压:redis-4.0.9.tar.gz
进入解压目录

cd redis-4.0.9
make
make install

2、创建目录并修改配置文件(redis.conf)
192.168.0.106
/usr/local/redis/master/7001/7001.conf
/usr/local/redis/cluster/7002/7002.conf
192.168.0.101
/usr/local/redis/master/7003/7003.conf
/usr/local/redis/cluster/7004/7004.conf
192.168.0.107
/usr/local/redis/master/7005/7005.conf
/usr/local/redis/cluster/7006/7006.conf

注:为了方便对应,将文件重命名为端口.conf
redis.conf修改配置如下:

(1)daemonize yes  (后台运行)
(2)port  700*   (端口)
(3)bind 192.168.0.*** (绑定当前机器的ip)
(4)dir /usr/local/***   (指定数据文件存放的位置,每个节点必须要指定不同的目录)
(5)cluster-enabled yes  (启动集群模式)
(6)cluster-config-file   (配置文件:***.conf, 配置文件首次启动自动生成)
(7)cluster-node-timeout  5000
(8)appendonly yes
(9)logfile /usr/local/redis/*****700*.log

3、安装ruby
由于redis集群要使用ruby命令,所以安装一下ruby

yum install ruby
yum install rubygems
gem install redis    (安装redis和ruby的接口)

注:安装时若遇到如下错误,可以参考链接内容解决

Fetching: redis-4.1.2.gem (100%)
ERROR:  Error installing redis:
        redis requires Ruby version >= 2.3.0.

链接如下(在此多谢博主的分享):
https://www.cnblogs.com/PatrickLiu/p/8454579.html

4、分别启动所有redis节点

/usr/local/redis/redis-4.0.9/src/redis-server /usr/local/redis/master/7001.conf
/usr/local/redis/redis-4.0.9/src/redis-server /usr/local/redis/cluster/7002.conf
/usr/local/redis/redis-4.0.9/src/redis-server /usr/local/redis/master/7003.conf
/usr/local/redis/redis-4.0.9/src/redis-server /usr/local/redis/cluster/7004.conf
/usr/local/redis/redis-4.0.9/src/redis-server /usr/local/redis/master/7005.conf
/usr/local/redis/redis-4.0.9/src/redis-server /usr/local/redis/cluster/7006.conf

检查是否启动成功:

ps -ef | grep redis  

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5、执行redis-trib.rb

cd /usr/local/redis/redis-4.0.9/src
./redis-trib.rb create --replicas 1 192.168.0.106:7001 192.168.0.101:7003 192.168.0.107:7005 192.168.0.106:7002 192.168.0.101:7004 192.168.0.107:7006

注:
–replicas 1 主节点与从节点的比值
192.168.0.106:7001 192.168.0.101:7003 192.168.0.107:7005 192.168.0.106:7002 192.168.0.101:7004 192.168.0.107:7006 这些表示主从为1:1,前三个为主节点,后三个为从节点。

执行完毕如下所示:
在这里插入图片描述
到此,redis集群搭建完毕。
注:如果在创建集群时遇到如下错误,可以尝试关闭防火墙。

>>> Creating cluster
[ERR] Sorry, can't connect to node 192.168.0.101:7003

查看防火墙状态

systemctl status firewalld

关闭运行的防火墙

systemctl stop firewalld

使用客户端测试一下效果:
在这里插入图片描述
二、销毁redis集群
       当我们创建好集群之后,以后使用只需要启动各个节点就行了,不需要再重新去进行集群操作。但是如果想销毁当前集群重新创建,只需要删除每个节点的"nodes-700*.conf"文件即可,也就是cluster-config-file配置的文件。
注:若redis中存在数据,销毁后重新创建会存在问题,所以不建议销毁。无奈必须销毁的话,最好把之前的相关存储信息以及配置信息删除后重新创建,相当于一切重头开始,只是配置文件我们已经配置好了。

三、java使用jedis连接

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>3.0.1</version>
</dependency>
public static void main(String[] args) {
		Set<HostAndPort> jedisNode = new HashSet<HostAndPort>();
		jedisNode.add(new HostAndPort("192.168.0.106", 7001));
		jedisNode.add(new HostAndPort("192.168.0.106", 7002));
		jedisNode.add(new HostAndPort("192.168.0.101", 7003));
		jedisNode.add(new HostAndPort("192.168.0.101", 7004));
		jedisNode.add(new HostAndPort("192.168.0.107", 7005));
		jedisNode.add(new HostAndPort("192.168.0.107", 7006));
		
		JedisPoolConfig cfg = new JedisPoolConfig();
		cfg.setMaxTotal(100);
		cfg.setMaxIdle(20);
		cfg.setMaxWaitMillis(-1);
		cfg.setTestOnBorrow(true);
		JedisCluster jc = new JedisCluster(jedisNode, 600, 100, cfg);
		System.out.println(jc.set("name", "button"));
		System.out.println(jc.get("name"));
		System.out.println(jc.lpush("age", "28"));
		System.out.println(jc.lpop("age"));
		jc.close();
	}

四、jedis整合spring
application-redis-cluster.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd    
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  ">

	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:redis.properties" />
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxTotal" value="${redis.maxTotal}" />
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>

	<bean id="hostport1" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="192.168.0.106" />
		<constructor-arg name="port" value="7001" />
	</bean>
	<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="192.168.0.106" />
		<constructor-arg name="port" value="7002" />
	</bean>
	<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="192.168.0.101" />
		<constructor-arg name="port" value="7003" />
	</bean>
	<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="192.168.0.101" />
		<constructor-arg name="port" value="7004" />
	</bean>
	<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="192.168.0.107" />
		<constructor-arg name="port" value="7005" />
	</bean>
	<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="192.168.0.107" />
		<constructor-arg name="port" value="7006" />
	</bean>

	<bean id="redisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="nodes">
			<set>
				<ref bean="hostport1" />
				<ref bean="hostport2" />
				<ref bean="hostport3" />
				<ref bean="hostport4" />
				<ref bean="hostport5" />
				<ref bean="hostport6" />
			</set>
		</constructor-arg>
	</bean>
</beans>  

redis.properties

redis.maxIdle=20
redis.maxTotal=100
redis.maxWaitMillis=-1
redis.testOnBorrow=true

java测试类

public static void main(String[] args) {
		 ApplicationContext apx = new ClassPathXmlApplicationContext("classpath:application-redis-cluster.xml");
		 JedisCluster jc = (JedisCluster) apx.getBean("redisCluster"); 
		 System.out.println(jc.set("sex", "man"));
		 System.out.println(jc.get("sex"));
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值