我正在尝试调用我的无服务器函数,我正在使用 Open-FaaS 无服务器框架的子集 FaasD。我正在使用 Raspbian OS Lite x64 的树莓派 4b 型号上运行它。
我的无服务器函数将“Hello World”作为 MQTT 消息发布,并且我有一个本地订阅者来接收该消息。
这是我的 handler.py 函数:
import paho.mqtt.client as mqtt
# MQTT broker configuration.
BROKER = 'localhost'
PORT = 1883
TOPIC = 'topic'
def handle(req):
"""handle a request to the function
Args:
req (str): request body
"""
# Create the MQTT client.
client = mqtt.Client()
# Connect to the broker and publish the request body to the topic.
client.connect(BROKER, PORT, 60)
client.publish(TOPIC, req)
return f'Message published to topic {TOPIC}'
这是我的本地订阅者:
import paho.mqtt.client as mqtt
BROKER = 'localhost'
PORT = 1883
TOPIC = 'topic'
def on_connect(client, userdata, flags, rc):
print(f'Connected: {rc}')
client.subscribe(TOPIC)
def on_message(client, userdata, msg):
print(f'Topic: {msg.topic} - Message: {msg.payload.decode()}')
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(BROKER, PORT, 60)
client.loop_forever()
我尝试设置本地发布者和订阅者,效果很好。每次我尝试调用我们的无服务器函数时,我们都会得到:
ConnectionRefusedError:[Errno 111] 连接被拒绝