Cloud Native 10 min read

Seamlessly Migrate Consul Services to Nacos with Nacos‑Sync: A Step‑by‑Step Guide

This article explains why HashiCorp's restrictions on its enterprise products in China prompt a migration from Consul to Nacos, compares dual‑registration and Nacos‑Sync approaches, and provides a complete Docker‑based demo showing how to configure, run, and verify bidirectional service synchronization using Nacos‑Sync in a Spring Cloud environment.

Programmer DD
Programmer DD
Programmer DD
Seamlessly Migrate Consul Services to Nacos with Nacos‑Sync: A Step‑by‑Step Guide

Background

HashiCorp announced that its enterprise products cannot be used, deployed or installed in the People’s Republic of China. Although the notice was later clarified to affect only Vault, companies using Consul should prepare for possible restrictions.

Migration Options

Option 1 – Dual Registration

During the transition, services are registered simultaneously to Consul and Nacos. After migration, Consul registration is removed.

Option 2 – Nacos‑Sync

Nacos‑Sync is a cross‑registry synchronization component that supports service sync and migration among Zookeeper, Eureka, Consul, and Nacos.

The second option has lower intrusion and allows one‑time migration, which we recommend.

Demo Overview

The demo shows how to use Nacos‑Sync to migrate Consul‑registered services to Nacos in a Spring Cloud environment.

Environment Preparation

All middleware services run as Docker containers.

Start Consul Cluster

# Start first Consul server (maps container port 8500 to host 8900 and enables UI)
docker run -d --name=consul1 -p 8900:8500 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --bootstrap-expect=3 --client=0.0.0.0 -ui

# Get the IP of the first container
docker exec -it consul1 cat /etc/hosts | grep 172

# Start second server and join the cluster
docker run -d --name=consul2 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --client=0.0.0.0 --join <first_container_ip>

# Start third server and join the cluster
docker run -d --name=consul3 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=true --client=0.0.0.0 --join <first_container_ip>

# Start a client node and join the cluster
docker run -d --name=consul4 -e CONSUL_BIND_INTERFACE=eth0 consul agent --server=false --client=0.0.0.0 --join <first_container_ip>

Start Nacos Server

docker run --name nacos-standalone -e MODE=standalone -p 8848:8848 -d nacos/nacos-server:1.2.1

Start MySQL for Nacos‑Sync

docker run --name nacos-sync-db -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nacos_sync -p 3306:3306 -d mysql:5.7

Start Nacos‑Sync

# Edit conf/application.properties (example)
server.port=8081
server.servlet.context-path=/
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/nacos_sync?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

Launch Demo Applications

Two Spring Cloud applications are created: one registers services to Consul, the other to Nacos. Controllers expose endpoints that call each other via Feign.

Consul Provider

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello World from Consul!";
    }

    @Autowired
    private HelloService helloService;

    @GetMapping("/nacos-hello")
    public String hello2() {
        return helloService.hello();
    }
}

Nacos Provider

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;

    @GetMapping("/consul-hello")
    public String hello() {
        return helloService.hello();
    }

    @GetMapping("/hello")
    public String hello2() {
        return "Hello World from Nacos!";
    }
}

Configure Nacos‑Sync

Open http://localhost:8081, add a new cluster for Consul (host and port) and another for Nacos (host and port). Then create a synchronization task that maps Consul services to Nacos.

Verification

After starting the sync task, the services appear in both the Nacos console ( http://localhost:8848/nacos) and the Consul UI ( http://localhost:8900/ui/dc1/services). Accessing http://localhost:18089/hello returns the Consul response, while http://localhost:18088/hello returns the Nacos response, confirming successful bidirectional sync.

Conclusion

Nacos‑Sync enables smooth, bidirectional synchronization between Consul and Nacos for Spring Cloud applications, providing a reliable path for migrating away from Consul. Future work will add batch migration capabilities.

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.

migrationDockerNacosConsulSpring CloudNacos‑Sync
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.