Backend Development 13 min read

Implementing a Lightweight Service Registry with Eureka for Task Distribution

This article walks through building a lightweight service registry using Eureka Server and Client to enable task distribution and execution, covering requirements, integration steps, code examples, load balancing, and troubleshooting, while avoiding separate deployment of a full registration center.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Implementing a Lightweight Service Registry with Eureka for Task Distribution

Requirement Description

A manager asked for a clear requirement: two services – a task dispatcher (distribution center) and a task executor. The dispatcher must split tasks and hand them to executors, and both services must support horizontal scaling.

Two services: task distribution center, task executor. The distribution center is responsible for task splitting and then dispatches the split tasks to executors; executors execute the tasks. Both the distribution center and the executor must support horizontal node expansion.

The initial suggestion was to introduce a registration center, but the manager insisted that due to limited hardware resources and deployment complexity, a separate registration center could not be deployed.

"We want the horse to run without eating grass – how can we achieve that?"

Thus the idea emerged: let the task distribution center also act as a registration center, and let executors automatically register to it.

Implementation

Typical registration centers such as Zookeeper , Nacos , etcd , Consul , and Eureka all require separate deployment. The goal here is to implement a simplified registration center without a separate deployment.

Service discovery – the registry stores and manages addresses of all available services. Service registration – providers register their address to the registry. Health check – the registry periodically checks service health and removes unavailable instances. Horizontal expansion – the registry must support scaling, synchronize service lists across nodes, and maintain consistency.

Integrating Eureka Server into the Distribution Center

Among the mainstream registries, Eureka is familiar and relatively simple. By embedding Eureka Server into the distribution center, we can avoid a separate deployment.

Can the task distribution center integrate an Eureka Server ?

The Eureka Server is a regular web service; it should only handle registration responsibilities and not be coupled with business logic. In this scenario there is only one service to register – the task executor – and the number of executor nodes is small (around ten), so the load on Eureka is minimal.

Integrating Eureka Client into the Distribution Center

To demonstrate the integration, the distribution center also acts as an Eureka Client to fetch executor instances.

1. Add Dependency

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. Enable Eureka Server, Eureka Client and OpenFeign

@SpringBootApplication
@EnableEurekaServer
@EnableEurekaClient
@EnableFeignClients
public class TaskDispatcherApplication {
    public static void main(String[] args) {
        SpringApplication.run(TaskDispatcherApplication.class, args);
    }
}

3. Implement TaskExecutorService for load‑balanced calls

@FeignClient("TASK-EXECUTOR")
public interface TaskExecutorService {
    @GetMapping("task/exec")
    String exec(@RequestParam("taskId") Long taskId);
}
@GetMapping("dispatch")
public String dispatch(Long jobId) {
    LOGGER.info("Received job [jobId={}] execution request", jobId);
    LOGGER.info("Job [jobId={}] splitting tasks...", jobId);
    List
taskIds = Arrays.asList(123L, 666L, 888L, 999L);
    LOGGER.info("Job [jobId={}] split completed, task list: {}", jobId, taskIds);
    for (Long taskId : taskIds) {
        String execResult = taskExecutorService.exec(taskId);
        LOGGER.info("Task [{}] execution result: {}", taskId, execResult);
    }
    return "success";
}

After starting task-dispatcher and task-executor , calling http://192.168.2.10:8080/dispatcher/job/dispatch?jobId=689 initially returned a 500 error because the load balancer could not find any TASK-EXECUTOR instances – the client configuration had fetch-registry=false . Enabling the registry and restarting both services resolved the issue, and the call returned success .

Task Dispatcher Logs

2024-06-30 10:51:51.653|INFO|...|收到作业[jobId=689]执行请求
2024-06-30 10:51:51.653|INFO|...|作业[jobId=689]拆分任务中...
2024-06-30 10:51:51.653|INFO|...|作业[jobId=689]拆分完成,得到作业列表[[123, 666, 888, 999]]
2024-06-30 10:51:51.657|INFO|...|任务[123]执行结果:success
... (similar lines for other tasks)

Task Executor Logs

2024-06-30 10:51:51.656|INFO|...|收到任务[taskId=123]执行请求
2024-06-30 10:51:51.656|INFO|...|任务[taskId=123]执行中...
2024-06-30 10:51:51.656|INFO|...|任务[taskId=123]执行完成
... (similar lines for other tasks)

Running a second instance of task-executor demonstrated load balancing across instances.

Summary

It is recommended to deploy a registration center separately from business code.

Eureka Server can also act as a Eureka Client ; although not ideal, it can be a quick solution.

Experimentation is essential – you won’t know what works until you try.

Reference

gitee.com/youzhibing/…: https://gitee.com/youzhibing/qsl-project/tree/master/integrate-eureka

JavaMicroservicesLoad BalancingService DiscoveryEurekaSpring Cloud
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.