0
点赞
收藏
分享

微信扫一扫

nacos使用

楚木巽 2021-09-28 阅读 32

1. 什么是Nacos

Nacos 支持基于 DNS 和基于 RPC 的服务发现(可以作为springcloud的注册中心)、动态配置服务(可以做配置中心)、动态 DNS 服务。

官方介绍是这样的:

2. Nacos与Eureka有什么区别

分布式CAP理论告诉我们,CP 和 AP不可能同时满足。

相比与Eureka: (1)Nacos具备服务优雅上下线和流量管理(API+后台管理页面),而Eureka的后台页面仅供展示,需要使用api操作上下线且不具备流量管理功能。

(2)从部署来看,Nacos整合了注册中心、配置中心功能,把原来两套集群整合成一套,简化了部署维护

(3)从长远来看,Eureka开源工作已停止,后续不再有更新和维护,而Nacos在以后的版本会支SpringCLoud+Kubernetes的组合,填补 2 者的鸿沟,在两套体系下可以采用同一套服务发现和配置管理的解决方案,这将大大的简化使用和维护的成本。同时来说,Nacos 计划实现 Service Mesh,是未来微服务的趋势

(4)从伸缩性和扩展性来看Nacos支持跨注册中心同步,而Eureka不支持,且在伸缩扩容方面,Nacos比Eureka更(nacos支持大数量级的集群)。

(5)Nacos具有分组隔离功能,一套Nacos集群可以支撑多项目、多环境。

阿里nacos异常情况 leader挂了

1.不影响服务之间互相调用

2.不影响服务注册

3.不影响服务正常启动拉取配置文件

4.选举新leader差不多4,5秒钟

3. Nacos 与 SpringCloud

3.1 作为配置中心

下载nacos-server

https://github.com/alibaba/nacos/releases

启动过程参考官方文档:https://nacos.io/zh-cn/docs/quick-start.html

启动后登录: http://127.0.0.1:8848/nacos/index.html 用户名:nacos 密码:nacos

集成在spingcloud项目中,引入pom

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
​
<dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
         <version>0.2.2.RELEASE</version>
</dependency>

bootstrap.yaml如下:

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        shared-dataids: nacos-config.yaml,spm-nacos-config-develop.yaml,spm-nacos-config-prod.yaml
        refreshable-dataids: nacos-config.yaml,spm-nacos-config-develop.yaml,spm-nacos-config-prod.yaml
  application:
    name: spm-nacos-config
  profiles:
    active: prod
management:
  endpoints:
    web:
      exposure:
        exclude: "*"

在nacos控制台添加一个配置,参考官方文档,然后启动项目
启动控制会看到:in develop-env enviroment; user name :test; age: 26 已经从nacos拿到了配置

2019-11-13 11:48:25.608  INFO 32602 --- [           main] c.h.s.config.SpmNacosConfigApplication   : Started SpmNacosConfigApplication in 3.133 seconds (JVM running for 3.72)
in develop-env enviroment; user name :test; age: 26

nacos支持多环境配置 例如:dev,test,prod

3.2 作为注册中心
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
 </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>![image-20191113114308300.png](https://upload-images.jianshu.io/upload_images/11541995-cc7b771a6c2026dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

启动类添加注解:@EnableDiscoveryClient

nacos端点:http://127.0.0.1:8082/actuator/nacos-discovery

启动服务查看nacos控制界面,我本地启动了两个服务

举报

相关推荐

0 条评论