高可用 HA 集群中机器挂了以后,不影响用户的使用
-
安装方式:在一台机器上搭建集群
-
安装要求:最少有三个主节点三个从节点
-
前置:
- 在/opt目录下创建redis-cluster目录
- 在/opt/redis-cluster目录下依次创建6001、6002、6003、6004、6005、6006六个目录
- 先将一个redis.conf复制/opt/redis-cluster/6000下
- 修改redis.conf文件
3.1. bind 192.168.42.130 (当前机器的ip)
3.2. protected-mode no
3.3. daemonize yes
3.4. port 600* 端口号,依次修改为6001、6002、6003、6004、6005、6006
3.5. pidfile “/var/run/redis_600*.pid” 由于是在同一台机器上,所以pid文件也要修改
3.6. logfile “/opt/cluster/600*/redis.log”
3.7. dir “/opt/redis-cluster/600*” 持久化文件等的存储位置
3.8. appendonly yes 开启aof持久化
3.9. cluster-enabled yes 启动集群模式
x删除
3.10. cluster-config-file nodes-600*.conf 这个文件用于保存集群信息,文件会自动生成到上面dir配置的目录下
clear清屏 windows下是cls清屏
将此redis.conf 文件 拷贝到上一层目录里的其他文件夹下
按up键复制上一层命令
保存退出:wq
其他的也是这样
4. 按次序启动每一个节点
/opt/redis/src/redis-server /opt/redis-cluster/6001/redis.conf
/opt/redis/src/redis-server /opt/redis-cluster/6002/redis.conf
/opt/redis/src/redis-server /opt/redis-cluster/6003/redis.conf
/opt/redis/src/redis-server /opt/redis-cluster/6004/redis.conf
/opt/redis/src/redis-server /opt/redis-cluster/6005/redis.conf
/opt/redis/src/redis-server /opt/redis-cluster/6006/redis.conf
- 创建集群
/opt/redis-5.0.3/src/redis-cli --cluster create 192.168.42.130:6001 192.168.42.130:6002 192.168.42.130:6003 192.168.42.130:6004 192.168.42.130:6005 192.168.42.130:6006 --cluster-replicas 1
world Ctrl + H 替换
6. 登录集群
/opt/redis-5.0.3/src/redis-cli -h 192.168.42.130 -p 6001 -c
-h:指定机器的ip
-p:端口号
-c:表示以集群方式登录
计算hash槽的槽号:cluster keyslot name
package com.etoak.util;
import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
public class ClusterUtil {
private static final int MAX_TOTAL = 5;
private static final int MAX_IDLE = 5;
private static final int MIN_IDLE = 1;
private static JedisPoolConfig poolConfig //
= new JedisPoolConfig();
private static Set<HostAndPort> nodes //
= new HashSet<>();
static {
poolConfig.setMaxTotal(MAX_TOTAL);
poolConfig.setMaxIdle(MAX_IDLE);
poolConfig.setMinIdle(MIN_IDLE);
for (int port = 6001; port <= 6006; port++) {
nodes.add(new HostAndPort("192.168.42.130", port));
}
}
// 获取JedisCluster实例
public static JedisCluster getCluster() {
return new JedisCluster(nodes, poolConfig);
}
}
package com.etoak.standalone;
import java.io.IOException;
import com.etoak.util.ClusterUtil;
import redis.clients.jedis.JedisCluster;
public class ClusterTest {
public static void main(String[] args) throws IOException {
JedisCluster cluster = ClusterUtil.getCluster();
cluster.set("user:id:1", "1");
String id = cluster.get("user:id:1");
System.out.println(id);
cluster.close();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.etoak.et1903.redis</groupId>
<artifactId>redis-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- JedisPoolConfig -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="10" />
<property name="maxIdle" value="8" />
<property name="minIdle" value="1" />
</bean>
<!-- HostAndPort 6个
constructor-arg name="host" value="ip地址"
constructor-arg name="port" value="6001"
-->
<bean id="node6001" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.179.128" />
<constructor-arg name="port" value="6001" />
</bean>
<bean id="node6002" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.179.128" />
<constructor-arg name="port" value="6002" />
</bean>
<bean id="node6003" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.179.128" />
<constructor-arg name="port" value="6003" />
</bean>
<bean id="node6004" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.179.128" />
<constructor-arg name="port" value="6004" />
</bean>
<bean id="node6005" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.179.128" />
<constructor-arg name="port" value="6005" />
</bean>
<bean id="node6006" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.179.128" />
<constructor-arg name="port" value="6006" />
</bean>
<bean id="cluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<ref bean="node6001"/>
<ref bean="node6002"/>
<ref bean="node6003"/>
<ref bean="node6004"/>
<ref bean="node6005"/>
<ref bean="node6006"/>
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="poolConfig" />
</bean>
<!-- JedisCluster
<constructor-arg name="nodes">
<set>
<ref bean="node6001" />
<ref bean="node6002" />
...
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="poolConfig" />
-->
</beans>
package com.etoak;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.JedisCluster;
public class ClusterTest {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext
= new ClassPathXmlApplicationContext("cluster.xml");
JedisCluster cluster = applicationContext.getBean("cluster", JedisCluster.class);
String name = cluster.get("name");
System.out.println(name);
cluster.close();
applicationContext.close();
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.etoak.et1903.redis</groupId>
<artifactId>redis-spring-data</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- JedisPoolConfig -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="10" />
<property name="maxIdle" value="8" />
<property name="minIdle" value="1" />
</bean>
<!-- 2.配置6个RedisNode -->
<bean id="node6001" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.42.130" />
<constructor-arg name="port" value="6001" />
</bean>
<bean id="node6002" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.42.130" />
<constructor-arg name="port" value="6002" />
</bean>
<bean id="node6003" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.42.130" />
<constructor-arg name="port" value="6003" />
</bean>
<bean id="node6004" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.42.130" />
<constructor-arg name="port" value="6004" />
</bean>
<bean id="node6005" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.42.130" />
<constructor-arg name="port" value="6005" />
</bean>
<bean id="node6006" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.42.130" />
<constructor-arg name="port" value="6006" />
</bean>
<!-- 3.配置RedisClusterConfiguration -->
<bean id="clusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<ref bean="node6001" />
<ref bean="node6002" />
<ref bean="node6003" />
<ref bean="node6004" />
<ref bean="node6005" />
<ref bean="node6006" />
</set>
</property>
</bean>
<!-- 4.JedisConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="clusterConfig" />
<constructor-arg name="poolConfig" ref="poolConfig" />
</bean>
<!-- 5.StringRedisTemplate -->
<bean id="stringTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<constructor-arg name="connectionFactory" ref="connectionFactory" />
</bean>
</beans>
package com.etoak;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
public class ClusterTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ioc//
= new ClassPathXmlApplicationContext("cluster.xml");
StringRedisTemplate stringTemplate//
= ioc.getBean("stringTemplate", StringRedisTemplate.class);
String name = stringTemplate.opsForValue().get("name");
System.out.println(name);
}
}