Spring Cloud Alibaba系列之快速开始搭建Nacos环境 4年前

1、前言介绍

前言:本实验介绍如何搭建Spring Cloud Alibaba Nacos的服务端,nacos server是一个服务注册中心也是一个分布式配置中心

2、实验环境准备

  • 实验环境准备
    • 64bit Git Server
    • 64bit JDK 1.8+
    • Maven 3.2.x+

3、下载编译Nacos

git clone nacos server:

git clone https://github.com/alibaba/nacos.git

nacos官网也给出了比较详细的教程,如果是下载源码的,需要自己用Maven编译

如果不想自己编译源码,可以自行去官网下载release,https://github.com/alibaba/nacos/releases

版本关系表,图来自官网:

如果下载了zip文件,linux使用unzip,window环境直接使用压缩软件

unzip nacos-server-$version.zip

tar.gz文件使用tar命令:

tar -xvf nacos-server-$version.tar.gz

4、启动Nacos Server

window+R启动cmd窗口,cd到nacos server的bin目录,linux系统直接使用cd ${nacos_server_home}/bin

./startup.sh -m standalone

window系统使用命令startup.cmd -m standalone

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

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

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 1.4.0
,`--.'`|  ' :                       ,---.               Running in stand alone m
ode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 5780
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.
9.30:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2020-12-25 10:03:22,242 INFO Bean 'org.springframework.security.access.expressio
n.method.DefaultMethodSecurityExpressionHandler@2474f125' of type [org.springfra
mework.security.access.expression.method.DefaultMethodSecurityExpressionHandler]
 is not eligible for getting processed by all BeanPostProcessors (for example: n
ot eligible for auto-proxying)

2020-12-25 10:03:22,255 INFO Bean 'methodSecurityMetadataSource' of type [org.sp
ringframework.security.access.method.DelegatingMethodSecurityMetadataSource] is
not eligible for getting processed by all BeanPostProcessors (for example: not e
ligible for auto-proxying)

2020-12-25 10:03:24,192 INFO Tomcat initialized with port(s): 8848 (http)

2020-12-25 10:03:25,110 INFO Root WebApplicationContext: initialization complete
d in 9321 ms

2020-12-25 10:03:46,747 INFO Initializing ExecutorService 'applicationTaskExecut
or'

2020-12-25 10:03:46,994 INFO Adding welcome page: class path resource [static/in
dex.html]

2020-12-25 10:03:47,827 INFO Creating filter chain: Ant [pattern='/**'], []

2020-12-25 10:03:47,898 INFO Creating filter chain: any request, [org.springfram
ework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@33aa93
c, org.springframework.security.web.context.SecurityContextPersistenceFilter@6e9
c413e, org.springframework.security.web.header.HeaderWriterFilter@7561db12, org.
springframework.security.web.csrf.CsrfFilter@619bd14c, org.springframework.secur
ity.web.authentication.logout.LogoutFilter@27a0a5a2, org.springframework.securit
y.web.savedrequest.RequestCacheAwareFilter@5af5def9, org.springframework.securit
y.web.servletapi.SecurityContextHolderAwareRequestFilter@47a64f7d, org.springfra
mework.security.web.authentication.AnonymousAuthenticationFilter@32c0915e, org.s
pringframework.security.web.session.SessionManagementFilter@24b52d3e, org.spring
framework.security.web.access.ExceptionTranslationFilter@a23a01d]

2020-12-25 10:03:48,112 INFO Initializing ExecutorService 'taskScheduler'

2020-12-25 10:03:48,145 INFO Exposing 2 endpoint(s) beneath base path '/actuator
'

2020-12-25 10:03:48,348 INFO Tomcat started on port(s): 8848 (http) with context
 path '/nacos'

2020-12-25 10:03:48,355 INFO Nacos Log files: D:\nacos-server-1.4.0\logs

2020-12-25 10:03:48,457 INFO Nacos Log files: D:\nacos-server-1.4.0\conf

2020-12-25 10:03:48,458 INFO Nacos Log files: D:\nacos-server-1.4.0\data

2020-12-25 10:03:48,460 INFO Nacos started successfully in stand alone mode. use
 embedded storage

2020-12-25 10:05:10,943 INFO Initializing Servlet 'dispatcherServlet'

2020-12-25 10:05:10,970 INFO Completed initialization in 26 ms

启动成功,访问:http://127.0.0.1:8848/nacos,账号密码都是nacos

登录成功,来到主页:

5、测试Nacos服务注册

使用官网的命令在linux环境测试:curl是linux系统的命令,在window系统可以使用postman测试

curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'


[www@localhost ~]$ curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
ok

命令:获取nacos服务列表数据

curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?servicame=nacos.naming.serviceName'

服务列表接口:

[www@localhost ~]$ curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?servicame=nacos.naming.serviceName'
{
   
   
   "metadata":{
   
   
   },"dom":"nacos.naming.serviceName","cacheMillis":3000,"useSpecifiedURL":false,"hosts":[{
   
   
   "valid":true,"marked":false,"metadata":{
   
   
   },"instanceId":"20.18.7.10#8080#DEFAULT#DEFAULT_GROUP@@nacos.naming.serviceName","port":8080,"healthy":true,"ip":"20.18.7.10","clusterName":"DEFAULT","weight":1.0,"ephemeral":true,"serviceName":"nacos.naming.serviceName","enabled":true}],"name":"DEFAULT_GROUP@@nacos.naming.serviceName","checksum":"ccfea5630926adefdc86acdf6076af15","lastRefTime":1608880386244,"env":"","clusters":""}

格式化的json数据:

{
   
   
   
    "metadata":{
   
   
   

    },
    "dom":"nacos.naming.serviceName",
    "cacheMillis":3000,
    "useSpecifiedURL":false,
    "hosts":[
        {
   
   
   
            "valid":true,
            "marked":false,
            "metadata":{
   
   
   

            },
            "instanceId":"20.18.7.10#8080#DEFAULT#DEFAULT_GROUP@@nacos.naming.serviceName",
            "port":8080,
            "healthy":true,
            "ip":"20.18.7.10",
            "clusterName":"DEFAULT",
            "weight":1,
            "ephemeral":true,
            "serviceName":"nacos.naming.serviceName",
            "enabled":true
        }
    ],
    "name":"DEFAULT_GROUP@@nacos.naming.serviceName",
    "checksum":"ccfea5630926adefdc86acdf6076af15",
    "lastRefTime":1608880386244,
    "env":"",
    "clusters":""
}

6、测试分布式配置

nacos也提供了分布式的配置中心:

curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"


[www@localhost ~]$ curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
config data not exist

7、关闭Nacos Server

linux命令:

sh shutdown.sh

window系统要先cd到bin目录,然后在cmd窗口执行,也可以直接点击shutdown.cmd这个文件

shutdown.cmd

8、Nacos参考资料

本文同步分享在 博客“smileNicky”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

image
战场原黒仪
晚日寒鸦一片愁。柳塘新绿却温柔。
4
发布数
1
关注者
1262
累计阅读

热门教程文档

React
18小节
C++
73小节
Java
12小节
React Native
40小节
MySQL
34小节
广告