Cloud Native 6 min read

How to Register and Deregister Services in Nacos for Dynamic Prometheus Monitoring

This article explains why dynamic service discovery is essential, compares static Prometheus configurations with Nacos‑based discovery, and provides step‑by‑step OpenAPI and command‑line examples for registering and deregistering service instances, enabling a fully automated monitoring loop.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
How to Register and Deregister Services in Nacos for Dynamic Prometheus Monitoring

Why Dynamic Service Discovery?

Static static_configs in Prometheus require manually listing each target IP and port, leading to high maintenance cost, error‑prone updates, lack of real‑time health awareness, and poor scalability in large, dynamic environments.

Static Configuration Drawbacks

High maintenance: every scale‑out, scale‑in or migration needs manual file edits and Prometheus restart.

Prone to mistakes: manual operations can miss or misconfigure targets.

Not real‑time: cannot instantly detect instance health changes.

Hard to scale: infeasible for large, highly dynamic setups.

Register Service Instance via Open API

Use curl to post instance metadata to Nacos:

$ curl -d 'username=jiaxzeng' \
  -d 'password=xxxx' \
  -d 'namespaceId=46f891fb-c1a2-4b67-a0cb-b9d3888ac9be' \
  -d 'serviceName=node-exporter' \
  -d 'ip=172.139.20.177' \
  -d 'port=9100' \
  -d 'weight=1' \
  -d 'ephemeral=false' \
  -X POST 'http://172.139.20.100:8848/nacos/v2/ns/instance'
{ "code":0, "message":"success", "data":"ok" }

To deregister the same instance:

$ curl -d 'username=jiaxzeng' \
  -d 'password=xxx' \
  -d 'namespaceId=46f891fb-c1a2-4b67-a0cb-b9d3888ac9be' \
  -d 'serviceName=node-exporter' \
  -d 'ip=172.139.20.177' \
  -d 'port=9100' \
  -d 'weight=1' \
  -d 'ephemeral=false' \
  -X DELETE 'http://172.139.20.100:8848/nacos/v2/ns/instance'
{ "code":0, "message":"success", "data":"ok" }

Register/Deregister via nacos-service-register Command

First obtain an encrypted password:

$ ./nacos-service-register enc
? Please enter NACOS password: **********
? Please enter NACOS password again: **********
Encrypted password: /retaIkk6uNjaQTdqp5bgg==

Register the instance:

$ ./nacos-service-register register -s http://172.139.20.100:8848/nacos \
  --namespaceId 46f891fb-c1a2-4b67-a0cb-b9d3888ac9be -u jiaxzeng \
  -p /retaIkk6uNjaQTdqp5bgg== \
  --instance '{"ip": "172.139.20.177", "port": 9100, "serviceName": "node-exporter"}'
[INFO] node-exporter service register 172.139.20.177 instance succeed

Deregister the instance:

$ ./nacos-service-register deregister -s http://172.139.20.100:8848/nacos \
  --namespaceId 46f891fb-c1a2-4b67-a0cb-b9d3888ac9be -u jiaxzeng \
  -p /retaIkk6uNjaQTdqp5bgg== \
  --instance '{"ip": "172.139.20.177", "port": 9100, "serviceName": "node-exporter"}'
[INFO] node-exporter service deregister 172.139.20.177 instance succeed

Conclusion

Nacos’s instance registration mechanism, with standardized metadata, flexible SDK/API options, and reliable heartbeat and health checks, provides robust and stable dynamic service discovery for microservice architectures.

Nacos console
Nacos console
service discoveryNacosPrometheusOpenAPIDynamic Monitoring
Linux Ops Smart Journey
Written by

Linux Ops Smart Journey

The operations journey never stops—pursuing excellence endlessly.

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.