spring-cloud-alibaba-nocas


Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

Nacos

前言

Nacos

从上图不难看出阿里巴巴的野心,一个Nacos干掉了Spring Cloud的三大组件,分别是注册中心Eureka服务配置Config服务总线Bus

启动Nocas

Nocas下载地址:https://github.com/alibaba/nacos/tags

下载完成之后直接解压即可,从它的目录结构和文件名称一看这就是一个Spring Boot 项目。

进入/bin目录,有两个脚本,如下:

  • startup.cmd:windows平台的启动脚本
  • startup.sh:Linux平台的启动脚本

startup.cmd中配置的启动参数

# standalone代表单机模式运行
if %MODE% == "standalone" (
    set "JAVA_OPT=%JAVA_OPT% -Xms512m -Xmx512m -Xmn256m"
    set "JAVA_OPT=%JAVA_OPT% -Dnacos.standalone=true"
) else (
    set "JAVA_OPT=%JAVA_OPT% -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
    set "JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%BASE_DIR%\logs\java_heapdump.hprof"
    set "JAVA_OPT=%JAVA_OPT% -XX:-UseLargePages"
)

由于作者本地是windows,直接双击startup.cmd启动项目,出现以下界面则启动完成:


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

2022-06-13 00:48:19,027 INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$cbd40a56] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2022-06-13 00:48:19,113 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)

2022-06-13 00:48:19,115 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@1a245833' 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-06-13 00:48:19,118 INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$f0a8ad08] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2022-06-13 00:48:19,123 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-06-13 00:48:20,160 INFO Tomcat initialized with port(s): 8848 (http)

2022-06-13 00:48:20,303 INFO Root WebApplicationContext: initialization completed in 3139 ms

2022-06-13 00:48:24,089 INFO Initializing ExecutorService 'applicationTaskExecutor'

2022-06-13 00:48:24,308 INFO Adding welcome page: class path resource [static/index.html]

2022-06-13 00:48:24,643 INFO Creating filter chain: Ant [pattern='/**'], []

2022-06-13 00:48:24,683 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7a8fa663, org.springframework.security.web.context.SecurityContextPersistenceFilter@f0e995e, org.springframework.security.web.header.HeaderWriterFilter@4f2c9ba6, org.springframework.security.web.csrf.CsrfFilter@2bef51f2, org.springframework.security.web.authentication.logout.LogoutFilter@50fe837a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@73db4768, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@150ab4ed, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5ce33a58, org.springframework.security.web.session.SessionManagementFilter@53f48368, org.springframework.security.web.access.ExceptionTranslationFilter@30f5a68a]

2022-06-13 00:48:24,789 INFO Exposing 2 endpoint(s) beneath base path '/actuator'

2022-06-13 00:48:24,822 INFO Initializing ExecutorService 'taskScheduler'

2022-06-13 00:48:24,939 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2022-06-13 00:48:24,950 INFO Nacos logs files: E:\Springcloud\nacos\logs\

2022-06-13 00:48:24,950 INFO Nacos conf files: E:\Springcloud\nacos\conf\

2022-06-13 00:48:24,950 INFO Nacos data files: E:\Springcloud\nacos\data\

2022-06-13 00:48:24,951 INFO Nacos started successfully in stand alone mode.

2022-06-13 00:48:25,078 INFO Initializing Servlet 'dispatcherServlet'

2022-06-13 00:48:25,092 INFO Completed initialization in 14 ms

在浏览器输入http://localhost:8848/nacos进入Nacos的登录界面。

用户名:nacos;密码:nacos

登录成功的界面如下:

登陆成功

服务注册与发现

微服务的服务注册和发现相信都用过Eureka,要自己本地构建一个Eureka微服务,但是整合了Alibaba的Nacos则不用那么复杂,直接启动Alibaba提供的Nacos服务即可,这样让程序员把全部精力放在业务上,下面是一个简单的架构图

服务的注册与发现

参照上面架构图,分别创建了两个模块,分别是nacos-provider(服务提供者)、nacos-consumer(服务消费者),职责如下:

  • nacos-provider:注册进入nacos-server,对外暴露服务
  • nacos-consumer:注册进入nacos-server,调用nacos-provider的服务

nacos-provider

添加Maven依赖

nacos-consumer

文档

五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强?

对于初学者当然是官方文档了,下面作者列出了Nacos相关的官方文档:

问题

@RefreshScope原理


文章作者: WangQingLei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 WangQingLei !
  目录