Load Balancing and Advanced Configuration in Nacos Service Discovery

Nacos clusters organize microservices into logical groups, defaulting to the DEFAULT cluster. Adjust this behavior via the clusterName property in your configuration file:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        clusterName: SH

Services in the same Nacos cluster receive priority when handling cross-instance calls to the same microservice. For load balancing with in a single cluster, configure rules under each target service name in your application's application.yml:

# Customize load balancing rule for ordersvc
ordersvc:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule

NacosRule follows a random distribution strategy by default. For example, 6 test requests might return 4 responses from one instance and 2 from another. To fine-tune this, adjust instance weights (0–1) in the Nacos console: higher values mean more frequent calls, and a weight of 0 disables traffic entirely.

Nacos also supports environment isolation through namespaces. Only services registered in the same namespace can communicate. Configure your namespace in application.yml:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        clusterName: SH
        namespace: dev

All services start as ephemeral instances by default, where health status relies on active client-side heartbeats. Nacos automatically removes unhealthy ephemeral instances after 30 seconds. To create persistent instances, set ephemeral to false. Persistent instances use server-side health checks and stay registered evenif offline:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        clusterName: SH
        ephemeral: false

To switch an existing instance from ephemeral to persistent, shut down both the Nacos server and the client, delete the relevant instance metadata file under Nacos' data directory, and restart both components.

Tags: Nacos microservices Load Balancing Spring Cloud Environment Isolation

Posted on Thu, 07 May 2026 17:41:58 +0000 by Jyotsna