首頁 > 軟體

SpringBoot實現服務接入nacos註冊中心流程詳解

2023-01-14 14:00:43

概述

某些場景下只需要把springboot微服務化而不想引入springcloud如何實現的呢?

下面我們介紹nacos註冊中心方案。

接入nacos註冊中心

springboot服務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註冊中心-->
        <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註冊
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
# 指定微服務註冊那個地址
nacos.discovery.register.ip=

原始碼分析

主流程:

1、nacos-discovery-spring-boot-starter 啟動服務通過SPI機制掃描到nacos-discovery-spring-boot-autoconfigure包。

2、nacos-discovery-spring-boot-autoconfigure專案通過自動裝配功能裝配nacos使用者端

3、 Nacos自動設定服務實現Spring的應用監聽器用來註冊nacos服務

NacosDiscoveryAutoRegister implements ApplicationListener

4、NacosDiscoveryAutoRegister監聽到spring的ServletWebServerInitializedEvent事件後把springboot服務註冊到nacos註冊中心

5、呼叫nacos-client jar包中的com.alibaba.nacos.client.naming.net.NamingProxy#registerService完成服務註冊

核心處理邏輯: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是逗號分隔的列表走改分組
            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只設定一個服務地址而不是逗號分隔的多個服務地址
            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;
                    }
                }
            }
			...
        }
    }
	/**
		呼叫nacos伺服器,把springboot服務註冊為微服務
		使用服務註冊介面: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://或者https://開頭走該分組
        if (!curServer.startsWith("https://") && !curServer.startsWith("http://")) {
            // 如果只寫了ip地址,會追加預設的8848埠
            if (!curServer.contains(":")) {
                curServer = curServer + ":" + this.serverPort;
            }
            // 新增http字首,http或者https,設定項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);
        }
    }

小結

nacos.discovery.server-addr設定項支援的方式:

單個ip:port形式

nacos.discovery.server-addr=192.168.10.18:8858

多個ip:port形式

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

域名方式(http://或者https://開頭)

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

從原始碼我們可以看出,其實nacos.discovery.server-addr設定多個地址,nacos會隨機選擇一個伺服器地址,如果註冊成功就返回了,不會去處理其他的服務地址,除非一個地址註冊失敗才會使用其他的地址註冊;nacos叢集情況下,最好設定多個地址,放在一個nacos註冊失敗導致服務註冊不上的問題。

原始碼流程圖

到此這篇關於SpringBoot實現服務接入nacos註冊中心流程詳解的文章就介紹到這了,更多相關SpringBoot接入nacos內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com