1.Spring结合集群Jedis
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<packaging>jar</packaging>
<groupId>com.bjsxt</groupId>
<artifactId>SpringJedisDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<!--注入Redis坐标-->
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<!--注入spring坐标-->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
</dependencies>
</project>
JedisDao接口:
package com.bjsxt.jedisdao;
public interface JedisDao {
public String set(String key,String value);
public String get(String key);
public Long hset(String hkey,String key,String value);
public String hget(String hkey,String key);
}
JedisDaoImplCluster实现类:
package com.bjsxt.jedisdao.impl;
import com.bjsxt.jedisdao.JedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisCluster;
public class JedisDaoImplCluster implements JedisDao {
@Autowired
private JedisCluster jedisCluster;
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public Long hset(String hkey, String key, String value) {
return jedisCluster.hset(hkey, key, value);
}
@Override
public String hget(String hkey, String key) {
return jedisCluster.hget(hkey, key);
}
}
applicationContext-jedis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.bjsxt.jedisdao"></context:component-scan>
<!--配置扫描实现类JedisDaoImplSingle:从而被初始化-->
<!--<bean id="jedisDaoImplSingle" class="com.bjsxt.jedisdao.impl.JedisDaoImplSingle"></bean>-->
<!-- jedisPool 的配置 -->
<bean id="poolconfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30"/>
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10"/>
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024"/>
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000"/>
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000"/>
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不 确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500"/>
<!-- 在获取连接的时候检查有效性, 默认 false-->
<property name="testOnBorrow" value="true"/>
<!-- 在空闲时检查有效性, 默认 false -->
<property name="testWhileIdle" value="true"/>
<!-- 连接耗尽时是否阻塞, false 报异常,ture 阻塞直到超时, 默认 true -->
<property name="blockWhenExhausted" value="false"/>
</bean>
<!--配置jedisPool:并且用构造器传入参数-->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig">
<ref bean="poolconfig"/>
</constructor-arg>
<constructor-arg name="host">
<value>192.168.92.130</value>
</constructor-arg>
<constructor-arg name="port">
<value>6379</value>
</constructor-arg>
</bean>
<!--配置扫描实现类JedisDaoImplCluster:从而被初始化-->
<bean id="jedisDaoImplCluster" class="com.bjsxt.jedisdao.impl.JedisDaoImplCluster"></bean>
<!--配置jedisCluster:并且用构造器传入参数-->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host">
<value>192.168.92.130</value>
</constructor-arg>
<constructor-arg name="port">
<value>8001</value>
</constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host">
<value>192.168.92.130</value>
</constructor-arg>
<constructor-arg name="port">
<value>8002</value>
</constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host">
<value>192.168.92.130</value>
</constructor-arg>
<constructor-arg name="port">
<value>8003</value>
</constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host">
<value>192.168.92.130</value>
</constructor-arg>
<constructor-arg name="port">
<value>8004</value>
</constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host">
<value>192.168.92.130</value>
</constructor-arg>
<constructor-arg name="port">
<value>8005</value>
</constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host">
<value>192.168.92.130</value>
</constructor-arg>
<constructor-arg name="port">
<value>8006</value>
</constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name="poolConfig">
<ref bean="poolconfig"/>
</constructor-arg>
</bean>
</beans>
测试类:
package com.bjsxt.test;
import com.bjsxt.jedisdao.JedisDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringJedis {
public static void main(String[] args) {
//TestSpringJedis.testSpringJrdisSingle();
TestSpringJedis.testSpringJrdisCluster();
}
/*单机版*/
public static void testSpringJrdisSingle(){
//启动xml文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-Jedis.xml");
//获取对象
JedisDao jd = (JedisDao)ac.getBean("jedisDaoImplSingle");
//对象调用方法
String set = jd.set("jedis", "springJedis");
System.out.println(set);
String get = jd.get("jedis");
System.out.println(get);
}
/*集群版*/
public static void testSpringJrdisCluster(){
//启动xml文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext-Jedis.xml");
//获取对象
JedisDao jd = (JedisDao)ac.getBean("jedisDaoImplCluster");
//对象调用方法
String set = jd.set("jedis2", "springJedis2");
System.out.println(set);
String get = jd.get("jedis2");
System.out.println(get);
}
}