国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Spring 整合redis集群 實(shí)現(xiàn) 以及過程中需要注意的問題點(diǎn)

一,準(zhǔn)備工作:

1.首先安裝好redis集群,啟動(dòng)并配置集群。

2.SpringMVC環(huán)境,看項(xiàng)目或個(gè)人需要,可以使SpringMVC的web項(xiàng)目,也可以是只使用SpringBean管理器。

二,著手配置:

由于項(xiàng)目是由maven管理的所以需要的jar 包添加到maven 的pom文件即可


1.添加jar依賴,再maven pom.xml 文件中添加依賴如下:// 這里需要說明下,依賴的jar包 redis.client  和 spring-data-redis 的版本匹配問題 實(shí)驗(yàn)了好幾個(gè)對應(yīng)如下

       redis.client  2.9.0 ---- spring-data-redis  1.7.1.RELEASE

       redis.client 2.9.0 -----spring-data-redis   1.7.2.RELEASE    這兩個(gè)是可以使用的

       由于版本不匹配,遇到的錯(cuò)誤如下:

         1. ClassNotFoundException  : redis/client/util/geoUtils    說這個(gè)類找不到。

         2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [applicationContext.xml]

             說創(chuàng)建 redisTemplate bean 對象時(shí)失敗了。

           <!-- jedis (一個(gè)redis client端的jar)-->

          <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

         <!-- spring-data-redis 依賴-->

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.1.RELEASE</version>
        </dependency>

2.配置redis 配置文件 我把它單獨(dú)提取出來放在一個(gè)配置文件(spring-data-redis-cluster.xml)中,然后import 到 spring ApplicationContext.xml 文件中:

spring-data-redis-cluster.xml 配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

     <!--  redis連接池  這里引用的是jedis 包中的功能  -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxActive:1024}" />
        <property name="maxIdle" value="${redis.maxIdle:1024}" />
        <property name="maxWaitMillis" value="${redis.maxWait:10000}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow:true}" />
        <property name="testOnReturn" value="${redis.testOnReturn:true}" />
    </bean>

    <!-- Redis集群配置     這里使用的是spring-data-redis  包中內(nèi)容 -->
     <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="maxRedirects" value="6"></property>
        <property name="clusterNodes">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="192.168.1.105"></constructor-arg>
                    <constructor-arg name="port" value="7111"></constructor-arg>
                </bean>

                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="192.168.1.105"></constructor-arg>
                    <constructor-arg name="port" value="7112"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="192.168.1.105"></constructor-arg>
                    <constructor-arg name="port" value="7116"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="192.168.1.102"></constructor-arg>
                    <constructor-arg name="port" value="7113"></constructor-arg>
                </bean>
                 <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="192.168.1.102"></constructor-arg>
                    <constructor-arg name="port" value="7114"></constructor-arg>
                </bean>
                 <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="192.168.1.102"></constructor-arg>
                    <constructor-arg name="port" value="7115"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>
    <!-- Redis連接工廠     -->
    <bean id="redis4CacheConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="clusterConfig" ref="redisClusterConfig" />
        <property name="timeout" value="${redis.timeout:10000}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>
    <!-- 存儲序列化 -->
    <bean name="stringRedisSerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />

    <!-- 集群Resis使用模板 -->
    <bean id="clusterRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redis4CacheConnectionFactory" />
        <property name="keySerializer" ref="stringRedisSerializer" />
        <property name="hashKeySerializer" ref="stringRedisSerializer" />
        <property name="valueSerializer" ref="stringRedisSerializer" />
        <property name="hashValueSerializer" ref="stringRedisSerializer" />
    </bean>

</beans>


3.再ApplicationContent.xml 配置文件中 導(dǎo)入以上配置:

<!-- 導(dǎo)入rediscluster配置文件 -->
    <import resource="classpath:spring-data-redis-cluster.xml" />


4.redisTemplate  的應(yīng)用:

自定義一個(gè)redisClient類來管理操作 redis 的存取操作:我定義的是:RedisClusterClient.java 類,內(nèi)容如下:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class RedisClusterClient {

        // 由于在Spring ApplicationContext.xml 配置文件中導(dǎo)入了 redis的配置文件,也就間接的將 <bean id="clusterRedisTemplate"                                                                  class="org.springframework.data.redis.core.RedisTemplate">  這個(gè)Bean托管給了Spring bean容器來管理所以 只要我使用注解就可以把這個(gè)模板類對象引用過來。

       @Autowired
       private RedisTemplate<String,String> clusterRedisTemplate;
    
       //添加數(shù)據(jù)
        public void put(Object key, Object value) {
            if(null == value) {
                return;
            }
    
            if(value instanceof String) {
                if(StringUtils.isEmpty(value.toString())) {
                    return;
                }
            }
    
            // TODO Auto-generated method stub
            final String keyf = key + "";
            final Object valuef = value;
            final long liveTime = 86400;
    
            clusterRedisTemplate.execute(new RedisCallback<Long>() {
                public Long doInRedis(RedisConnection connection)
                        throws DataAccessException {
                    byte[] keyb = keyf.getBytes();
                    byte[] valueb = toByteArray(valuef);
                    connection.set(keyb, valueb);
                    if (liveTime > 0) {
                        connection.expire(keyb, liveTime);
                    }
                    return 1L;
                }
            });
        }
    

         // 獲取數(shù)據(jù)

        public Object get(Object key) {
            final String keyf = (String) key;
            Object object;
            object = clusterRedisTemplate.execute(new RedisCallback<Object>() {
                public Object doInRedis(RedisConnection connection)
                        throws DataAccessException {
    
                    byte[] key = keyf.getBytes();
                    byte[] value = connection.get(key);
                    if (value == null) {
                        return null;
                    }
                    return toObject(value);
    
                }
            });
    
            return object;
        }
    
        /**
         * 描述 : <byte[]轉(zhuǎn)Object>. <br>
         * <p>
         * <使用方法說明>
         * </p>
         *
         * @param bytes
         * @return
         */
        private Object toObject(byte[] bytes) {
            Object obj = null;
            try {
                ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bis);
                obj = ois.readObject();
                ois.close();
                bis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            return obj;
        }
    
        private byte[] toByteArray(Object obj) {
            byte[] bytes = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                ObjectOutputStream oos = new ObjectOutputStream(bos);
                oos.writeObject(obj);
                oos.flush();
                bytes = bos.toByteArray();
                oos.close();
                bos.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return bytes;
        }
    
}


5.以上配置+實(shí)現(xiàn)。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
(四)spring+servlet 整合
Spring+Redis(keyspace notification)實(shí)現(xiàn)定時(shí)任務(wù)(訂單過期自動(dòng)關(guān)閉)
spring memcache
spring security3 自定義過濾鏈
Spring構(gòu)造函數(shù)注入
Spring極速集成注解redis實(shí)踐
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服