开发指南

这个章节是关于如何基于实现微服务发现功能

Micro-service注册

  1. curl -X POST \
  2. http://127.0.0.1:30100/registry/v3/microservices \
  3. -H 'content-type: application/json' \
  4. -H 'x-domain-name: default' \
  5. -d '{
  6. "service":
  7. {
  8. "appId": "default",
  9. "serviceName": "DemoService",
  10. "version":"1.0.0"
  11. }
  12. }'

这时候你可以额获取'DemoService'的ID,如下:

  1. {
  2. "serviceId": "a3fae679211211e8a831286ed488fc1b"
  3. }

实例注册

标记micro-service ID并调用实例注册API,根据Service CeCube定义:一个进程应该注册一个实例。

  1. curl -X POST \
  2. http://127.0.0.1:30100/registry/v3/microservices/a3fae679211211e8a831286ed488fc1b/instances \
  3. -H 'content-type: application/json' \
  4. -H 'x-domain-name: default' \
  5. -d '{
  6. "instance":
  7. {
  8. "hostName":"demo-pc",
  9. "endpoints": [
  10. "rest://127.0.0.1:8080"
  11. ]
  12. }
  13. }'

响应成功如下:

  1. {
  2. "instanceId": "288ad703211311e8a831286ed488fc1b"
  3. }

如果全部成功,意味着你已经完成微服务的注册于实例的发布。

服务发现

下一步是根据服务名称和版本规则的微服务实例发现。

  1. curl -X GET \
  2. 'http://127.0.0.1:30100/registry/v3/instances?appId=default&serviceName=DemoService&version=latest' \
  3. -H 'content-type: application/json' \
  4. -H 'x-consumerid: a3fae679211211e8a831286ed488fc1b' \
  5. -H 'x-domain-name: default'

这里,你可以从响应中获取信息。

  1. {
  2. "instances": [
  3. {
  4. "instanceId": "b4c9e57f211311e8a831286ed488fc1b",
  5. "serviceId": "a3fae679211211e8a831286ed488fc1b",
  6. "version": "1.0.0",
  7. "hostName": "demo-pc",
  8. "endpoints": [
  9. "rest://127.0.0.1:8080"
  10. ],
  11. "status": "UP",
  12. "healthCheck": {
  13. "mode": "push",
  14. "interval": 30,
  15. "times": 3
  16. },
  17. "timestamp": "1520322915",
  18. "modTimestamp": "1520322915"
  19. }
  20. ]
  21. }