欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解簡(jiǎn)單基于spring的redis配置(單機(jī)和集群模式)

 更新時(shí)間:2019年02月20日 09:56:05   作者:muistar  
這篇文章主要介紹了詳解簡(jiǎn)單基于spring的redis配置(單機(jī)和集群模式),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

需要的jar包:spring版本:4.3.6.RELEASE,jedis版本:2.9.0,spring-data-redis:1.8.0.RELEASE;如果使用jackson序列化的話還額外需要:jackson-annotations和jackson-databind包

spring集成redis單機(jī)版:

1.配置RedisTemplate

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  <property name="connectionFactory" ref="connectionFactory"/>
   <property name="defaultSerializer" ref="stringRedisSerializer"/>
   <property name="keySerializer" ref="stringRedisSerializer"/>
   <property name="valueSerializer" ref="valueSerializer"/>
</bean>

2.配置connectionFactory

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
      <!-- 配置ip -->
      <property name="hostName" value="${redis.host}"/>
      <!-- 配置port -->
      <property name="port" value="${redis.port}"/>
      <!-- 是否使用連接池-->
      <property name="usePool" value="${redis.usePool}"/>
      <!-- 配置redis連接池-->
      <property name="poolConfig" ref="poolConfig"/>
    </bean>

3.配置連接池

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
      <!--最大空閑實(shí)例數(shù)-->
      <property name="maxIdle" value="${redis.maxIdle}" />
      <!--最大活躍實(shí)例數(shù)-->
      <property name="maxTotal" value="${redis.maxTotal}" />
      <!--創(chuàng)建實(shí)例時(shí)最長(zhǎng)等待時(shí)間-->
      <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
      <!--創(chuàng)建實(shí)例時(shí)是否驗(yàn)證-->
      <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

spring集成redis集群

1.配置RedisTemplate步驟與單機(jī)版一致

2.配置connectionFactory

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
      <!-- 配置redis連接池-->  
      <constructor-arg ref="poolConfig"></constructor-arg>
      <!-- 配置redis集群--> 
     <constructor-arg ref="clusterConfig"></constructor-arg>
      <!-- 是否使用連接池-->
      <property name="usePool" value="${redis.usePool}"/>
    </bean>

3.配置連接池步驟與單機(jī)版一致

4.配置redis集群

    <bean id="clusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
      <property name="maxRedirects" value="3"></property>
      <property name="clusterNodes">
        <set>
          <bean class="org.springframework.data.redis.connection.RedisClusterNode">
            <constructor-arg value="${redis.host1}"></constructor-arg>
            <constructor-arg value="${redis.port1}"></constructor-arg>
          </bean>
          <bean class="org.springframework.data.redis.connection.RedisClusterNode">
            <constructor-arg value="${redis.host2}"></constructor-arg>
            <constructor-arg value="${redis.port2}"></constructor-arg>
          </bean>
          ......
        </set>
      </property>
    </bean

或者

    <bean name="propertySource" class="org.springframework.core.io.support.ResourcePropertySource">
      <constructor-arg name="location" value="classpath:properties/spring-redis-cluster.properties" />
    </bean>
    <bean id="clusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
      <constructor-arg name="propertySource" ref="propertySource"/>
    </bean>

序列化配置簡(jiǎn)述:

1.stringRedisSerializer:由于redis的key是String類(lèi)型所以一般使用StringRedisSerializer

2.valueSerializer:對(duì)于redis的value序列化,spring-data-redis提供了許多序列化類(lèi),這里建議使用Jackson2JsonRedisSerializer,默認(rèn)為JdkSerializationRedisSerializer

3.JdkSerializationRedisSerializer: 使用JDK提供的序列化功能。 優(yōu)點(diǎn)是反序列化時(shí)不需要提供類(lèi)型信息(class),但缺點(diǎn)是序列化后的結(jié)果非常龐大,是JSON格式的5倍左右,這樣就會(huì)消耗redis服務(wù)器的大量?jī)?nèi)存。

4.Jackson2JsonRedisSerializer:使用Jackson庫(kù)將對(duì)象序列化為JSON字符串。優(yōu)點(diǎn)是速度快,序列化后的字符串短小精悍。但缺點(diǎn)也非常致命,那就是此類(lèi)的構(gòu)造函數(shù)中有一個(gè)類(lèi)型參數(shù),必須提供要序列化對(duì)象的類(lèi)型信息(.class對(duì)象)。

使用spring注解式來(lái)配置redis,這里只配置集群樣例

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

  //spring3支持注解方式獲取value 在application里配置配置文件路徑即可獲取
  @Value("${spring.redis.cluster.nodes}")
  private String clusterNodes;

  @Value("${spring.redis.cluster.timeout}")
  private Long timeout;

  @Value("${spring.redis.cluster.max-redirects}")
  private int redirects;

  @Value("${redis.maxIdle}")
  private int maxIdle;

  @Value("${redis.maxTotal}")
  private int maxTotal;

  @Value("${redis.maxWaitMillis}")
  private long maxWaitMillis;

  @Value("${redis.testOnBorrow}")
  private boolean testOnBorrow;

  /**
   * 選擇redis作為默認(rèn)緩存工具
   * @param redisTemplate
   * @return
   */
  @Bean
  public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
    //cacheManager.setDefaultExpiration(60);
    //Map<String,Long> expiresMap=new HashMap<>();
    //expiresMap.put("redisCache",5L);
    //cacheManager.setExpires(expiresMap);
    return cacheManager;
  }

  @Bean
  public RedisClusterConfiguration redisClusterConfiguration(){
    Map<String, Object> source = new HashMap<>();
    source.put("spring.redis.cluster.nodes", clusterNodes);
    source.put("spring.redis.cluster.timeout", timeout);
    source.put("spring.redis.cluster.max-redirects", redirects);
    return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
  }

  @Bean
  public JedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration configuration){
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMaxTotal(maxTotal); 
    poolConfig.setMaxWaitMillis(maxWaitMillis);
    poolConfig.setTestOnBorrow(testOnBorrow);
    return new JedisConnectionFactory(configuration,poolConfig);
  }

  /**
   * retemplate相關(guān)配置
   * @param factory
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory) {

    RedisTemplate<String, Object> template = new RedisTemplate<>();
    // 配置連接工廠
    template.setConnectionFactory(factory);

    //使用Jackson2JsonRedisSerializer來(lái)序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
    Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);

    ObjectMapper om = new ObjectMapper();
    // 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    // 指定序列化輸入的類(lèi)型,類(lèi)必須是非final修飾的,final修飾的類(lèi),比如String,Integer等會(huì)跑出異常
    //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jacksonSeial.setObjectMapper(om);

    // 值采用json序列化
    template.setValueSerializer(jacksonSeial);
    //使用StringRedisSerializer來(lái)序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());

    // 設(shè)置hash key 和value序列化模式
    template.setHashKeySerializer(new StringRedisSerializer());
  
    template.setHashValueSerializer(jacksonSeial);
    template.afterPropertiesSet();

    return template;
  }
}

注意事項(xiàng):

1.采用注解式配置redis或者使用@Configuration需要在配置文件中指定掃描什么哪些包下的配置文件,當(dāng)然如果是springboot那當(dāng)我沒(méi)說(shuō)過(guò)這句話...

<context:component-scan base-package="com.*"/>

2.spring3支持注解方式獲取Value,但是需要在加載的配置文件配置文件路徑即可,具體值自己指定吧...

<value>classpath:properties/spring-redis-cluster.properties</value>

3.由于我們公司原有的框架采用的是spring2.5.6, 而對(duì)于spring2 和spring2+主要區(qū)別(當(dāng)然是我自己覺(jué)得?。┦前迅髂K分成了不同的jar,而對(duì)于使用spring-data-redis模板化處理redis的話,單機(jī)情況下spring2.5.6和spring4不會(huì)沖突,而如果使用集群模式需要配置redis集群的時(shí)候就會(huì)出現(xiàn)jar包沖突,這個(gè)時(shí)候就看要如何取決了,可以直接使用jedisCluster來(lái)連接redis集群(不過(guò)很多方法都需要自己去寫(xiě)),也可以把spring2.5.6替換成高版本的spring4,只是框架替換需要注意的事情更多了啊(我們公司的直接全部替換沒(méi)啥毛病好吧,O(∩_∩)O哈哈~),至于重寫(xiě)JedisConnectionFactory和RedisClusterConfiguration我還未去嘗試,這個(gè)可以作為后續(xù)補(bǔ)充吧...

4.順便說(shuō)句,spring4不在支持ibatis了,如果你需要用spring4,又需要連接ibatis的話,最粗暴的方式是把spring-orm包換成spring3版本,其他的jar還是4版本即可。(當(dāng)然我這邊直接替換是沒(méi)啥問(wèn)題,但同3一樣可能存在潛在問(wèn)題啊,所以說(shuō)嘛,公司有時(shí)候還是需要更新?lián)Q代下吧...)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot配置Access-Control-Allow-Origin教程

    SpringBoot配置Access-Control-Allow-Origin教程

    文章介紹了三種配置Spring Boot跨域訪問(wèn)的方法:1. 使用過(guò)濾器;2. 在WebConfig配置文件中設(shè)置;3. 通過(guò)注解配置,作者分享了個(gè)人經(jīng)驗(yàn),并鼓勵(lì)讀者支持腳本之家
    2025-03-03
  • Java使用Gateway自定義負(fù)載均衡過(guò)濾器

    Java使用Gateway自定義負(fù)載均衡過(guò)濾器

    這篇文章主要介紹了Java使用Gateway自定義負(fù)載均衡過(guò)濾器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • JAVA十大排序算法之基數(shù)排序詳解

    JAVA十大排序算法之基數(shù)排序詳解

    這篇文章主要介紹了java中的基數(shù)排序,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • springboot添加https服務(wù)器的方法

    springboot添加https服務(wù)器的方法

    這篇文章主要介紹了springboot添加https服務(wù)器的方法,小編在android開(kāi)發(fā)中發(fā)現(xiàn)很多app都是https訪問(wèn)的,為了方便測(cè)試,自己搭建了一個(gè)https服務(wù)器,需要的朋友可以參考下
    2017-11-11
  • springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3)

    springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3)

    這篇文章主要介紹了springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • spring?cache注解@Cacheable緩存穿透詳解

    spring?cache注解@Cacheable緩存穿透詳解

    這篇文章主要介紹了spring?cache注解@Cacheable緩存穿透詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Maven項(xiàng)目引用第三方j(luò)ar包找不到類(lèi)ClassNotFoundException

    Maven項(xiàng)目引用第三方j(luò)ar包找不到類(lèi)ClassNotFoundException

    這篇文章主要為大家介紹了Maven項(xiàng)目引用第三方j(luò)ar包找不到類(lèi)ClassNotFoundException解決及原因分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Java線程池由淺入深掌握到精通

    Java線程池由淺入深掌握到精通

    什么是線程池?很簡(jiǎn)單,簡(jiǎn)單看名字就知道是裝有線程的池子,我們可以把要執(zhí)行的多線程交給線程池來(lái)處理,和連接池的概念一樣,通過(guò)維護(hù)一定數(shù)量的線程池來(lái)達(dá)到多個(gè)線程的復(fù)用
    2021-09-09
  • Java用Cookie限制點(diǎn)贊次數(shù)(簡(jiǎn)版)

    Java用Cookie限制點(diǎn)贊次數(shù)(簡(jiǎn)版)

    最近做了一個(gè)項(xiàng)目,其中有項(xiàng)目需求是,要用cookie實(shí)現(xiàn)限制點(diǎn)贊次數(shù),特此整理,把實(shí)現(xiàn)代碼分享給大家供大家學(xué)習(xí)
    2016-02-02
  • idea 開(kāi)發(fā)神器之idea插件匯總

    idea 開(kāi)發(fā)神器之idea插件匯總

    這篇文章主要介紹了idea 開(kāi)發(fā)神器之idea插件匯總,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論