私人笔记二:Spring连接redis
Spring连接redis
一、新建Spring项目
配置文件如下:
POM文件 <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.redisTest</groupId> <artifactId>redisTest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>redisTest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.4.RELEASE</spring.version> <jackson.version>2.5.0</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 1、java连接Redis的jar包,也就是使用jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.4.2</version> </dependency> <!-- 2、spring整合Redis的jar包 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.4.2.RELEASE</version> </dependency> </dependencies> <build> <finalName>redisTest</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <finalName>mgr</finalName> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> </project>
web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> </web-app>
applicationContext.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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 引入redis配置文件 --> <import resource="classpath:spring/redis-context.xml"/> </beans>
redis-context.xml 以main方法单节点测试为例 <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描redis配置文件--> <context:property-placeholder ignore-unresolvable="true" 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}" /> <property name="testOnReturn" value="${redis.testOnReturn}" /> </bean> <!--设置链接属性--> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.host}" p:port="${redis.port}" p:password="" p:pool-config-ref="poolConfig" p:timeout="100000"/> <!-- Jedis模板配置 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> </beans>
二、开始测试
- main方法测试单节点
package com.redisTest; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.core.RedisTemplate; public class test1 { public static void main(String[] args){ String[] classConfig = {"spring/applicationContext.xml","spring/redis-context.xml"}; ApplicationContext app = new ClassPathXmlApplicationContext(classConfig); RedisTemplate redisTemplate = (RedisTemplate)app.getBean(RedisTemplate.class); redisTemplate.opsForValue().set("test1","测试数据1"); System.out.print(redisTemplate.opsForValue().get("test1")); } }
运行结果
- main方法测试cluster集群
这块项目搭建因为POM依赖是在网上复制粘贴的,所以有些版本问题,最后修改POM文件如下(版本这里真是坑啊。。)
新的POM依赖如下
<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.redisTest</groupId> <artifactId>redisTest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>redisTest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.1.RELEASE</spring.version> <jackson.version>2.5.0</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 1、java连接Redis的jar包,也就是使用jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.10.2</version> </dependency> <!-- 2、spring整合Redis的jar包 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.22.RELEASE</version> </dependency> </dependencies> <build> <finalName>redisTest</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <finalName>mgr</finalName> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> </project>
redis-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <description>redis cluster配置</description> <!-- 加载properties文件 --> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- 配置JedisPoolConfig实例 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大空闲数--> <property name="maxIdle" value="${redis.maxIdle}" /> <!-- 最小空闲数 --> <property name="minIdle" value="${redis.minIdle}" /> <!--连接池的最大数据库连接数 --> <property name="maxTotal" value="${redis.maxTotal}" /> <!-- 最大等待时间 单位毫秒(ms) --> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <!-- 使用连接时测试连接是否可用 --> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- redis集群配置 cluster模式 --> <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="${redis.maxRedirects}"></property> <property name="clusterNodes"> <set> <bean name="sentinelNode1" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${cluster1.host}"></constructor-arg> <constructor-arg name="port" value="${cluster1.port}"></constructor-arg> </bean> <bean name="sentinelNode2" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${cluster2.host}"></constructor-arg> <constructor-arg name="port" value="${cluster2.port}"></constructor-arg> </bean> <bean name="sentinelNode3" class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${cluster3.host}"></constructor-arg> <constructor-arg name="port" value="${cluster3.port}"></constructor-arg> </bean> </set> </property> </bean> <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"></constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> <property name="password" value="${redis.password}"></property> <property name="timeout" value="${redis.timeout}"></property> </bean> <!-- Jedis模板配置 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jeidsConnectionFactory" /> </bean> </beans>
redis.properties如下
#redis中心 #redis的服务器地址 redis.host=192.168.160.130 #redis的服务端口 redis.port=6379 #密码 redis.password= #缓存池最小空闲数 redis.minIdle=5 #缓存池最大空闲数 redis.maxIdle=50 #缓存池最大连接数 redis.maxTotal=200 #最大等待时间 redis.maxWaitMillis=3000 #客户端超时时间单位是毫秒 redis.timeout=100000 #明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 redis.testOnBorrow=true redis.maxRedirects=3 cluster1.host=192.168.160.130 cluster1.port=6379 cluster2.host=192.168.160.130 cluster2.port=6380 cluster3.host=192.168.160.130 cluster3.port=6381 #rediscluster spring.redis.cluster.nodes=192.168.160.130:6379,192.168.160.130:6380,192.168.160.130:8381 spring.redis.cluster.max-redirects=3 #rediscluster
测试代码不用改变
最近有点偷懒了。。