NACOS快速入门

200 阅读3分钟

nacos介绍

阿里的文档一直是很不错的,想要详细了解去看官方文档即可nacos.io/zh-cn/docs/…

在用nacos之前需要知道nacos主要是作为【注册中心】与【配置中心】使用的。

本篇文章先只简单说明【注册中心】如何快速使用,他天然就支持对服务的分组管理,多方面维度分组,我都将在这篇文章中快速演示。

安装nacos

默认基于docker安装

镜像拉取

docker pull nacos/nacos-server

容器生成

# MODE=standalone ---> 单例模式启动
docker run --name nacos-quick -e MODE=standalone -p 8848:8848 -d nacos/nacos-server:latest

访问nacos

地址:http://127.0.0.1:8848/nacos 账号/密码:nacos/nacos

image.png

测试服务搭建

后续引入spring cloud的构建方式,目前就快速的整两个纯净的spring boot项目

先从官网下载一个纯净的spring boot项目

image.png

引入相关pom

要注意版本对应关系,官方地址:github.com/alibaba/spr… 目前的关系表:(我本地boot版本2.6.7)

Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version
2021.0.1.0Spring Cloud 2021.0.12.6.3
2.2.7.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2021.1Spring Cloud 2020.0.12.4.2
2.2.6.RELEASESpring Cloud Hoxton.SR92.3.2.RELEASE
2.1.4.RELEASESpring Cloud Greenwich.SR62.1.13.RELEASE
2.2.1.RELEASESpring Cloud Hoxton.SR32.2.5.RELEASE
2.2.0.RELEASESpring Cloud Hoxton.RELEASE2.2.X.RELEASE
2.1.2.RELEASESpring Cloud Greenwich2.1.X.RELEASE
2.0.4.RELEASE(停止维护,建议升级)Spring Cloud Finchley2.0.X.RELEASE
1.5.1.RELEASE(停止维护,建议升级)Spring Cloud Edgware1.5.X.RELEASE
  • 正确版本的cloud
<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-dependencies</artifactId>
         <version>2021.0.1</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>
  • 正确版本的spring cloud alibaba
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-alibaba-dependencies</artifactId>
   <version>2021.0.1.0</version>
   <type>pom</type>
   <scope>import</scope>
</dependency>
  • nacos服务发现
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   <version>2021.0.1.0</version>
</dependency>
  • 经常被忽视但是特别重要的starter-web
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

启动类添加被扫描 @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

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

}

增加相关配置,application.properties或者.yaml都可以

# 服务端口号
server.port=8081
# 注册中心地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# 应用名称
spring.application.name=student

启动服务,查看页面

image.png 如法炮制的去给另一个服务做相同配置,两个就都可以被发现了

集群

顾名思义,像阿里这种大互联网公司,肯定是有许多集群构成,可能有的在上海,有的在杭州,nacos就做了可以分配集群的功能。同一片局域网内的访问效率肯定是要高于夸域访问的。

配置集群

一个配杭州,一个配上海

spring.cloud.nacos.discovery.cluster-name=杭州

查看详情

image.png

image.png 跨集群可以访问,但是优先级是集群内更高,这里牵扯到了负载均衡,咱不讨论,等着负载均衡的时候一起说

命名空间

主要是为了解决区分生成环境与测试环境.他们之间是生殖隔离的,无法互相访问

新建命名空间

image.png

项目中增加配置,注意这里用的是ID

spring.cloud.nacos.discovery.namespace=20c0e599-919c-483a-a3d7-c73eaf3b4e7c

启动项目

image.png

临时实例

在服务的详情中可以看到一个字段,临时实例。

服务与nacos之间默认心跳间隔5秒;

nacos会在超过15秒未收到心跳后将实例设置为不健康状态;

客户端每隔一定时间会去拉nacos的服务列表等信息

超过30秒将实例删除;

nacos有变化会主动给客户端推送。

image.png

设置为非临时实例/持久化实例

spring.cloud.nacos.discovery.ephemeral=false

临时与持久的区别

顾名思义,临时的会被清除,持久的会持续保留

临时和持久化的区别主要在健康检查失败后的表现,持久化实例健康检查失败后会被标记成不健康,而临时实例会直接从列表中被删除。