Spring整合Redis
2015-09-07 21:57 tony4geek 阅读(3018) 评论(0) 编辑 收藏 举报Spring Redis
上一次已经安装好redis,redis提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。这次将redis 和spring 进行整合。
springapp.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 注解 -->
<context:annotation-config />
<!--扫描-->
<context:component-scan base-package="com.tony.springredis.redis.service" />
<util:properties id="appConfig" location="classpath:config/application.properties"/>
<context:property-placeholder properties-ref="appConfig"/>
<!-- redis -->
<import resource="redis.xml" />
</beans>
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${appConfig.redis.host}" p:port="${appConfig.redis.port}" p:password="${appConfig.redis.password}" p:use-pool="true" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="connectionFactory"/>
<bean id="redisStringTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
p:connection-factory-ref="connectionFactory"/>
<bean id="userRedisMap" class="org.springframework.data.redis.support.collections.DefaultRedisMap">
<constructor-arg ref="redisTemplate"/>
<constructor-arg value="USER"/>
</bean>
</beans>
application.properties
appConfig.redis.host=192.168.163.33
appConfig.redis.port=6379
appConfig.redis.user=
appConfig.redis.password=
Service.java
public interface Service<V extends DomainObject> {
public void put(V obj);
public V get(V key);
public void delete(V key);
}
userService.java
@Service("userService")
public class UserService implements com.tony.springredis.redis.service.Service<User> {
@Autowired
RedisTemplate<String, DomainObject> redisTemplate;
public void put(User user) {
redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user);
}
public void delete(User key) {
redisTemplate.opsForHash().delete(key.getObjectKey(), key.getKey());
}
public User get(User key) {
return (User) redisTemplate.opsForHash().get(key.getObjectKey(), key.getKey());
}
}
userMapService.java
@Service("userMapService")
public class UserMapService implements com.tony.springredis.redis.service.Service<User> {
@Autowired
DefaultRedisMap<String, DomainObject> userRedisMap;
public void put(User user) {
userRedisMap.put(user.getKey(), user);
}
public void delete(User key) {
userRedisMap.remove(key.getKey());
}
public User get(User key) {
return (User) userRedisMap.get(key.getKey());
}
}
App.java
public class App
{
private static ApplicationContext context;
public static void main( String[] args )
{
context = new ClassPathXmlApplicationContext("config/springapp.xml");
@SuppressWarnings("unchecked")
Service<User> userService = (Service<User>)context.getBean("userService");
User user1 = new User("tony1Key", "User 1");
User user2 = new User("tony2Key", "User 2");
System.out.println("==== 获取数据 ====");
System.out.println("User is not in redis yet: " + userService.get(user1));
System.out.println("User is not in redis yet: " + userService.get(user2));
System.out.println("==== 新增====");
userService.put(user1);
userService.put(user2);
System.out.println("User in redis yet: " + userService.get(user1));
System.out.println("User in redis yet: " + userService.get(user2));
}
}
DomainObject
public interface DomainObject extends Serializable {
String getKey();
String getObjectKey();
}
User.java
public class User implements DomainObject {
private static final long serialVersionUID = -7898194272883238670L;
public static final String OBJECT_KEY = "USER";
public User() {
}
public User(String id) {
}
public User(String id, String name) {
this.id = id;
this.name = name;
}
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
public String getKey() {
return getId();
}
public String getObjectKey() {
return OBJECT_KEY;
}
}
pom.xml
<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.tony</groupId>
<artifactId>springredis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springredis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
测试过程中报以下的错误。
Exception in thread "main" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:140)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:229)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:57)
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:85)
at org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:48)
at com.tony.springredis.redis.service.UserService.get(UserService.java:22)
at com.tony.springredis.redis.service.UserService.get(UserService.java:1)
at com.tony.springredis.redis.App.main(App.java:28)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:99)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:11)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:133)
... 12 more
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
at redis.clients.jedis.Connection.connect(Connection.java:148)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:75)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1790)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:71)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:819)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:429)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)
at redis.clients.util.Pool.getResource(Pool.java:51)
... 15 more
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:142)
... 22 more
总结了下有3个方面
- 确认网络通的,可以尝试客户端ping 服务端的网络(redis的安装机器)
- redis的redis-server是否启动成功
- redis 是否加入到linux的防火墙范围内
redis 启动成功如下
测试结果如下