1.为什么Nacos?
Nacos是一款微服务的注册和配置管理中心开源软件,是阿里巴巴贡献给开源社区的,其遵循Apache 2.0的开源协议,可以免费的使用于商业环境,而且阿里云上已经全面支持。
其Github地址https://github.com/alibaba/nacos ,目前此开源项目已经有14.4k个星星了,已经非常厉害了!
相对于Eureka,其不仅仅是微服务的注册中心,还能同时担任配置管理中心,界面也非常的友好。琦彦大神在其博客《微服务:注册中心ZooKeeper、Eureka、Consul 、Nacos对比》 一文中对这些注册中心进行了详细的对比,对比的标准就是分布式系统设计当中的2个黄金定律:
- CAP
1. 一致性(Consistency) (所有节点在同一时间具有相同的数据)
2. 可用性(Availability) (保证每个请求不管成功或者失败都有响应)
3. 分隔容忍(Partition tolerance) (系统中任意信息的丢失或失败不会
影响系统的继续运作)
具体的CAP理论,网上的资料很多,比如阮一峰大神的一篇博客《CAP 定理的含义》 就做了很详细的诠释,所以笔者就不再赘述。
- BASE
eBay的架构师Dan Pritchett源于对大规模分布式系统的实践总结,在ACM上发表文章提出BASE理论,BASE理论是对CAP理论的延伸,核心思想是即使无法做到强一致性(Strong Consistency,CAP的一致性就是强一致性),但应用可以采用适合的方式达到最终一致性(Eventual Consitency)。
具体信息,大家可以参考《分布式系统的BASE理论》
琦彦大神在其博客《微服务:注册中心ZooKeeper、Eureka、Consul 、Nacos对比》 一文中,对这些注册中心的对比简单明了。可以用下面的一张表格表示。
从上面的表格中以及Nacos自身的官方文档 可以知道,其不仅支持SpringCloud全家桶中的SpringCloud的RESTful Service, 更支持K8S,gPRC,而且和阿里自己的一套微服务实现框架Dubbo也能无缝集成。
此外,SpringBoot的官方权威的开发工具之一的Spring Tool Suite 也在其“Starter Project”里面默认集成了Ailibaba Nacos Configuration 和 Nacos Service Discovery, 看来其已经得到了主流界的推荐和认可!
说了这么多,下面就一探其真容。其基本架构如下:
其主要组件是Nacos 服务器和Nacos的控制台。截至2020年10月2日,其最新的版本的是1.3.2,可以到此地址下载
注:因为Github上面下载Nacos的安装包的速度比较慢,笔者把最新的上传到了CSDN,零CSDN积分供大家下载,CSDN下载地址
2. Nacos 安装
Nacos的安装还是比较简单的,如果是单机版本的安装更简单。下载一个最新版本的Nacos,其默认支持Windows和Linux, 以Windows为例子。比如,笔者下载并解压缩到了E:\bigdata\nacos-server-1.3.2\nacos\bin,直接进入这个目录,输入下面的命令:
startup.cmd -m standalone
注意:一定要加启动模式,默认情况下是集群模式启动。启动的Log如下:
E:\bigdata\nacos-server-1.3.2\nacos\bin>startup.cmd -m standalone
"nacos is starting with standalone"
,--.
,--.'|
,--,: : | Nacos 1.3.2
,`--.'`| ' : ,---. Running in stand alone mode, All function modules
| : : | | ' ,'\ .--.--. Port: 8848
: | \ | : ,--.--. ,---. / / | / / ' Pid: 13112
| : ' '; | / \ / \. ; ,. :| : /`./ Console: http://192.168.79.1:8848/nacos/index.html
' ' ;. ;.--. .-. | / / '' | |: :| : ;_
| | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io
' : | ; .' ," .--.; |' ; :__| : | `----. \
| | '`--' / / ,. |' | '.'|\ \ / / /`--' /
' : | ; : .' \ : : `----' '--'. /
; |.' | , .-./\ \ / `--'---'
'---' `--`---' `----'
2020-10-03 00:15:00,292 INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$c7035815] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-03 00:15:00,437 INFO Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-03 00:15:00,445 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@7674b62c' 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)
2020-10-03 00:15:00,450 INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$ebd7fac7] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-03 00:15:00,462 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)
2020-10-03 00:15:01,578 INFO Tomcat initialized with port(s): 8848 (http)
2020-10-03 00:15:01,827 INFO Root WebApplicationContext: initialization completed in 8794 ms
2020-10-03 00:15:17,800 INFO Initializing ExecutorService 'applicationTaskExecutor'
2020-10-03 00:15:18,092 INFO Adding welcome page: class path resource [static/index.html]
2020-10-03 00:15:18,954 INFO Creating filter chain: Ant [pattern='/**'], []
2020-10-03 00:15:19,050 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@56db847e, org.springframework.security.web.context.SecurityContextPersistenceFilter@5c1bd44c, org.springframework.security.web.header.HeaderWriterFilter@1eef9aef, org.springframework.security.web.csrf.CsrfFilter@2e5c7f0b, org.springframework.security.web.authentication.logout.LogoutFilter@6e57e95e, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@18cc679e, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@67ef029, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@740abb5, org.springframework.security.web.session.SessionManagementFilter@5db99216, org.springframework.security.web.access.ExceptionTranslationFilter@4de025bf]
2020-10-03 00:15:19,286 INFO Exposing 2 endpoint(s) beneath base path '/actuator'
2020-10-03 00:15:19,335 INFO Initializing ExecutorService 'taskScheduler'
2020-10-03 00:15:19,516 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
2020-10-03 00:15:19,525 INFO Nacos Log files: E:\bigdata\nacos-server-1.3.2\nacos\logs
2020-10-03 00:15:19,529 INFO Nacos Log files: E:\bigdata\nacos-server-1.3.2\nacos\conf
2020-10-03 00:15:19,529 INFO Nacos Log files: E:\bigdata\nacos-server-1.3.2\nacos\data
2020-10-03 00:15:19,529 INFO Nacos started successfully in stand alone mode. use embedded storage
2020-10-03 00:15:22,099 INFO Initializing Servlet 'dispatcherServlet'
2020-10-03 00:15:22,117 INFO Completed initialization in 18 ms
2020-10-03 00:19:42,321 WARN [NotifyCenter] Start destroying Publisher
2020-10-03 00:19:42,321 WARN [ThreadPoolManager] Start destroying ThreadPool
2020-10-03 00:19:42,321 WARN [NotifyCenter] Destruction of the end
2020-10-03 00:19:42,321 INFO Shutting down ExecutorService 'taskScheduler'
2020-10-03 00:19:42,331 INFO Shutting down ExecutorService 'applicationTaskExecutor'
2020-10-03 00:20:06,685 WARN [ThreadPoolManager] Destruction of the end
启动后就能打开网站地址:http://localhost:8848/nacos
默认用户名和密码是:nacos/nacos
在左边的菜单栏里面,我看可以到看到其有配置管理和服务管理
如果需要把配置信息存储到数据库,则需要修改其applications.properties文件。比如下面,笔者准备用mysql
server.servlet.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=nacos
db.password=nacos
nacos.naming.empty-service.auto-clean=true
nacos.naming.empty-service.clean.initial-delay-ms=50000
nacos.naming.empty-service.clean.period-time-ms=30000
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
server.tomcat.basedir=
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=nacos
### If turn on auth system:
nacos.core.auth.enabled=false
### The token expiration in seconds:
nacos.core.auth.default.token.expire.seconds=18000
### The default token:
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=true
#*************** Istio Related Configurations ***************#
### If turn on the MCP server:
nacos.istio.mcp.server.enabled=false
注意:修改上面的文件后,需要实现自己创建一张nacos的数据库表,并执行其conf文件夹下的nacos-mysql.sql数据库脚本,比如笔者本机:E:\bigdata\nacos-server-1.3.2\nacos\conf\nacos-mysql.sql
执行完SQL语句后,其会有下面的表格出现:
笔者添加一个用户devops,这个时候,我们在数据库里面就能看到这个新添加的用户了。
3. Nacos与SpringCloud的集成
在其官方网站 提供了详细的步骤。
而且其官方网站也提供了一个Github的代码的地址。
4. 总结
本文主要和大家一起回顾了一下,为什么选择Nacos产品作为注册中心,因为其不仅仅提供了注册中心的功能,也提供了配置管理中心的功能,而且其产品也在生产环境中得到验证。其管理界面的功能也做的非常的好,还自带权限管理功能。笔者强烈推荐大家,可以在自己的项目尝试用一下~
参考文献:
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
https://blog.csdn.net/fly910905/article/details/100023415
http://www.hollischuang.com/archives/672