redis与spring的集成

余生长醉 提交于 2020-03-02 18:52:23

首先需要安装redis

乌班图的安装   apt-get install redis-server

然后是修改 /etc/redis/redis.conf  文件 修改里面的 bind 127.0.0.1 改成你的外网ip这样 外网才能访问

然后/etc/init.d/redis-server restart

 

客户端与spring集成 需要一下jar包

http://yun.baidu.com/share/link?shareid=1006564976&uk=958682606

下载后配置applicationContext.xml spring的配置文件

加入(注意 sentinels的地址是你客户端的地址 不要与jedisConnFactory中的服务器端地址搞混)

    <bean id="redisSentinelConfiguration"
          class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
          <property name="master">
              <bean class="org.springframework.data.redis.connection.RedisNode">
                  <property name="name" value="mymaster"></property>
              </bean>
          </property>
          <property name="sentinels">
              <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                     <constructor-arg index="0" value="192.168.1.18" />
                     <constructor-arg index="1" value="7031" />                    
                 </bean>
              <!--    <bean class="org.springframework.data.redis.connection.RedisNode">
                     <constructor-arg index="0" value="10.6.1**.**6" />
                     <constructor-arg index="1" value="7031" />                
                 </bean>
                 <bean class="org.springframework.data.redis.connection.RedisNode">                    
                     <constructor-arg index="0" value="10.6.1**.**1" />
                     <constructor-arg index="1" value="7031" />                
                 </bean> -->
             </set>
         </property>
     </bean>
 
      <bean id="jedisConnFactory"
         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
         <property name="hostName" value="115.XX.XXX.XX"/>
        <property name="port" value="6379"/>
        <property name="usePool" value="false"/>
        <constructor-arg ref="redisSentinelConfiguration"/>
     </bean>
 
     <bean id="RedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
         <property name="connectionFactory" ref="jedisConnFactory" />
     </bean>

然后再程序中可以这样调用

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/selecttest")
    @ResponseBody
    public String selecttest(HttpServletRequest request) {
        redisTemplate.opsForValue().set("name", "周小帅");
        System.out.println(redisTemplate.opsForValue().get("name"));
        return redisTemplate.opsForValue().get("name");
        
        
    }

感觉和memached的集成差不多 但是 听说redis的功能要强大的许多,,,继续研究中

后续补充:

另一种简单的xml实现方式

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"
	    p:host-name="IP" p:port="6379" p:password="XXXX" />
		  <!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
   

实现代码map取值

package com.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.model.Cat;
import com.model.User;
import com.service.TestService;
import com.util.nuozhengtong.idcard.Demo;

@Controller
public class TestController {

	@Autowired
	private TestService testService;
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, 
		 String pageNo,String pageCount, String name, 
			ModelMap modelMap) throws IOException, ParseException {
		 request.setCharacterEncoding("utf-8");  
		    response.setCharacterEncoding("utf-8");
		if (pageNo == null || pageNo.equals("") || pageCount == null
				|| pageCount.equals("")) {
			pageNo = "1";
			pageCount = "10";
		}
		if ((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageCount)>210) {
			pageNo="21";
			pageNo="10";
		}
		if (name!=null&&!name.equals("")) {
			if (name.contains("%")) {
				modelMap.put("name", URLEncoder.encode(name, "UTF-8"));	
				name=URLDecoder.decode(URLDecoder.decode(name, "UTF-8"), "UTF-8");
			}else {
				modelMap.put("name", URLEncoder.encode(URLEncoder.encode(name, "UTF-8"), "UTF-8"));	
			}
			modelMap.put("yname", URLDecoder.decode(name, "UTF-8"));
		}else{
			modelMap.put("name", "");
			name="";
		}
		Object redisname=redisTemplate.opsForHash().entries(name);//取map值
		if (redisname!=null) {
			modelMap.putAll( (Map<String, Object>)redisname);
			return new ModelAndView("/news", modelMap);
		}
		modelMap.put("pageNo", Integer.parseInt(pageNo));
		modelMap.put("pageCount", Integer.parseInt(pageCount));
		Long totleCount = testService.getCatsByUserCount(name);//TODO
		if (totleCount % 10 != 0) {
			totleCount = totleCount / 10 + 1;
		} else {
			totleCount = totleCount / 10;
		}
		List<Integer> totleCountList = new ArrayList<Integer>();
		for (int i = 0; i < totleCount; i++) {
			totleCountList.add(i + 1);
		}
		modelMap.put("totleCountList", totleCountList);
		List<Map> list = testService.getCatsByUser(name,pageNo,pageCount);//TODO
		modelMap.put("list", list);
		redisTemplate.opsForValue().set(name, modelMap.toString(), 10);
		return new ModelAndView("/news", modelMap);
		
	}
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!