Search code examples
dockerpostmanfiwarefiware-orion

Dockerize FIWARE can't notify a service


I just started to use FIWARE. I downloaded the latest version on the website (v2) using docker-compose on a PopOs distro. I'm using Postman to make requests (create the entities and subscriptions) and a Laravel application to listen the notification from the FIWARE subscriptions. But for some reason, today, when I started the docker service and start to send requests: the FIWARE notifications suddenly stopped to work.

When I access the subscriptions endpoint FIWARE returns:

        "notification": {
            "timesSent": 1,
            "lastNotification": "2021-09-02T01:19:39.000Z",
            "attrs": [],
            "onlyChangedAttrs": false,
            "attrsFormat": "keyValues",
            "http": {
                "url": "http://localhost:8000/api/notifications"
            },
            "lastFailure": "2021-09-02T01:19:39.000Z",
            "lastFailureReason": "Couldn't connect to server"
        }

FIWARE can't comunicate, but if I make a POST request using Postman for that endpoint (http://localhost:8000/api/notifications) it returns 200.

There's some aditional configuration between the FIWARE docker container and the local machine? Or I'm doing something wrong?

This is my entity:

// http://{{orion}}/v2/subscription

{
    "id": "movie",
    "type": "movie",
    "name": {
        "type": "text",
        "value": "movie name"
    },
    "gender": {
        "type": "text",
        "value": "drama"
    }
}

This is how I'm doing the subscription:

// http://{{orion}}/v2/subscriptions

{
    "description": "Notify me about any movie of gender drama",
    "subject": {
        "entities": [{"idPattern": ".*","type": "movie"}],
        "condition": {
            "attrs": ["gender"],
            "expression": {
                "q": "gender==drama"
            }
        }
    },
    "notification": {
        "http": {
            "url": "http://127.0.0.1:8000/api/notifications"
        }
    }
}

Solution

  • If you are using Docker, then you need to consider what http://localhost:8000/api/notifications actually means. localhost will mean the localhost as experienced by the Orion container itself. Generally Orion listens on 1026 and there is nothing listening on 8000 within a dockerized Orion, therefore your subscription fails.

    If you have another micro-service running within the same docker network and in a separate container you must use the hostname of that container (or an alias or defined IP) to describe the notification URL, not localhost.

    So for example in the following tutorial where a subscription payload is displayed on screen:

    curl -iX POST \
      --url 'http://localhost:1026/v2/subscriptions' \
      --header 'content-type: application/json' \
      --data '{
      "description": "Notify me of all product price changes",
      "subject": {
        "entities": [{"idPattern": ".*", "type": "Product"}],
        "condition": {
          "attrs": [ "price" ]
        }
      },
      "notification": {
        "http": {
          "url": "http://tutorial:3000/subscription/price-change"
        }
      }
    }'
    

    refers to a container which is called tutorial within the docker network

      tutorial:
        image: fiware/tutorials.context-provider
        hostname: tutorial
        container_name: fiware-tutorial
        depends_on:
          - orion
        networks:
          default:
            aliases:
              - iot-sensors
              - context-provider
        expose:
          - 3000
    

    As it happens the tutorial container is also exposing its internal port 3000 to the localhost of the machine it is running on so it can be viewed by a user, but Orion can only access it via the hostname on the docker network.