How to Build Service Governance with Spring Cloud Eureka and Consul
This guide walks through setting up Spring Cloud, explains microservice architecture, and provides step‑by‑step instructions with code samples for creating a Eureka service registry, a compute service provider, and switching to Consul for service discovery in a cloud‑native Java environment.
Spring Cloud Overview
Spring Cloud is a set of tools built on Spring Boot that simplifies development of cloud‑native JVM applications. It provides configuration management, service discovery, circuit breaking, intelligent routing, distributed sessions, and more through sub‑projects such as Spring Cloud Config, Netflix, Zookeeper, and others.
Microservice Architecture
Microservices split a monolithic application into independently deployable services that communicate via RESTful APIs, allowing each service to be developed, scaled, and maintained separately.
Service Governance with Spring Cloud
Spring Cloud abstracts service‑governance implementations, enabling seamless switching between frameworks like Netflix Eureka, Consul, or Zookeeper without changing registration or discovery code.
Using Spring Cloud Eureka
First, create a Spring Boot project and add the following Maven dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Enable the Eureka server with @EnableEurekaServer on a Spring Boot application class and run it. In application.properties disable the server’s own client registration:
server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/After starting, visiting http://localhost:1111/ shows the Eureka dashboard with no registered services.
Creating a Service Provider
Create another Spring Boot project with the same Eureka dependency and add a REST controller that exposes an /add endpoint. The controller logs the service instance information obtained via DiscoveryClient and returns the sum of the two request parameters.
@RestController
public class ComputeController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
ServiceInstance instance = client.getLocalServiceInstance();
Integer r = a + b;
logger.info("/add, host:" + instance.getHost()
+ ", service_id:" + instance.getServiceId()
+ ", result:" + r);
return r;
}
}Activate discovery on the provider side with @EnableDiscoveryClient and configure:
spring.application.name=compute-service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/Running the provider registers it with Eureka; the dashboard now lists the service.
Spring Cloud Consul
Consul offers service discovery, health checking, a key/value store, and multi‑datacenter support. Switching to Consul only requires changing the Maven dependency to spring-cloud-starter-consul-discovery and adding Consul connection properties:
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500Consul runs its own agent; start it in development mode with consul agent -dev. No separate registration server is needed.
Key Takeaway
Spring Cloud’s abstraction layer lets developers move between Eureka and Consul (or other registries) by swapping dependencies and configuration, without modifying business logic.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
