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

概述

某些場景下隻需要把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內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: