nacos详解 一

Nacos是什么

 

 

欢迎来到Nocos的世界!

组成部分全称描述
Na naming/nameServer 即服务注册中心,与 Spring Cloud Eureka 的功能类似。
co configuration 即配置中心,与 Spring Cloud Config+Spring Cloud Bus 的功能类似。
s service 即服务,表示 Nacos 实现的服务注册中心和配置中心都是以服务为核心的。

Nacos 是 Dynamic Naming and Configuration-Service 的首字母简称,定位于一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。从 2018 年 7 月开始宣布开源以来,已经走过了第四个年头,在这四年里,备受广大开源用户欢迎,共收获 25.1K ⭐的 star 数11.1K 的 Fork 🌲 数

Nacos 在社区共同的建设下不断成长,逐步的开始帮助用户解决实际问题,助力企业数字化转型,目前已经广泛的使用在国内的公司中。

Nacos两大组件

组件描述功能
Nacos Server Nacos 服务端,与 Eureka Server 不同,Nacos Server 由阿里巴巴团队使用 Java 语言编写并将 Nacos Server 的下载地址给用户,用户只需要直接下载并运行即可。 Nacos Server 可以作为服务注册中心,帮助 Nacos Client 实现服务的注册与发现。
Nacos Server 可以作为配置中心,帮助 Nacos Client 在不重启的情况下,实现配置的动态刷新。
Nacos Client Nacos 客户端,通常指的是微服务架构中的各个服务,由用户自己搭建,可以使用多种语言编写。 Nacos Client 通过添加依赖 spring-cloud-starter-alibaba-nacos-discovery,在服务注册中心(Nacos Server)中实现服务的注册与发现。
Nacos Client 通过添加依赖 spring-cloud-starter-alibaba-nacos-config,在配置中心(Nacos Server)中实现配置的动态刷新。

数据模型(配置和服务的组织形式)

Nacos 数据模型 Key 由三元组唯一确定。 Namespace默认是空串,公共命名空间(public)。分组默认是 DEFAULT_GROUP。

 

 

nacos相关概念

  • 命名空间(Namespace)

    Namespace 的常用场景之一是不同环境的配置的区分隔离,例如开发测试环境(dev)和生产环境(pro)的资源(如配置、服务)隔离等。

    • 可以隔离开发环境——测试环境和生产环境,因为它们的配置可能各不相同;
    • 可以隔离不同的用户——不同的开发人员使用同一个nacos管理各自的配置,可通过namespace隔离。不同的命名空间下,可以存在相同名称的配置分组(Group) 或配置集。
  • 配置分组(group)

    Nacos 中的一组配置集,一个有意义的字符串(如 Buy 或 Trade )对配置集进行分组,从而区分 Data ID 相同的配置集,默认采用 DEFAULT_GROUP 。

    配置分组的常见场景:不同的应用或组件使用了相同的配置类型,如 database_url 配置和 MQ_topic 配置。

  • 配置集/配置集ID(Service/DataId)

    配置项的集合称为配置集。在系统中,一个配置文件通常就是一个配置集,包含了系统各个方面的配置。每个配置集都可以被一个有意义的名称标识——配置集 ID (Data ID)。

其他相关概念详情请阅读官网Nacos 概念

安装和下载Nacos服务

下面我们以 Nacos 2.0.3 为例,演示下如何安装和运行 Nacos Server,步骤如下。

  1. 使用浏览器访问 Nacos Server 下载页面,并在页面最下方点击链接 nacos-server-2.0.3.zip,如下图。

     

     

     

  2. 下载完成后,解压 nacos-server-2.0.3.zip,目录结构如下。

     

     

     

  3. 打开命令行窗口,跳转到 Nacos Server 安装目录的 bin 下,执行以下命令,以单机模式启动 Nacos Server。

     
     
     
     
     
     
    # 单机模式
    startup.cmd -m standalone
     

     

  4. Nacos Server 启动日志如下。

     
     
     
     
     
     
    "nacos is starting with standalone"
    
    
             ,--.
           ,--.'|
       ,--,:  : |                                           Nacos 2.0.3
    ,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
    |   :  :  | |                      '   ,'\   .--.--.    Port: 8848
    :   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 27512
    |   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.3.138:8848/nacos/index.html
    '   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
    |   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
    '   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
    |   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
    '   : |     ;  :   .'   \   :    : `----'  '--'.     /
    ;   |.'     |  ,     .-./\   \  /            `--'---'
    '---'        `--`---'     `----'
    
    
    2021-11-08 16:16:38,877 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@5ab9b447' 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)
    2021-11-08 16:16:38,884 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by al
    l BeanPostProcessors (for example: not eligible for auto-proxying)
    2021-11-08 16:16:40,001 INFO Tomcat initialized with port(s): 8848 (http)
    2021-11-08 16:16:40,713 INFO Root WebApplicationContext: initialization completed in 14868 ms
    2021-11-08 16:16:52,351 INFO Initializing ExecutorService 'applicationTaskExecutor'
    2021-11-08 16:16:52,560 INFO Adding welcome page: class path resource [static/index.html]
    2021-11-08 16:16:54,239 INFO Creating filter chain: Ant [pattern='/**'], []
    2021-11-08 16:16:54,344 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7dd611c8, org.springframework.security.web.con
    text.SecurityContextPersistenceFilter@5c7668ba, org.springframework.security.web.header.HeaderWriterFilter@fb713e7, org.springframework.security.web.csrf.CsrfFilter@6ec7bce0, org.springframework.secur
    ity.web.authentication.logout.LogoutFilter@7d9ba6c, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@158f4cfe, org.springframework.security.web.servletapi.SecurityContextHolderAwa
    reRequestFilter@6c6333cd, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5d425813, org.springframework.security.web.session.SessionManagementFilter@13741d5a, org.springf
    ramework.security.web.access.ExceptionTranslationFilter@3727f0ee]
    2021-11-08 16:16:54,948 INFO Initializing ExecutorService 'taskScheduler'
    2021-11-08 16:16:54,977 INFO Exposing 16 endpoint(s) beneath base path '/actuator'
    2021-11-08 16:16:55,309 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
    2021-11-08 16:16:55,319 INFO Nacos started successfully in stand alone mode. use embedded storage
     

     

  5. 使用浏览器访问“http://localhost:8848/nacos”,跳转到 Nacos Server 登陆页面,如下图。

     

     

     

  6. 在登陆页输入登录名和密码(默认都是 nacos),点击提交按钮,跳转到 Nacos Server 控制台主页,如下图。

     

     

     

    自此,我们就完成了 Nacos Server 的下载、安装和运行工作。

 

Nacos server集群部署

在实际的项目开发中,一个微服务系统往往由十几,几十个甚至几百个微服务组成。 这些服务若全部注册到同一台 Nacos Server,就极有可能导致 Nacos Server 因为不堪重负而崩溃,最终导致整个微服务系统瘫痪。解决这个问题最直接的办法就是使用 Nacos Server 集群。

Nacos Server 的集群化部署有一个十分明显的优点,那就是可以保障系统的高可用性。在集群化部署中,只要不是所有的 Nacos Server 都停止工作,Nacos Client 就还可以从集群中正常的 Nacos Server 上获取服务信息及配置,而不会导致系统的整体瘫痪,这就是 Nacos Server 集群化部署的高可用性。

下图展示了 Nacos Server 集群化部署的基本架构。

 

 

下面我们以 Windows 系统为例,演示如何部署 Nacos Server 集群。

  1. 复制三分Nacos目录分别修改application.properties的端口号为8848、8846、8844

  2. 在conf目录下新建cluster.conf 输入:

     
     
     
     
     
     
    192.168.10.76:8845
    192.168.10.76:8847
    192.168.10.76:8849
     
  3. 启动nginx.exe, nginx的配置文件如下:

     
     
     
     
     
     
    http {
        upstream nacos {
                    server 192.168.10.76:8848;
                    server 192.168.10.76:8846;
                    server 192.168.10.76:8844;
            }
            server {
                listen       80;
                server_name  localhost;
    
    
                #charset koi8-r;
    
    
                #access_log  logs/host.access.log  main;
    
    
                location / {
                    root   html;
                    index  index.html index.htm;
                    add_header Access-Control-Allow-Origin *;
                }
    
    
                location /nacos/ {
                    proxy_pass http://nacos/nacos/;
                }
          }
    }
     

     

  4. 分别打开三个nacos的bin目录,执行下面命令:

     
     
     
     
     
     
    startup.cmd -m cluster
     

     

  5. 启动日志如下:

     
     
     
     
     
     
    "nacos is starting with cluster"
    
    
             ,--.
           ,--.'|
       ,--,:  : |                                           Nacos 2.1.1
    ,`--.'`|  ' :                       ,---.               Running in cluster mode, All function modules
    |   :  :  | |                      '   ,'\   .--.--.    Port: 8848
    :   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 16340
    |   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.10.76:8848/nacos/index.html
    '   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
    |   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
    '   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
    |   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
    '   : |     ;  :   .'   \   :    : `----'  '--'.     /
    ;   |.'     |  ,     .-./\   \  /            `--'---'
    '---'        `--`---'     `----'
    
    
    2023-01-03 16:14:49,041 INFO The server IP list of Nacos is [192.168.10.76:8844, 192.168.10.76:8846, 192.168.10.76:8848]
    
    
    2023-01-03 16:14:50,042 INFO Nacos is starting...
    
    
    2023-01-03 16:14:51,043 INFO Nacos is starting...
    
    
    2023-01-03 16:14:52,062 INFO Nacos is starting...
    
    
    2023-01-03 16:14:53,080 INFO Nacos is starting...
    
    
    2023-01-03 16:14:54,085 INFO Nacos is starting...
    
    
    2023-01-03 16:14:55,086 INFO Nacos is starting...
    
    
    2023-01-03 16:14:56,089 INFO Nacos is starting...
    
    
    2023-01-03 16:14:57,091 INFO Nacos is starting...
    
    
    2023-01-03 16:14:58,094 INFO Nacos is starting...
    
    
    2023-01-03 16:14:58,799 INFO Nacos started successfully in cluster mode. use external storage
     

     

  6. 访问http://localhost/nacos,在页面的集群管理下的节点列表中可以看到如下启动节点,则说明启动成功,页面如下:

     

     

  7. 将主工程配置文件中的 Nacos Server 地址统一修改为:localhost:80。我们以 spring-cloud-alibaba-consumer-nacos-8801 为例,配置文件 application.yml 的配置内容如下。

posted @ 2023-01-09 10:06  jiuchengi  阅读(653)  评论(0编辑  收藏  举报