SpringCloud微服务:Nacos和Eureka的区别

目录

配置:

区别:

ephemeral设置为true时

ephemeral设置为false时(这里我使用的服务是order-service)

  1. Nacos与eureka的共同点

都支持服务注册和服务拉取

都支持服务提供者心跳方式做健康检测

  1. Nacos与Eureka的区别

Nacos支持服务端主动检测提供者状态:临时实例采用心跳模式,非临时实例采用主动检测模式

临时实例心跳不正常会被剔除,非临时实例则不会被剔除

Nacos支持服务列表变更的消息推送模式,服务列表更新更及时

Nacos集群默认采用AP方式,当集群中存在非临时实例时,采用CP模式;Eureka采用AP方式

配置:

服务注册到Nacos时,可以选择注册为临时或非临时实例,通过下面的配置来设置:

spring:

cloud :

nacos:

discovery:

ephemeral: false#设置为非临时实例

区别:

当我们 ephemeral设置为true时(默认就是true),把正在运行的服务关闭,nacos网站的服务队列在30秒内就会把该集群删掉

ephemeral设置为true时

java 复制代码
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false
    username: root
    password: 1234
    driver-class-name: com.mysql.jdbc.Driver
  application:
    name: orderservice #order的微服务名称
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ #集群名称
        namespace: 8279562b-ce89-420a-b765-f8b2adfdbe49 #命名空间id
        ephemeral: true #是否为临时实例
mybatis:
  type-aliases-package: cn.itcast.user.pojo
  configuration:
    map-underscore-to-camel-case: true
logging:
  level:
    cn.itcast: debug
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
#eureka:
#  client:
#    service-url: #eureka地址信息1
#      defaultZone: http://127.0.0.1:10086/eureka
userservice:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #负载均衡规则:随机。范围:userservice服务
ribbon:
  eager-load:
    enabled: true #开启饥饿加载
    clients:  #指定饥饿加载的服务
      - userservice

当服务开启状态时:

当服务关闭后30秒内:

集群被剔除了:

ephemeral设置为false时(这里我使用的服务是order-service)

java 复制代码
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false
    username: root
    password: 1234
    driver-class-name: com.mysql.jdbc.Driver
  application:
    name: orderservice #order的微服务名称
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ #集群名称
        namespace: 8279562b-ce89-420a-b765-f8b2adfdbe49 #命名空间id
        ephemeral: false #是否为临时实例
mybatis:
  type-aliases-package: cn.itcast.user.pojo
  configuration:
    map-underscore-to-camel-case: true
logging:
  level:
    cn.itcast: debug
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
#eureka:
#  client:
#    service-url: #eureka地址信息1
#      defaultZone: http://127.0.0.1:10086/eureka
userservice:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #负载均衡规则:随机。范围:userservice服务
ribbon:
  eager-load:
    enabled: true #开启饥饿加载
    clients:  #指定饥饿加载的服务
      - userservice

当服务开启状态时:

当服务关闭后30秒内(非临时变量很快就会有反应):

集群未被剔除:

代码文件点击下载https://pan.baidu.com/s/13bxrC2MIREUIroN-8pFJMQ?pwd=yr6m

上一篇:Nacos的集群、负载均衡、环境隔离

相关推荐
小坏讲微服务38 分钟前
Spring Boot整合Redis注解,实战Redis注解使用
spring boot·redis·分布式·后端·spring cloud·微服务·mybatis
周杰伦_Jay1 小时前
【智能体(Agent)技术深度解析】从架构到实现细节,核心是实现“感知环境→处理信息→决策行动→影响环境”的闭环
人工智能·机器学习·微服务·架构·golang·数据挖掘
眠りたいです3 小时前
基于脚手架微服务的视频点播系统-客户端业务逻辑处理部分(三)-客户端主体部分完结
c++·微服务·云原生·架构·json·restful·qt6.7
qq_316837753 小时前
初步压测的 nginx反向代理 到 Spring Cloud网关 到 Spring Cloud微服务的网络参考配置
nginx·spring cloud·微服务
但要及时清醒5 小时前
spring cloud微服务常用组件
spring·spring cloud·微服务
陈果然DeepVersion5 小时前
Java大厂面试真题:从Spring Boot到AI微服务的三轮技术拷问(二)
spring boot·redis·spring cloud·微服务·ai·java面试·rag
半旧夜夏6 小时前
【Gateway】服务调用和网关配置攻略
java·spring boot·spring cloud·gateway
he___H8 小时前
RabbitMQ 小项目之扫盲班
微服务·springboot
小坏讲微服务10 小时前
Nginx集群与SpringCloud Gateway集成Nacos的配置指南
spring boot·nginx·spring cloud·gateway
shixian103041110 小时前
Dify Docker Compose 安装指南
docker·容器·eureka