Cloud Native 5 min read

Implement Zookeeper Service Discovery and Load Balancing with Spring Cloud

This guide walks through configuring Spring Boot 2.3.9 with Spring Cloud Hoxton to register services in Zookeeper, set up provider and consumer applications, and achieve client‑side load balancing using Spring Cloud LoadBalancer.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Implement Zookeeper Service Discovery and Load Balancing with Spring Cloud

Environment: Spring Boot 2.3.9 + Spring Cloud Hoxton.SR8.

Reference Zookeeper installation and configuration (see related tutorial).

Service Provider

Dependency

<properties>
  <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  </dependency>
</dependencies>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

bootstrap.yml configuration

spring:
  cloud:
    zookeeper:
      connectString: localhost:2181,localhost:2182,localhost:2183
      connectionTimeout: 3000

This config connects the application to Zookeeper on startup.

application.yml configuration

server:
  port: 8081
---
spring:
  application:
    name: zk-client
---
spring:
  cloud:
    loadbalancer:
      ribbon:
        enabled: false

Ribbon is disabled because it is deprecated; Spring Cloud LoadBalancer replaces it.

Test endpoint

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Value("${server.port}")
    private Integer port;
    @GetMapping("")
    public String serviceUrl() {
        return "我的端口是" + port;
    }
}

Two instances are started on ports 8080 and 8081. After launch, both register in Zookeeper (see screenshots).

Service Consumer

Dependencies and configuration

Same as provider except spring.application.name=zk-consumer.

RestTemplate configuration

@Configuration
public class WebConfig {
    @Resource
    private RestTemplateBuilder builder;
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return builder.build();
    }
}

The @LoadBalanced annotation enables client‑side load balancing.

Test endpoint to list provider instances

@RestController
@RequestMapping("/balanced")
public class BalancedController {
    @Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/get")
    public Object serviceUrl() {
        List<ServiceInstance> list = discoveryClient.getInstances("zk-client");
        return list;
    }
}

This endpoint returns all instances of the service named zk-client.

Invoke provider via RestTemplate

@Resource
private RestTemplate restTemplate;
@GetMapping("/producer")
public Object invokeProducer() {
    return restTemplate.getForObject("http://zk-client/producer", String.class);
}

The host part uses the service name ( zk-client) instead of a fixed URL, allowing load balancing.

Running the consumer shows alternating responses from ports 8080 and 8081, confirming successful load balancing.

All steps complete.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend Developmentload balancingservice discoveryZooKeeperSpring BootSpring Cloud
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.