redis 整合SpringMvc

1、使用jedis 进行整合redis 和SpringMvc,项目框架如下:

所有的包都在一个工程里,很不好,后续拆分开来进行开发。

pom.xml 引入jedis

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
	<dependency>
	    <groupId>redis.clients</groupId>
	    <artifactId>jedis</artifactId>
	    <version>2.9.0</version>
	</dependency>

--jedis 文件在package:package com.fandong.weapon.database

JedisClusterFactory.java

/**
 * 
 */
package com.fandong.weapon.database;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig; 
import redis.clients.jedis.HostAndPort;
/**
 * @author fandong
 *
 */
public class JedisClusterFactory {
	
   public JedisCluster getJedisCLuster() {
		return jedisCLuster;
	}

	public void setJedisCLuster(JedisCluster jedisCLuster) {
		this.jedisCLuster = jedisCLuster;
	}

	public List<String> getHostPorts() {
		return hostPorts;
	}

	public void setHostPorts(List<String> hostPorts) {
		this.hostPorts = hostPorts;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

   private JedisCluster jedisCLuster;
   
   private List<String> hostPorts;
   
   private int timeout;
   
   private Log logger = LogFactory.getLog(JedisClusterFactory.class);
   
   public void init() {
	   
	   JedisPoolConfig jedispoolConfig = new JedisPoolConfig();
	   
	   Set<HostAndPort> nodeSet = new HashSet<HostAndPort>();
	   
	   for(String hostPort : hostPorts) {
		   String[] hostAndPort=hostPort.split(":");
		   if(hostAndPort.length!=2)continue;
		   nodeSet.add(new HostAndPort(hostAndPort[0],Integer.parseInt(hostAndPort[1])));
	   }
	   try {
		   logger.info("开始初始化jedis程序");
		   jedisCLuster = new JedisCluster(nodeSet,timeout,jedispoolConfig);
		   System.out.println("redis pool是否成功创建。");
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(), e);
		}
	   
   }
   
   public void destroy() {
	   if(jedisCLuster !=null) {
		   try {
			jedisCLuster.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(),e);
		}
	   }
	
   }
   
   
   
}

在service 包中定义Redis 的相关操作。

RedisService.java

/**
 * 
 */
package com.fandong.weapon.service;

/**
 * @author fandong
 *
 */
public interface RedisService {
  
	String set(String key, String value);
	String get(String key);
  
}

再包impl 进行实现接口。

注意在使用@Service @Autowired 进行注释。

/**
 * 
 */
package com.fandong.weapon.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fandong.weapon.service.RedisService;

import redis.clients.jedis.JedisCluster;

/**
 * @author fandong
 *
 */
@Service
public class JedisClusterRedisServiceImpl implements RedisService{

    @Autowired
	private JedisCluster jedisCluster;
	public String set(String key, String value) {
		// TODO Auto-generated method stub
		return jedisCluster.set(key,value);
	}

	public String get(String key) {
		// TODO Auto-generated method stub
		return jedisCluster.get(key);
	}
	
}

在Resource 中进行配置;

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                         http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <bean id="jedisClusterFactory" class="com.fandong.weapon.database.JedisClusterFactory" init-method="init" destroy-method="destroy">
      <property name="hostPorts">
         <list>
            <value>192.168.1.121:8000</value>
            <value>192.168.1.121:8001</value>
            <value>192.168.1.121:8002</value>
            <value>192.168.1.121:8003</value>
            <value>192.168.1.121:8004</value>
            <value>192.168.1.121:8005</value>
         </list>
      </property>
      <property name="timeout">
         <value>1000</value>
      </property>
    </bean>
    <bean id="jedisCluster" factory-bean="jedisClusterFactory" factory-method="getJedisCLuster"></bean>
</beans>

这里需要使用spring bean 进行对象的实例化。

Controller

前端jsp页面:

登录redis 中改变key11的值:

[root@hadoop05 bin]# ./redis-cli -c -h 192.168.1.121 -p 8000
192.168.1.121:8000> get key11
"hhhSpringMvc"
192.168.1.121:8000> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值