How to Synchronize Nacos Services to CoSky Using CoSky-Mirror
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:
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:
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 to enable the bridge and define synchronization directions:
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:
# 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:
@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:
- Enable the Mirror with
cosky.mirror.nacos.sync-to-cosky: trueinbootstrap.yaml - Gradually replace Nacos clients with CoSky clients in your services
- 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.ktsand inclusion of theme.ahoo.cosky:cosky-mirrordependency - Configuration in
bootstrap.yamlcontrols unidirectional or bidirectional sync viasync-to-coskyandsync-to-nacosflags - The architecture translates Nacos events into CoSky
ServiceRegistry.register()andderegister()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.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →