redis集群使用的一台虚拟机模拟6台服务器。
applicationContext-redis.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 加载数据 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<!-- redis集群配置 -->
<bean id="host1" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis1.port}"></constructor-arg>
</bean>
<bean id="host2" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis2.port}"></constructor-arg>
</bean>
<bean id="host3" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis3.port}"></constructor-arg>
</bean>
<bean id="host4" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis4.port}"></constructor-arg>
</bean>
<bean id="host5" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis5.port}"></constructor-arg>
</bean>
<bean id="host6" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host}"></constructor-arg>
<constructor-arg name="port" value="${redis6.port}"></constructor-arg>
</bean>
<bean id="redisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<ref bean="host1"/>
<ref bean="host2"/>
<ref bean="host3"/>
<ref bean="host4"/>
<ref bean="host5"/>
<ref bean="host6"/>
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
<constructor-arg name="timeout" value="${redis.timeout}"></constructor-arg>
</bean>
</beans>
配置文件db.properties
redis.host=192.168.159.140
redis1.port=7001
redis2.port=7002
redis3.port=7003
redis4.port=7004
redis5.port=7005
redis6.port=7006
redis.maxIdle=50
redis.maxTotal=100
redis.maxWaitMillis=1000
redis.testOnBorrow=true
redis.timeout=6000
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>redis-cluster</display-name>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
ClusterTest
package com.redis.test;
import java.util.HashSet;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author 刘喆
* @date 2018年11月15日
*/
public class ClusterTest {
//spring
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
JedisCluster jedisCluster = (JedisCluster) context.getBean("redisCluster");
jedisCluster.flushAll();
jedisCluster.set("name", "redis");
System.out.println(jedisCluster.get("name"));
}
//编码
// public static void main(String[] args) {
// JedisPoolConfig poolConfig = new JedisPoolConfig();
// poolConfig.setMaxIdle(50);
// poolConfig.setMinIdle(30);
// poolConfig.setMaxTotal(300);
// poolConfig.setTestOnBorrow(true);
// Set<HostAndPort> nodes = new HashSet<>();
// HostAndPort host1 = new HostAndPort("192.168.159.140",7001);
// nodes.add(host1);
// HostAndPort host2 = new HostAndPort("192.168.159.140",7002);
// nodes.add(host2);
// HostAndPort host3 = new HostAndPort("192.168.159.140",7003);
// nodes.add(host3);
// HostAndPort host4 = new HostAndPort("192.168.159.140",7004);
// nodes.add(host4);
// HostAndPort host5 = new HostAndPort("192.168.159.140",7005);
// nodes.add(host5);
// HostAndPort host6 = new HostAndPort("192.168.159.140",7006);
// nodes.add(host6);
// JedisCluster jedisCluster = new JedisCluster(nodes, 6000, poolConfig);
// jedisCluster.set("name", "redis");
// System.out.println(jedisCluster.get("name"));
// }
}
运行结果
十一月 16, 2018 9:25:17 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@617c74e5: startup date [Fri Nov 16 09:25:17 GMT+08:00 2018]; root of context hierarchy
十一月 16, 2018 9:25:17 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
十一月 16, 2018 9:25:18 上午 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [db.properties]
redis
github:https://github.com/LiuZhe715718/redis-cluster