Cloud Native 8 min read

Master Spring Cloud Consul: From Service Registry to Dynamic Config Center

This guide walks you through installing Consul, registering Spring Boot services, enabling client-side load balancing, using Consul as a configuration center, and leveraging its dynamic refresh capabilities, complete with code snippets and configuration examples for a full cloud-native microservices setup.

macrozheng
macrozheng
macrozheng
Master Spring Cloud Consul: From Service Registry to Dynamic Config Center

Consul Overview

Consul is an open‑source tool from HashiCorp that provides service discovery, configuration, and control bus capabilities for microservice systems. Each feature can be used independently or together to build a full service mesh.

Features of Spring Cloud Consul

Service discovery: applications register themselves with Consul and can query other services.

Client‑side load balancing: integrates with Ribbon and Spring Cloud LoadBalancer.

Zuul integration: gateway can discover services from Consul.

Distributed configuration: Consul stores configuration as key‑value pairs.

Control bus: events can be broadcast across the microservice system.

Using Consul as a Service Registry

Install and Run Consul

Download Consul from the official website.

Run the executable; check the version with consul --version.

Start Consul in development mode: consul agent -dev.

Access the UI at http://localhost:8500.

Create Services and Register to Consul

We modify user‑service and ribbon‑service to replace Eureka with Consul.

Create consul-user-service and consul-ribbon-service modules.

Replace Eureka dependencies with spring-cloud-starter-consul-discovery and add Spring Boot Actuator.

Update application.yml to configure Consul host, port, and service name.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
server:
  port: 8206
spring:
  application:
    name: consul-user-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

Load Balancing Demo

Two instances of consul‑user‑service are called by consul‑ribbon‑service.

Repeated calls to http://localhost:8308/user/1 show alternating logs from the two services, demonstrating client‑side load balancing.

2019-10-20 10:39:32.580  INFO 12428 --- [io-8206-exec-10] c.macro.cloud.controller.UserController  : 根据id获取用户信息,用户名称为:macro

Using Consul as a Configuration Center

We create a consul-config-client module and store configuration in Consul.

Create consul‑config‑client Module

Add spring-cloud-starter-consul-config and spring-cloud-starter-consul-discovery dependencies.

Set spring.profiles.active=dev in application.yml.

Configure Consul connection and enable the config center in bootstrap.yml.

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: consul-config-client
      config:
        enabled: true
        format: yaml
        prefix: config
        profile-separator: ':'
        data-key: data

ConfigClientController

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

Add Configuration to Consul

Key: config/consul-config-client:dev/data Value:

config:
  info: "config info for dev"

Run the client and request http://localhost:9101/configInfo to see the refreshed configuration.

Dynamic Refresh with Consul

Modifying the value in Consul automatically refreshes the configuration without needing Spring Cloud Bus, because Consul’s built‑in control bus propagates the change.

Modules Used

springcloud-learning
├── consul-config-client   # Consul config client demo
├── consul-user-service    # User CRUD service registered to Consul
└── consul-service         # Ribbon service calling the user service

Source Code

https://github.com/macrozheng/springcloud-learning

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.

service discoveryConsulConfiguration Center
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.