最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot redis使用lettuce配置多數(shù)據(jù)源的實(shí)現(xiàn)

 更新時(shí)間:2021年04月29日 16:27:08   作者:cccccloud.com  
這篇文章主要介紹了springboot redis使用lettuce配置多數(shù)據(jù)源的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

目前項(xiàng)目上需要連接兩個(gè)redis數(shù)據(jù)源,一個(gè)redis數(shù)據(jù)源是單機(jī)模式,一個(gè)redis數(shù)據(jù)源是分片集群模式,這里將具體配置列一下。

項(xiàng)目用的springboot版本為

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

一、在yml中配置redis數(shù)據(jù)源信息

redis:
    cluster:
      nodes: 127.0.0.1:9001
    lettuce:
      #連接池配置
      pool:
        #連接池最大連接數(shù)
        max-active: 20
        #連接池最大等待時(shí)間,負(fù)數(shù)表示不做限制
        max-wait: -1
        #最大空閑連接
        max-idle: 9
        #最小空閑連接
        min-idle: 0
    timeout: 500000
  redis2:
    host: 127.0.0.1
    port: 6385
    lettuce:
      pool:
        max-active: 20
        max-idle: 8
        max-wait: -1
        min-idle: 0
    timeout: 500000

(這里的redis都沒(méi)有配置密碼)

二、添加redis配置類

package com.cq.config;
 
import cn.hutool.core.convert.Convert;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author cccccloud on 2020/11/16 17:16
 */
@Configuration
public class RedisConfig {
 
    @Autowired
    private Environment environment;
 
    @Value("${spring.redis2.host}")
    private String host;
    @Value("${spring.redis2.port}")
    private String port;
    @Value("${spring.redis2.lettuce.pool.max-active}")
    private String max_active;
    @Value("${spring.redis2.lettuce.pool.max-idle}")
    private String max_idle;
    @Value("${spring.redis2.lettuce.pool.max-wait}")
    private String max_wait;
    @Value("${spring.redis2.lettuce.pool.min-idle}")
    private String min_idle;
 
    /**
     * 配置lettuce連接池
     *
     * @return
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.redis.cluster.lettuce.pool")
    public GenericObjectPoolConfig redisPool() {
        return new GenericObjectPoolConfig();
    }
 
    /**
     * 配置第一個(gè)數(shù)據(jù)源的
     *
     * @return
     */
    @Bean("redisClusterConfig")
    @Primary
    public RedisClusterConfiguration redisClusterConfig() {
 
        Map<String, Object> source = new HashMap<>(8);
        source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes"));
        RedisClusterConfiguration redisClusterConfiguration;
        redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
        redisClusterConfiguration.setPassword(environment.getProperty("spring.redis.password"));
        return redisClusterConfiguration;
 
    }
 
    /**
     * 配置第一個(gè)數(shù)據(jù)源的連接工廠
     * 這里注意:需要添加@Primary 指定bean的名稱,目的是為了創(chuàng)建兩個(gè)不同名稱的LettuceConnectionFactory
     *
     * @param redisPool
     * @param redisClusterConfig
     * @return
     */
    @Bean("lettuceConnectionFactory")
    @Primary
    public LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig redisPool, @Qualifier("redisClusterConfig") RedisClusterConfiguration redisClusterConfig) {
        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).build();
        return new LettuceConnectionFactory(redisClusterConfig, clientConfiguration);
    }
 
    /**
     * 配置第一個(gè)數(shù)據(jù)源的RedisTemplate
     * 注意:這里指定使用名稱=factory 的 RedisConnectionFactory
     * 并且標(biāo)識(shí)第一個(gè)數(shù)據(jù)源是默認(rèn)數(shù)據(jù)源 @Primary
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean("redisTemplate")
    @Primary
    public RedisTemplate redisTemplate(@Qualifier("lettuceConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
 
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(stringRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(stringRedisSerializer);
        template.afterPropertiesSet();
 
        return template;
    }
 
    @Bean
    public GenericObjectPoolConfig redisPool2() {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMinIdle(Convert.toInt(min_idle));
        config.setMaxIdle(Convert.toInt(max_idle));
        config.setMaxTotal(Convert.toInt(max_active));
        config.setMaxWaitMillis(Convert.toInt(max_wait));
        return config;
    }
 
    @Bean
    public RedisStandaloneConfiguration redisConfig2() {
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(host,Convert.toInt(port));
        return redisConfig;
    }
 
    @Bean("factory2")
    public LettuceConnectionFactory factory2(@Qualifier("redisPool2") GenericObjectPoolConfig config,
                                             @Qualifier("redisConfig2") RedisStandaloneConfiguration redisConfig) {//注意傳入的對(duì)象名和類型RedisStandaloneConfiguration
        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
        return new LettuceConnectionFactory(redisConfig, clientConfiguration);
    }
 
 
    /**
     * 單實(shí)例redis數(shù)據(jù)源
     *
     * @param connectionFactory
     * @return
     */
    @Bean("redisTemplateSingle")
    public RedisTemplate<String, Object> redisTemplateSingle(@Qualifier("factory2")LettuceConnectionFactory connectionFactory) {//注意傳入的對(duì)象名
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
 
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setValueSerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        redisTemplate.setHashValueSerializer(redisSerializer);
        return redisTemplate;
    }
}

三、使用redis

使用單實(shí)例redis

    /**
     * redis 單節(jié)點(diǎn)
     */
    @Resource(name = "redisTemplateSingle")
    private RedisTemplate redisTemplateSingle;

使用redis集群

    /**
     * redis 集群
     */
    @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;
 

到此這篇關(guān)于springboot redis使用lettuce配置多數(shù)據(jù)源的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot lettuce多數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java字符串編碼解碼性能提升的技巧分享

    Java字符串編碼解碼性能提升的技巧分享

    這篇文章主要是和大家分享幾個(gè)Java中提升字符串編碼解碼性能的小技巧,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,需要的可以參考一下
    2022-05-05
  • springboot使用校驗(yàn)框架validation校驗(yàn)的示例

    springboot使用校驗(yàn)框架validation校驗(yàn)的示例

    這篇文章主要介紹了springboot使用校驗(yàn)框架validation校驗(yàn)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Java優(yōu)雅的關(guān)閉線程池的方法

    Java優(yōu)雅的關(guān)閉線程池的方法

    本文主要介紹了Java如何優(yōu)雅的關(guān)閉線程池,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • java中l(wèi)ombok的@Data引發(fā)問(wèn)題詳解

    java中l(wèi)ombok的@Data引發(fā)問(wèn)題詳解

    這篇文章主要給大家介紹了關(guān)于java中l(wèi)ombok的@Data引發(fā)問(wèn)題的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • idea集成shell運(yùn)行環(huán)境以及shell輸出中文亂碼的解決

    idea集成shell運(yùn)行環(huán)境以及shell輸出中文亂碼的解決

    這篇文章主要介紹了idea集成shell運(yùn)行環(huán)境以及shell輸出中文亂碼的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 淺談 JDBC 元數(shù)據(jù)

    淺談 JDBC 元數(shù)據(jù)

    這篇文章主要介紹了JDBC元數(shù)據(jù)的相關(guān)內(nèi)容,涉及一些獲取數(shù)據(jù)源各種信息的方法,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-09-09
  • Java ClassCastException異常解決方案

    Java ClassCastException異常解決方案

    這篇文章主要介紹了Java ClassCastException異常解決方案,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解決方案

    IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解決

    原來(lái)win10電腦上安裝的是jdk8的版本,因某些原因,現(xiàn)在想換成jdk7的版本,修改環(huán)境變量后,在cmd中執(zhí)行 [java -version]命令,顯示的是7的版本,遇到這樣的問(wèn)題如何解決呢?下面小編給大家分享IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解決方案,一起看看吧
    2023-09-09
  • Spring?MVC請(qǐng)求轉(zhuǎn)發(fā)與請(qǐng)求重定向的示例詳解

    Spring?MVC請(qǐng)求轉(zhuǎn)發(fā)與請(qǐng)求重定向的示例詳解

    轉(zhuǎn)發(fā)指服務(wù)器接收請(qǐng)求后,從一個(gè)資源跳轉(zhuǎn)到另一個(gè)資源中,請(qǐng)求轉(zhuǎn)發(fā)是一次請(qǐng)求,不會(huì)改變?yōu)g覽器的請(qǐng)求地址,這篇文章主要介紹了Spring?MVC請(qǐng)求轉(zhuǎn)發(fā)與請(qǐng)求重定向的相關(guān)知識(shí),需要的朋友可以參考下
    2023-09-09
  • java 使用線程監(jiān)控文件目錄變化的實(shí)現(xiàn)方法

    java 使用線程監(jiān)控文件目錄變化的實(shí)現(xiàn)方法

    這篇文章主要介紹了java 使用線程監(jiān)控文件目錄變化的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-10-10

最新評(píng)論

宁德市| 永川市| 苗栗市| 怀来县| 南和县| 萝北县| 新绛县| 大名县| 上饶县| 论坛| 翁源县| 池州市| 乳山市| 彰化县| 九寨沟县| 章丘市| 河源市| 桐城市| 新竹市| 和林格尔县| 赤城县| 桐柏县| 湘阴县| 砚山县| 建瓯市| 葫芦岛市| 缙云县| 定州市| 集贤县| 田东县| 郧西县| 延庆县| 精河县| 沙洋县| 庆安县| 大姚县| 新竹县| 辽阳县| 名山县| 化德县| 广元市|