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的集群、负载均衡、环境隔离

相关推荐
SL_staff10 小时前
私有化低代码不是减配版开发:JVS双模式建模如何重建IT交付信任
java·低代码·spring cloud
SL_staff1 天前
JVS-APS的7个刚性约束设计:从无限产能幻觉到可执行排产的工程实践
java·spring cloud·全栈
利威尔xu2 天前
Go IM 源码实战(14):本地缓存快 3000 倍,失效却难 10 倍
后端·缓存·微服务·面试·golang
逃逸线LOF2 天前
Ribbon负载均衡原理(Irule组件、源码分析)
后端·spring cloud·ribbon
利威尔xu2 天前
Go IM 源码实战(13):30 个会话只有 2 个红点,未读数错在三条路径
redis·后端·微服务·面试·golang
n112122 天前
Ollama 接 Open WebUI 中文界面:我踩的 4 个坑
云原生·eureka·ollama·本地大模型·open webui·私有部署·中文界面
骑着蜗牛撵大象3273 天前
Spring Boot 4.1 + Spring AI 2.0:RAG 供文、图谱供关系、MCP 供工具的 Java Agent 平台
java·spring boot·spring·微服务·知识图谱·rag·mcp
code_slave(码畜)3 天前
微服务架构落地:慢 SQL 治理与连接池调优 —— 守住数据库的最后一道防线
数据库·spring boot·sql·spring cloud·微服务·架构
水无痕simon4 天前
4、Eureka服务端安装
云原生·eureka
2501_933923254 天前
SpringCloud:OpenFeign快速实现
java·后端·spring cloud·openfeign