SpringCloud Alibaba 之 Nacos注册中心实战

Nacos作为注册中心(单机版)

一.简介

Nacos是一个易于使用的动态服务发现、配置和服务管理平台,用于构建云原生应用程序,通过Spring Cloud和阿里巴巴Nacos
Discovery,您可以基于Spring云的编程模型快速访问Nacos服务注册功能。

服务发现是微服务体系结构中的关键组件之一。在这样的体系结构中,为每个客户机手动配置服务列表可能是一项艰巨的任务,这使得动态扩展非常困难。Nacos Discovery帮助您自动向Nacos服务器注册服务,Nacos server跟踪服务并动态刷新服务列表。此外,Nacos Discovery向Nacos注册服务实例的一些元数据,例如主机、端口、健康检查URL和主页。

二.使用

版本选择一直是使用SpringBoot与SpringCloud中遇到的比较恶心的问题,网上关于Nacos的介绍也很少提到这些版本对应的关系,一不小心就会导致各种错误,为了防止大家踩坑,专门在这里整理了一下SpringBoot与SpringCloud以及SpringBoot与SpringCloudAlibaba版本的对应关系,希望对大家有帮助.

1. 版本选择

1.SpringCloudAlibaba与SpringBoot的版本对应

由于 Spring Boot 1 和 Spring Boot 2 在 Actuator 模块的接口和注解有很大的变更,且 spring-cloud-commons 从 1.x.x 版本升级到 2.0.0 版本也有较大的变更,因此我们采取跟 SpringBoot 版本号一致的版本:

  • 1.5.x 版本适用于 Spring Boot 1.5.x
  • 2.0.x 版本适用于 Spring Boot 2.0.x
  • 2.1.x 版本适用于 Spring Boot 2.1.x
  • 2.2.x 版本适用于 Spring Boot 2.2.x
  • 2021.x 版本适用于 Spring Boot 2.4.x

目前官方文档只公示以上几个SpringBoot与Spring Cloud Alibaba的对应关系。

2.SpringCloud与SpringBoot的版本对应
SpringCloud版本SpringBoot版本
2021.0.1-SNAPSHOTSpring Boot >=2.6.4-SNAPSHOT and <2.7.0-M1
2021.0.0Spring Boot >=2.6.1 and <2.6.4-SNAPSHOT
2021.0.0-RC1Spring Boot >=2.6.0-RC1 and <2.6.1
2021.0.0-M3Spring Boot >=2.6.0-M3 and <2.6.0-RC1
2021.0.0-M1Spring Boot >=2.6.0-M1 and <2.6.0-M3
2020.0.5Spring Boot >=2.4.0.M1 and <2.6.0-M1
Hoxton.SR12Spring Boot >=2.2.0.RELEASE and <2.4.0.M1

详情请参考SpringBoot与SpringCloud对应关系

2. 依赖说明

本次使用版本说明

  • Jdk 1.8
  • Spring Boot 2.4.2
  • Spring Cloud 2020.0.1
  • Spring Cloud Alibaba 2021.1
1. 依赖管理

依赖关系管理如果您是Maven Central用户,请将我们的BOM添加到您的pom中。xml<dependencyManagement>部分。这将允许您省略任何Maven依赖项的版本,而是将版本控制委托给BOM。

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

spring-cloud-dependencies和spring-cloud-alibaba-dependencies是微服务必不可少的两个依赖,作用是去声明组件的版本,方便版本管理。

2. nacos依赖

我们上面已经做了依赖管理,所以这里就不用再去关注spring-cloud-starter-alibaba-nacos-discovery的版本了

 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 </dependency>
3. 其他依赖

使用 spring-boot-starter-actuator 可以用于检测系统的健康情况、当前的Beans、系统的缓存等

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
4. pom.xml

以下pom文件是本次教程的所有依赖,可以直接拷贝使用。

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nacos</groupId>
    <artifactId>nacos_provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos_provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.version>2020.0.1</spring.cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-actuator</artifactId>
        </dependency>
        <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3. 配置文件

在application.properties接入以下配置:

server.port=8081
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*

4. 启动类加注解

在启动类上加上@EnableDiscoveryClient注解

@EnableDiscoveryClient
@SpringBootApplication
public class NacosProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }

}

此时如果我们启动工程的话,会报以下错误:

com.alibaba.nacos.api.exception.NacosException: failed to req API:/nacos/v1/ns/instance after all servers([127.0.0.1:8848]) tried: java.net.ConnectException: Connection refused: connect
org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is java.lang.reflect.UndeclaredThrowableException

那是因为我们还没有启动nacos客户端,因此无法注册我们的服务信息,不要着急,请接着往下看。

5. 启动nacoe客户端

1.下载客户端zip文件

进入Nacos官网nacos
在这里插入图片描述
点击下面的版本说明V2.1.0版本说明下载nacos-server-2.1.0.zip
在这里插入图片描述

2.启动nacos-service

nacos-service默认是集群启动,若要单机启动,在bin目录下执行 startup.cmd -m standalone 命令

在这里插入图片描述

D:\nacos\nacos-server-2.1.0\nacos\bin>startup.cmd -m standalone
"nacos is starting with standalone"

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 2.1.0
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 6428
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://10.61.156.135:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2022-08-09 15:03:01,860 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@48f5bde6' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2022-08-09 15:03:01,860 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2022-08-09 15:03:02,404 INFO Tomcat initialized with port(s): 8848 (http)

2022-08-09 15:03:02,879 INFO Root WebApplicationContext: initialization completed in 4303 ms

2022-08-09 15:03:08,507 INFO Initializing ExecutorService 'applicationTaskExecutor'

2022-08-09 15:03:08,691 INFO Adding welcome page: class path resource [static/index.html]

2022-08-09 15:03:09,175 INFO Creating filter chain: Ant [pattern='/**'], []

2022-08-09 15:03:09,248 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@5c20aab9, org.springframework.security.web.context.SecurityContextPersistenceFilter@7fb33394, org.springframework.security.web.header.HeaderWriterFilter@75308740, org.springframework.security.web.csrf.CsrfFilter@25da615a, org.springframework.security.web.authentication.logout.LogoutFilter@387bf2d9, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1a891add, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@d74bac4, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4b7c4456, org.springframework.security.web.session.SessionManagementFilter@3a5c2626, org.springframework.security.web.access.ExceptionTranslationFilter@7ee3d262]

2022-08-09 15:03:09,388 INFO Initializing ExecutorService 'taskScheduler'

2022-08-09 15:03:09,420 INFO Exposing 2 endpoint(s) beneath base path '/actuator'

2022-08-09 15:03:09,583 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2022-08-09 15:03:09,593 INFO Nacos started successfully in stand alone mode. use embedded storage

6. 启动服务端

在这里插入图片描述

输入 http://127.0.0.1:8848/nacos/index.html#/
就可以登陆我们的注册中心了,nacos注册中心的用户名和密码都是nocas

在这里就能看到我们的服务注册上来了
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值