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

SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊(cè)中心流程詳解

 更新時(shí)間:2023年01月13日 16:05:50   作者:融極  
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊(cè)中心流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

概述

某些場景下只需要把springboot微服務(wù)化而不想引入springcloud如何實(shí)現(xiàn)的呢?

下面我們介紹nacos注冊(cè)中心方案。

接入nacos注冊(cè)中心

springboot服務(wù)pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.study</groupId>
        <artifactId>practice</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>practice-demo</artifactId>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <!-- springboot -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!--nacos注冊(cè)中心-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.3.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

application.properties配置

# nacos注冊(cè)
spring.application.name=file-server-service
nacos.discovery.server-addr=192.168.1.1:8848,192.168.1.2:8848
nacos.discovery.auto-register=true
nacos.discovery.enabled=true
# 指定微服務(wù)注冊(cè)那個(gè)地址
nacos.discovery.register.ip=

源碼分析

主流程:

1、nacos-discovery-spring-boot-starter 啟動(dòng)服務(wù)通過SPI機(jī)制掃描到nacos-discovery-spring-boot-autoconfigure包。

2、nacos-discovery-spring-boot-autoconfigure項(xiàng)目通過自動(dòng)裝配功能裝配nacos客戶端

3、 Nacos自動(dòng)配置服務(wù)實(shí)現(xiàn)Spring的應(yīng)用監(jiān)聽器用來注冊(cè)nacos服務(wù)

NacosDiscoveryAutoRegister implements ApplicationListener

4、NacosDiscoveryAutoRegister監(jiān)聽到spring的ServletWebServerInitializedEvent事件后把springboot服務(wù)注冊(cè)到nacos注冊(cè)中心

5、調(diào)用nacos-client jar包中的com.alibaba.nacos.client.naming.net.NamingProxy#registerService完成服務(wù)注冊(cè)

核心處理邏輯:com.alibaba.nacos.client.naming.net.NamingProxy.java

    public String reqAPI(String api, Map<String, String> params, String body, List<String> servers, String method) throws NacosException {
        params.put("namespaceId", this.getNamespaceId());
        if (CollectionUtils.isEmpty(servers) && StringUtils.isEmpty(this.nacosDomain)) {
            throw new NacosException(400, "no server available");
        } else {
            NacosException exception = new NacosException();
            // 如果nacos.discovery.server-addr是逗號(hào)分隔的列表走改分組
            if (servers != null && !servers.isEmpty()) {
                Random random = new Random(System.currentTimeMillis());
                int index = random.nextInt(servers.size());
                int i = 0;
                while(i < servers.size()) {
                    String server = (String)servers.get(index);
                    try {
                        return this.callServer(api, params, body, server, method);
                    } catch (NacosException var13) {
                        exception = var13;
                        if (LogUtils.NAMING_LOGGER.isDebugEnabled()) {
                            LogUtils.NAMING_LOGGER.debug("request {} failed.", server, var13);
                        }
                        index = (index + 1) % servers.size();
                        ++i;
                    }
                }
            }
             // 如果nacos.discovery.server-addr只配置一個(gè)服務(wù)地址而不是逗號(hào)分隔的多個(gè)服務(wù)地址
            if (StringUtils.isNotBlank(this.nacosDomain)) {
                int i = 0;
                while(i < 3) {
                    try {
                        return this.callServer(api, params, body, this.nacosDomain, method);
                    } catch (NacosException var12) {
                        exception = var12;
                        if (LogUtils.NAMING_LOGGER.isDebugEnabled()) {
                            LogUtils.NAMING_LOGGER.debug("request {} failed.", this.nacosDomain, var12);
                        }
                        ++i;
                    }
                }
            }
			...
        }
    }
	/**
		調(diào)用nacos服務(wù)器,把springboot服務(wù)注冊(cè)為微服務(wù)
		使用服務(wù)注冊(cè)接口:http://xxx:xxx/nacos/v1/ns/instance
	**/
    public String callServer(String api, Map<String, String> params, String body, String curServer, String method) throws NacosException {
        long start = System.currentTimeMillis();
        long end = 0L;
        this.injectSecurityInfo(params);
        List<String> headers = this.builderHeaders();
        String url;
        // 如果nacos.discovery.server-addr地址不是http://或者h(yuǎn)ttps://開頭走該分組
        if (!curServer.startsWith("https://") && !curServer.startsWith("http://")) {
            // 如果只寫了ip地址,會(huì)追加默認(rèn)的8848端口
            if (!curServer.contains(":")) {
                curServer = curServer + ":" + this.serverPort;
            }
            // 添加http前綴,http或者h(yuǎn)ttps,配置項(xiàng)com.alibaba.nacos.client.naming.tls.enable確定是http還是https
            url = HttpClient.getPrefix() + curServer + api;
        } else {
            url = curServer + api;
        }
        HttpResult result = HttpClient.request(url, headers, params, body, "UTF-8", method);
        end = System.currentTimeMillis();
        MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(result.code)).observe((double)(end - start));
        if (200 == result.code) {
            return result.content;
        } else if (304 == result.code) {
            return "";
        } else {
            throw new NacosException(result.code, result.content);
        }
    }

小結(jié)

nacos.discovery.server-addr配置項(xiàng)支持的方式:

單個(gè)ip:port形式

nacos.discovery.server-addr=192.168.10.18:8858

多個(gè)ip:port形式

nacos.discovery.server-addr=192.168.10.18:8858,192.168.10.19:8858

域名方式(http://或者h(yuǎn)ttps://開頭)

nacos.discovery.server-addr=https://www.xxx

從源碼我們可以看出,其實(shí)nacos.discovery.server-addr配置多個(gè)地址,nacos會(huì)隨機(jī)選擇一個(gè)服務(wù)器地址,如果注冊(cè)成功就返回了,不會(huì)去處理其他的服務(wù)地址,除非一個(gè)地址注冊(cè)失敗才會(huì)使用其他的地址注冊(cè);nacos集群情況下,最好配置多個(gè)地址,放在一個(gè)nacos注冊(cè)失敗導(dǎo)致服務(wù)注冊(cè)不上的問題。

源碼流程圖

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)服務(wù)接入nacos注冊(cè)中心流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot接入nacos內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Swing中依據(jù)鼠標(biāo)拖拽來畫出矩形的實(shí)現(xiàn)方法

    Swing中依據(jù)鼠標(biāo)拖拽來畫出矩形的實(shí)現(xiàn)方法

    這篇文章主要介紹了Swing中依據(jù)鼠標(biāo)拖拽來畫出矩形的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 在Spring異步調(diào)用中傳遞上下文的方法

    在Spring異步調(diào)用中傳遞上下文的方法

    這篇文章主要給大家介紹了關(guān)于如何在Spring異步調(diào)用中傳遞上下文的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • java多線程編程之使用Synchronized關(guān)鍵字同步類方法

    java多線程編程之使用Synchronized關(guān)鍵字同步類方法

    JAVA中要想解決“臟數(shù)據(jù)”的問題,最簡單的方法就是使用synchronized關(guān)鍵字來使run方法同步,看下面的代碼,只要在void和public之間加上synchronized關(guān)鍵字
    2014-01-01
  • Java使用Callable接口實(shí)現(xiàn)多線程的實(shí)例代碼

    Java使用Callable接口實(shí)現(xiàn)多線程的實(shí)例代碼

    這篇文章主要介紹了Java使用Callable接口實(shí)現(xiàn)多線程的實(shí)例代碼,實(shí)現(xiàn)Callable和實(shí)現(xiàn)Runnable類似,但是功能更強(qiáng)大,具體表現(xiàn)在可以在任務(wù)結(jié)束后提供一個(gè)返回值,Runnable不行,call方法可以拋出異,Runnable的run方法不行,需要的朋友可以參考下
    2023-08-08
  • SSH框架網(wǎng)上商城項(xiàng)目第7戰(zhàn)之整合Struts2和Json

    SSH框架網(wǎng)上商城項(xiàng)目第7戰(zhàn)之整合Struts2和Json

    SSH框架網(wǎng)上商城項(xiàng)目第7戰(zhàn)之整合Struts2和Json,打通EasyUI和Struts2之間的交互,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 與近日火爆的ChatGPT聊Elasticsearch源碼

    與近日火爆的ChatGPT聊Elasticsearch源碼

    這篇文章主要為大家分享了與近日火爆的ChatGPT聊Elasticsearch源碼的話題內(nèi)容,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • java基于C/S模式實(shí)現(xiàn)聊天程序(服務(wù)器)

    java基于C/S模式實(shí)現(xiàn)聊天程序(服務(wù)器)

    這篇文章主要為大家詳細(xì)介紹了java基于C/S模式實(shí)現(xiàn)聊天程序的服務(wù)器篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Java中for與foreach的區(qū)別

    Java中for與foreach的區(qū)別

    本文主要介紹了Java中for與foreach的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺(tái)服務(wù)注冊(cè)、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問負(fù)載均衡訪問的問題

    Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺(tái)服務(wù)注冊(cè)、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪

    這篇文章主要介紹了Spring?Cloud?Alibaba:Nacos服務(wù)治理平臺(tái),服務(wù)注冊(cè)、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問,負(fù)載均衡訪問,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Java數(shù)學(xué)工具類MathUtil詳解

    Java數(shù)學(xué)工具類MathUtil詳解

    這篇文章主要為大家詳細(xì)介紹了Java數(shù)學(xué)工具類MathUtil的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08

最新評(píng)論