亲宝软件园·资讯

展开

SpringBoot实现服务接入nacos注册中心流程详解

融极 人气:0

概述

某些场景下只需要把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注册失败导致服务注册不上的问题。

源码流程图

加载全部内容

相关教程
猜你喜欢
用户评论