# How to Synchronize Nacos Services to CoSky Using CoSky-Mirror

> Synchronize Nacos services to CoSky with CoSky-Mirror. This lightweight bridge ensures real-time replication for hybrid service discovery and seamless migration. Learn how now!

- Repository: [Ahoo Wang/cosky](https://github.com/ahoo-wang/cosky)
- Tags: how-to-guide
- Published: 2026-02-23

---

**CoSky-Mirror is a lightweight bridge that listens to Nacos service-registry events and replicates them to CoSky in real-time, enabling hybrid service discovery and zero-downtime migration from Nacos to CoSky.**

CoSky is a Redis-backed service registry and configuration center optimized for high-performance microservices. When migrating from Alibaba Nacos or operating a hybrid infrastructure, the CoSky-Mirror module—located in the `ahoo-wang/cosky` repository—provides seamless synchronization between both registries without requiring changes to existing service registration logic.

## Architecture of CoSky-Mirror

The Mirror module operates as a stateless translation layer between Nacos and CoSky. According to the CoSky source code, the architecture consists of four core components working in concert to maintain registry consistency.

### Event Listening and Translation

The Nacos client subscribes to service-instance change events (add, update, delete) through the built-in Nacos SDK. When changes occur in Nacos, the CoSky-Mirror core translates these events into calls to `ServiceRegistry.register()` or `ServiceRegistry.deregister()` in the CoSky client.

### Registry Integration

CoSky maintains a Redis-backed registry with Pub/Sub updates, while Nacos relies on its built-in cache. The Mirror does not maintain additional state between the two systems, ensuring negligible latency overhead during synchronization. As implemented in the `cosky-mirror` module, the bridge immediately propagates changes without persisting intermediate data.

## Configuring CoSky-Mirror

### Activate the Gradle Module

The Mirror module is excluded from the default build. Uncomment the following line in `settings.gradle.kts` to include the bridge in your project:

```kotlin
include(":cosky-mirror")

```

This change activates the module referenced at line 26 of the project's `settings.gradle.kts`.

### Add the Maven Dependency

Include the Mirror artifact in your build configuration using the Gradle Kotlin DSL:

```kotlin
implementation("me.ahoo.cosky:cosky-mirror:${coskyVersion}")

```

Replace `${coskyVersion}` with the latest release version available in Maven Central.

### Configure Application Bootstrap

Create or modify [`bootstrap.yaml`](https://github.com/ahoo-wang/cosky/blob/main/bootstrap.yaml) to enable the bridge and define synchronization directions:

```yaml
spring:
  cloud:
    nacos:
      server-addr: http://nacos.example.com:8848
      namespace: public
    cosky:
      namespace: ${cosky.namespace:cosky}
cosky:
  mirror:
    enabled: true
    nacos:
      enabled: true
      sync-to-cosky: true
    cosky:
      enabled: true
      sync-to-nacos: false

```

Setting `cosky.mirror.enabled: true` activates the bridge, while the nested `nacos` and `cosky` sections control the direction of synchronization.

## Implementation Examples

### Dual Registration Pattern

Deploy a service that registers with both registries while the Mirror synchronizes third-party services from Nacos:

```yaml

# src/main/resources/bootstrap.yaml

spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: http://nacos:8848
    cosky:
      namespace: cosky-production
cosky:
  mirror:
    enabled: true
    nacos:
      sync-to-cosky: true

```

When the Spring Boot application starts, it registers itself in both Nacos and CoSky, while the Mirror ensures any other instances added to Nacos are instantly visible to CoSky clients.

### Querying Mirrored Services

Once synchronized, discover Nacos-registered services through CoSky's DiscoveryClient without modifying consumer code:

```kotlin
@RestController
class ServiceController(
    private val discoveryClient: DiscoveryClient
) {
    @GetMapping("/services")
    fun getInstances(): List<ServiceInstance> {
        // Returns instances originally registered in Nacos
        return discoveryClient.getInstances("order-service")
    }
}

```

Because the Mirror kept CoSky's cache up-to-date via Redis Pub/Sub, the consumer sees `order-service` instances even though they were originally registered only in Nacos.

### One-Way Migration Strategy

To migrate from Nacos to CoSky without downtime:

1. Enable the Mirror with `cosky.mirror.nacos.sync-to-cosky: true` in [`bootstrap.yaml`](https://github.com/ahoo-wang/cosky/blob/main/bootstrap.yaml)
2. Gradually replace Nacos clients with CoSky clients in your services
3. Maintain the Mirror until all services complete migration, ensuring late-joining Nacos instances remain discoverable

## Summary

- CoSky-Mirror enables real-time synchronization between Nacos and CoSky registries through a stateless bridge architecture
- The module requires activation in `settings.gradle.kts` and inclusion of the `me.ahoo.cosky:cosky-mirror` dependency
- Configuration in [`bootstrap.yaml`](https://github.com/ahoo-wang/cosky/blob/main/bootstrap.yaml) controls unidirectional or bidirectional sync via `sync-to-cosky` and `sync-to-nacos` flags
- The architecture translates Nacos events into CoSky `ServiceRegistry.register()` and `deregister()` calls without maintaining local state
- Supports hybrid deployments and zero-downtime migration scenarios from Nacos to CoSky

## Frequently Asked Questions

### What is CoSky-Mirror and when should I use it?

CoSky-Mirror is a bridge module that synchronizes service registry information between Nacos and CoSky. Use it when gradually migrating from Nacos to CoSky, running both registries side-by-side for high availability, or maintaining compatibility with legacy Nacos services while adopting CoSky's Redis-backed discovery infrastructure.

### Does CoSky-Mirror support bidirectional synchronization?

Yes. While the primary flow synchronizes Nacos to CoSky via `cosky.mirror.nacos.sync-to-cosky: true`, you can enable reverse synchronization by setting `cosky.mirror.cosky.sync-to-nacos: true`. This keeps both registries identical, though unidirectional sync is recommended for migration scenarios to avoid circular update loops.

### How does CoSky-Mirror handle service instance updates?

The Mirror listens to Nacos service-registry events through the Nacos client SDK. When instances are added, removed, or updated in Nacos, the Mirror immediately calls CoSky's `ServiceRegistry.register()` or `ServiceRegistry.deregister()` methods to maintain consistency, leveraging CoSky's Redis Pub/Sub mechanism for real-time propagation to all CoSky clients.

### What is the performance impact of enabling CoSky-Mirror?

The Mirror adds negligible latency because it operates as a stateless translation layer without maintaining local caches or persistent state. It relies on the existing Nacos and CoSky client caches, merely translating events between the two protocols using asynchronous registry calls.