redis与spring的集成

首先需要安装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的功能要强大的许多,,,继续研究中

你可能感兴趣的:(redis与spring的集成)