国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

redisson-spring-boot-starter 自動化配置源碼解析

這篇具有很好參考價值的文章主要介紹了redisson-spring-boot-starter 自動化配置源碼解析。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

版本

redisson-spring-boot-starter:3.25.2

此starter會自動注冊RedissonClient Bean
并可通過注冊RedissonAutoConfigurationCustomizer Bean實現(xiàn)配置自定義

 @Bean
 RedissonAutoConfigurationCustomizer jdkCodecCustomizer() {
     return configuration->{
         // 使用JDK序列化器
         configuration.setCodec(new SerializationCodec());
     };
 }

源碼

spring-boot:2.7以上
org.redisson.spring.starter.RedissonAutoConfigurationV2

@AutoConfiguration(before = RedisAutoConfiguration.class)
@ConditionalOnClass({Redisson.class, RedisOperations.class})
@EnableConfigurationProperties({RedissonProperties.class, RedisProperties.class}) // 啟用spring.data.redis,spring.redisson配置屬性
public class RedissonAutoConfigurationV2 extends RedissonAutoConfiguration {
}

spring-boot:2.6以下
org.redisson.spring.starter.RedissonAutoConfiguration文章來源地址http://www.zghlxwxcb.cn/news/detail-797395.html

@Configuration
@ConditionalOnClass({Redisson.class, RedisOperations.class})
// 防止spring-boot:2.7以上版本時跟RedissonAutoConfigurationV2沖突
@ConditionalOnMissingClass("org.springframework.boot.autoconfigure.AutoConfiguration") 
@AutoConfigureBefore(RedisAutoConfiguration.class)
@EnableConfigurationProperties({RedissonProperties.class, RedisProperties.class})
public class RedissonAutoConfiguration {
	...
	// 注冊RedissonClient 
	@Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean(RedissonClient.class)
    public RedissonClient redisson() throws IOException {
        Config config;
        Method clusterMethod = ReflectionUtils.findMethod(RedisProperties.class, "getCluster");
        Method usernameMethod = ReflectionUtils.findMethod(RedisProperties.class, "getUsername");
        Method timeoutMethod = ReflectionUtils.findMethod(RedisProperties.class, "getTimeout");
        Method connectTimeoutMethod = ReflectionUtils.findMethod(RedisProperties.class, "getConnectTimeout");
        Method clientNameMethod = ReflectionUtils.findMethod(RedisProperties.class, "getClientName");

        Object timeoutValue = ReflectionUtils.invokeMethod(timeoutMethod, redisProperties);
        String prefix = getPrefix();

        String username = null;
        int database = redisProperties.getDatabase();
        String password = redisProperties.getPassword();
        boolean isSentinel = false;
        boolean isCluster = false;
        // 如果存在redis連接詳細配置,則從詳細配置中獲取用戶名,密碼,哨兵模式標(biāo)識,集群模式標(biāo)識
        if (hasConnectionDetails()) {
            ObjectProvider<RedisConnectionDetails> provider = ctx.getBeanProvider(RedisConnectionDetails.class);
            RedisConnectionDetails b = provider.getIfAvailable();
            if (b != null) {
                password = b.getPassword();
                username = b.getUsername();

                if (b.getSentinel() != null) {
                    isSentinel = true;
                }
                if (b.getCluster() != null) {
                    isCluster = true;
                }
            }
        }
		// 獲取redis配置中的超時時間
        Integer timeout = null;
        if (timeoutValue instanceof Duration) {
            timeout = (int) ((Duration) timeoutValue).toMillis();
        } else if (timeoutValue != null){
            timeout = (Integer)timeoutValue;
        }
		// 獲取redis配置中的連接超時時間
        Integer connectTimeout = null;
        if (connectTimeoutMethod != null) {
            Object connectTimeoutValue = ReflectionUtils.invokeMethod(connectTimeoutMethod, redisProperties);
            if (connectTimeoutValue != null) {
                connectTimeout = (int) ((Duration) connectTimeoutValue).toMillis();
            }
        } else {
            connectTimeout = timeout;
        }
		// 獲取redis配置中的客戶端名稱
        String clientName = null;
        if (clientNameMethod != null) {
            clientName = (String) ReflectionUtils.invokeMethod(clientNameMethod, redisProperties);
        }
		// 獲取redis配置中的用戶名
        if (usernameMethod != null) {
            username = (String) ReflectionUtils.invokeMethod(usernameMethod, redisProperties);
        }
		if (redissonProperties.getConfig() != null) {
            // 如果存在redisson配置(在application.yml中以字符串形式配置)
        	try {
            	// 嘗試解析yml格式配置字符串
                config = Config.fromYAML(redissonProperties.getConfig());
            } catch (IOException e) {
                try {
                	// 嘗試解析json格式配置字符串
                    config = Config.fromJSON(redissonProperties.getConfig());
                } catch (IOException e1) {
                    e1.addSuppressed(e);
                    throw new IllegalArgumentException("Can't parse config", e1);
                }
            }
        } else if (redissonProperties.getFile() != null) {
        	// 如果存在redisson配置文件(yml或json)
            try {
                InputStream is = getConfigStream();
                config = Config.fromYAML(is);
            } catch (IOException e) {
                // trying next format
                try {
                    InputStream is = getConfigStream();
                    config = Config.fromJSON(is);
                } catch (IOException e1) {
                    e1.addSuppressed(e);
                    throw new IllegalArgumentException("Can't parse config", e1);
                }
            }
        } else if (redisProperties.getSentinel() != null || isSentinel) {
        	// 連接哨兵模式集群
            String[] nodes = {};
            String sentinelMaster = null;

            if (redisProperties.getSentinel() != null) {
                Method nodesMethod = ReflectionUtils.findMethod(Sentinel.class, "getNodes");
                Object nodesValue = ReflectionUtils.invokeMethod(nodesMethod, redisProperties.getSentinel());
                if (nodesValue instanceof String) {
                    nodes = convert(prefix, Arrays.asList(((String)nodesValue).split(",")));
                } else {
                    nodes = convert(prefix, (List<String>)nodesValue);
                }
                sentinelMaster = redisProperties.getSentinel().getMaster();
            }


            String sentinelUsername = null;
            String sentinelPassword = null;
            if (hasConnectionDetails()) {
                ObjectProvider<RedisConnectionDetails> provider = ctx.getBeanProvider(RedisConnectionDetails.class);
                RedisConnectionDetails b = provider.getIfAvailable();
                if (b != null && b.getSentinel() != null) {
                    database = b.getSentinel().getDatabase();
                    sentinelMaster = b.getSentinel().getMaster();
                    nodes = convertNodes(prefix, (List<Object>) (Object) b.getSentinel().getNodes());
                    sentinelUsername = b.getSentinel().getUsername();
                    sentinelPassword = b.getSentinel().getPassword();
                }
            }

            config = new Config();
            SentinelServersConfig c = config.useSentinelServers()
                    .setMasterName(sentinelMaster)
                    .addSentinelAddress(nodes)
                    .setSentinelPassword(sentinelPassword)
                    .setSentinelUsername(sentinelUsername)
                    .setDatabase(database)
                    .setUsername(username)
                    .setPassword(password)
                    .setClientName(clientName);
            if (connectTimeout != null) {
                c.setConnectTimeout(connectTimeout);
            }
            if (connectTimeoutMethod != null && timeout != null) {
                c.setTimeout(timeout);
            }
            initSSL(c);
        } else if ((clusterMethod != null && ReflectionUtils.invokeMethod(clusterMethod, redisProperties) != null)
                    || isCluster) {
			// 連接分片集群
            String[] nodes = {};
            if (clusterMethod != null && ReflectionUtils.invokeMethod(clusterMethod, redisProperties) != null) {
                Object clusterObject = ReflectionUtils.invokeMethod(clusterMethod, redisProperties);
                Method nodesMethod = ReflectionUtils.findMethod(clusterObject.getClass(), "getNodes");
                List<String> nodesObject = (List) ReflectionUtils.invokeMethod(nodesMethod, clusterObject);

                nodes = convert(prefix, nodesObject);
            }

            if (hasConnectionDetails()) {
                ObjectProvider<RedisConnectionDetails> provider = ctx.getBeanProvider(RedisConnectionDetails.class);
                RedisConnectionDetails b = provider.getIfAvailable();
                if (b != null && b.getCluster() != null) {
                    nodes = convertNodes(prefix, (List<Object>) (Object) b.getCluster().getNodes());
                }
            }

            config = new Config();
            ClusterServersConfig c = config.useClusterServers()
                    .addNodeAddress(nodes)
                    .setUsername(username)
                    .setPassword(password)
                    .setClientName(clientName);
            if (connectTimeout != null) {
                c.setConnectTimeout(connectTimeout);
            }
            if (connectTimeoutMethod != null && timeout != null) {
                c.setTimeout(timeout);
            }
            initSSL(c);
        } else {
        	// 連接單實例
            config = new Config();

            String singleAddr = prefix + redisProperties.getHost() + ":" + redisProperties.getPort();

            if (hasConnectionDetails()) {
                ObjectProvider<RedisConnectionDetails> provider = ctx.getBeanProvider(RedisConnectionDetails.class);
                RedisConnectionDetails b = provider.getIfAvailable();
                if (b != null && b.getStandalone() != null) {
                    database = b.getStandalone().getDatabase();
                    singleAddr = prefix + b.getStandalone().getHost() + ":" + b.getStandalone().getPort();
                }
            }

            SingleServerConfig c = config.useSingleServer()
                    .setAddress(singleAddr)
                    .setDatabase(database)
                    .setUsername(username)
                    .setPassword(password)
                    .setClientName(clientName);
            if (connectTimeout != null) {
                c.setConnectTimeout(connectTimeout);
            }
            if (connectTimeoutMethod != null && timeout != null) {
                c.setTimeout(timeout);
            }
            initSSL(c);
        }
        // 應(yīng)用自定義配置bean
        if (redissonAutoConfigurationCustomizers != null) {
            for (RedissonAutoConfigurationCustomizer customizer : redissonAutoConfigurationCustomizers) {
                customizer.customize(config);
            }
        }
        return Redisson.create(config);
    }

    private void initSSL(BaseConfig<?> config) {
        Method getSSLMethod = ReflectionUtils.findMethod(RedisProperties.class, "getSsl");
        if (getSSLMethod == null) {
            return;
        }

        RedisProperties.Ssl ssl = redisProperties.getSsl();
        if (ssl.getBundle() == null) {
            return;
        }

        ObjectProvider<SslBundles> provider = ctx.getBeanProvider(SslBundles.class);
        SslBundles bundles = provider.getIfAvailable();
        if (bundles == null) {
            return;
        }
        SslBundle b = bundles.getBundle(ssl.getBundle());
        if (b == null) {
            return;
        }
        config.setSslCiphers(b.getOptions().getCiphers());
        config.setSslProtocols(b.getOptions().getEnabledProtocols());
        config.setSslTrustManagerFactory(b.getManagers().getTrustManagerFactory());
        config.setSslKeyManagerFactory(b.getManagers().getKeyManagerFactory());
    }
}

到了這里,關(guān)于redisson-spring-boot-starter 自動化配置源碼解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【Spring Boot 3】【Redis】集成Redisson

    軟件開發(fā)是一門實踐性科學(xué),對大多數(shù)人來說,學(xué)習(xí)一種新技術(shù)不是一開始就去深究其原理,而是先從做出一個可工作的DEMO入手。但在我個人學(xué)習(xí)和工作經(jīng)歷中,每次學(xué)習(xí)新技術(shù)總是要花費或多或少的時間、檢索不止一篇資料才能得出一個可工作的DEMO,這占用了我大量的時

    2024年01月23日
    瀏覽(94)
  • 自定義Spring Boot Starter

    自定義Spring Boot Starter

    Spring Boot starter 我們知道Spring Boot大大簡化了項目初始搭建以及開發(fā)過程,而這些都是通過Spring Boot提供的starter來完成的。在實際項目中一些基礎(chǔ)模塊其本質(zhì)就是starter,所以我們需要對Spring Boot的starter有一個全面深入的了解,這是我們的必備知識。 starter介紹 spring boot 在配置

    2024年02月10日
    瀏覽(29)
  • 6. Spring Boot的starters

    6. Spring Boot的starters

    6. Spring Boot的starters(重要) 一般認(rèn)為,SpringBoot 微框架從兩個主要層面影響 Spring 社區(qū)的開發(fā)者們: 基于 Spring 框架的“約定優(yōu)先于配置(COC)”理念以及最佳實踐之路。 提供了針對日常企業(yè)應(yīng)用研發(fā)各種場景的 spring-boot-starter 自動配置依賴模塊,如此多“開箱即用”的依賴模

    2024年01月24日
    瀏覽(28)
  • Spring Boot Starter設(shè)計實現(xiàn)

    Starter 是 Spring Boot 非常重要的一個硬核功能。 通過 Starter 我們可以快速的引入一個功能或模塊,而無須關(guān)心模塊依賴的其它組件。關(guān)于配置,Spring Boot 采用“約定大于配置”的設(shè)計理念,Starter 一般都會提供默認(rèn)配置,只有當(dāng)我們有特殊需求的時候,才需要在 application.yaml 里

    2024年01月18日
    瀏覽(19)
  • Spring Boot Starter Parent

    Spring Boot Starter Parent

    在這,您將學(xué)習(xí)了解 Spring Boot Starter Parent, 它是 Spring Boot 提供的父級 Pom 文件,旨在提供自動版本依賴管理,幫助我們輕松快速地進行?Spring Boot?開發(fā)。 通過 Spring Boot Starter Parent, 我們可以進行簡單便捷地包依賴管理。在 Spring Boot 每一個發(fā)行版中, 均提供了該版本所兼容的依

    2024年02月08日
    瀏覽(29)
  • Spring Boot 集成 Redisson分布式鎖

    Spring Boot 集成 Redisson分布式鎖

    ????????Redisson 是一種基于 Redis 的 Java 駐留集群的分布式對象和服務(wù)庫,可以為我們提供豐富的分布式鎖和線程安全集合的實現(xiàn)。在 Spring Boot 應(yīng)用程序中使用 Redisson 可以方便地實現(xiàn)分布式應(yīng)用程序的某些方面,例如分布式鎖、分布式集合、分布式事件發(fā)布和訂閱等。本篇

    2024年02月10日
    瀏覽(29)
  • shiro-spring-boot-starter針對不同Spring Boot版本

    對于Spring Boot 2.4.10,無法找到shiro-spring-boot-starter的2.7.2版本,這是一個錯誤的版本號。 shiro-spring-boot-starter針對不同Spring Boot版本,推薦使用的版本如下: Spring Boot 1.x - 使用版本1.4.1 Spring Boot 2.0.x - 使用版本1.5.3 Spring Boot 2.1.x - 使用版本1.6.0 Spring Boot 2.2.x - 使用版本1.7.0 Spring Boot 2.3

    2024年02月13日
    瀏覽(17)
  • Spring Boot Starter 剖析與實踐

    對于 Java 開發(fā)人員來說,Spring 框架幾乎是必不可少的。它是一個廣泛用于開發(fā)企業(yè)應(yīng)用程序的開源輕量級框架。近幾年,Spring Boot 在傳統(tǒng) Spring 框架的基礎(chǔ)上應(yīng)運而生,不僅提供了 Spring 的全部功能,還使開發(fā)人員更加便捷地使用。在使用 Spring Boot 時,我們經(jīng)常會接觸到各種

    2024年02月14日
    瀏覽(24)
  • 自定義 Spring Boot Starter 組件

    自定義 Spring Boot Starter 組件是為了封裝和簡化特定功能的配置和集成,讓用戶能夠更容易地集成你提供的庫或功能。Spring Boot Starter 組件通常包括自動配置、依賴管理和必要的配置。 下面是創(chuàng)建一個簡單的 Spring Boot Starter 的基本步驟: 步驟: 創(chuàng)建一個新的 Maven 或 Gradle 項目

    2024年02月05日
    瀏覽(35)
  • Spring Boot Starter介紹和實戰(zhàn)

    Spring Boot Starter 是 Spring Boot 提供的一種機制,用于簡化和集成應(yīng)用程序的依賴管理。通過創(chuàng)建自定義的 Starter,可以將一組相關(guān)的依賴打包成一個簡單的、可重用的模塊,使應(yīng)用程序的配置和依賴管理更加方便。在本文中,我們將深入探討 Spring Boot Starter 的原理、創(chuàng)建過程,

    2024年01月23日
    瀏覽(34)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包