krakend / krakend-ce

KrakenD Community Edition: High-performance, stateless, declarative, API Gateway written in Go.
https://www.krakend.io
Apache License 2.0
1.94k stars 453 forks source link

Pub-Sub Backend Integration - Not able to get the environment variable NATS_SERVER_URL #886

Closed viktorrobin closed 3 months ago

viktorrobin commented 3 months ago

Hello all! I've been using Krakend and loving it. I just face a little issue with configuring pubsub Nats as backend.

Environment info:

Describe the bug I have been following the instructionw from this page to set up NATS as a backend for PubSub. However, at startup, Krakend does not seem to be able to find my environment variable NATS_SERVER_URL:

export NATS_SERVER_URL=127.0.0.1:4222

Your configuration file:

{
    "$schema": "https://www.krakend.io/schema/krakend.json",
    "version": 3,
    "name": "API Gateway",
    "extra_config": {
        "telemetry/logging": {
            "level": "DEBUG",
            "prefix": "[KRAKEND]",
            "syslog": false,
            "stdout": true
        }
    },
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "port": 1304,
    "endpoints": [
        {
            "endpoint": "/v1/backends/pubsub",
            "method": "POST",
            "backend": [
                {
                    "host": [
                        "nats://"
                    ],
                    "disable_host_sanitize": true,
                    "extra_config": {
                        "backend/pubsub/publisher": {
                            "topic_url": "Storage.blobCreated"
                        }
                    }
                }
            ]
        }
    ]
}

Commands used

sudo systemctl start krakend.service

with

~ sudo vim /lib/systemd/system/krakend.service

"/lib/systemd/system/krakend.service" 22L, 645B                                                                                                                                                                                                                          8,10          All
[Unit]
Description=Krakend API Gateway
Documentation=http://krakend.io
After=network.target

[Service]
User=krakend
ExecStart=/usr/bin/krakend run -c /media/ssd/config/krakend/krakend.json
ExecStop=/bin/kill -s TERM $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
KillMode=process

[Install]
WantedBy=multi-user.target

Expected behavior When making the following call

curl --location 'https://api.myservice.io/v1/backends/pubsub' \
--header 'Content-Type: application/json' \
--data '{"msg: "Message from REST"}'

I would expect this:

 ~  nats sub "Storage.BlobCreated"                                                                                                                                      ✔  
21:24:37 Subscribing on Storage.BlobCreated
[#1] Received on "Storage.BlobCreated"
{"msg": "Message from REST"}

Logs

~ sudo systemctl status krakend.service
...
ERROR [BACKEND: nats://Storage.blobCreated][PubSub]%!(EXTRA string=open topic nats://Storage.blobCreated: failed to open default connection: NATS_SERVER_URL environment variable not set)
...

Additional context I don't face this problem with RabbitMQ

viktorrobin commented 3 months ago

It is an issue with the environment variable - when running with docker, this problem does not occur.

I have tried all the various ways to set up environment variables but none seems to be picked up. I don't know if it is a bug or a config issue from my side, but closing the bug.