Google cloud platform 功能框架(仿真器)订阅发布/订阅仿真器

Google cloud platform 功能框架(仿真器)订阅发布/订阅仿真器,google-cloud-platform,google-cloud-functions,google-cloud-pubsub,functions-framework,Google Cloud Platform,Google Cloud Functions,Google Cloud Pubsub,Functions Framework,我想做一个设置:My Docked App->Pub/Sub Docked emulator上面的函数emulator代码需要实际启动订阅,以侦听来自发布/订阅主题的消息。请注意,云函数使用底层订阅从云发布/订阅接收消息并触发用户定义的函数。Pub/Sub(在本例中是模拟器)不知道任何用户定义的云函数 要模拟此行为,请听下面的发布/子主题中的消息,然后使用构造的参数调用所需的用户定义函数。到目前为止,我发现如果从脚本中删除--signature type=event,请再次运行,并触发URL一次

我想做一个设置:My Docked App->Pub/Sub Docked emulator上面的
函数
emulator代码需要实际启动订阅,以侦听来自发布/订阅主题的消息。请注意,云函数使用底层订阅从云发布/订阅接收消息并触发用户定义的函数。Pub/Sub(在本例中是模拟器)不知道任何用户定义的云函数


要模拟此行为,请听下面的发布/子主题中的消息,然后使用构造的参数调用所需的用户定义函数。

到目前为止,我发现如果从脚本中删除--signature type=event,请再次运行,并触发URL一次,然后在函数中创建一个新的PubSub对象,然后它侦听来自PubSub的新消息。但这显然是错误的。我只是想指出,PubSub订阅没有问题,但是使用事件触发函数是没有问题的。函数中的代码可能是错误的,但我的观点是,函数从来没有被触发过,我只是想说清楚,非常感谢您的回复。所以我在做本地开发的时候应该只使用你链接的例子,对吗?我打赌我不需要任何监听器,因为这是由GCP自己处理的。还是我错了?此外,我发现functions framework的GitHub中有一个示例(这似乎是一个非常新的示例),尽管这是一个push方法。在进行本地开发时,推送方法是否更好?如果您已经在使用functions框架,我会按照他们的建议,使用推送订阅进行测试。那样的话,你就必须拉入应用程序代码,并且可以测试你的实现,因为它将在GCF上运行。现在谢谢你!
version: '3.4'

services:
  myapp:
    build: ./myapp
    ports:
        - 8080:8080
    environment:
      NODE_ENV: development
      PUBSUB_EMULATOR_HOST: pubsub:8681
      PUBSUB_PROJECT_ID: pubsubproject
    volumes:
        - ./myapp/:/workspace
        - /workspace/node_modules
    depends_on: 
    - pubsub

  pubsub:
    image: messagebird/gcloud-pubsub-emulator
    environment:
      PUBSUB_PROJECT1: 'pubsubproject,published-events:published-events-pull'
    ports:
      - "8681:8681"

  functions:
    build:
      dockerfile: Dockerfile
      context: ./functions
    ports:
      - 8888:8888
    depends_on: 
      - pubsub
    environment:
      NODE_ENV: development
      PUBSUB_EMULATOR_HOST: pubsub:8681
      PUBSUB_PROJECT_ID: pubsubproject
    volumes:
      - ./functions/:/workspace
      - /workspace/node_modules
async publishToQueue(events: any[]) {
        const pubSub = new PubSub();
        const buffer = Buffer.from(JSON.stringify(events))

        try {
            const messageId = await pubSub.topic("published-events").publish(buffer);
            console.log(`Message ${messageId} published.`);
          } catch (error) {
            console.error(`Received error while publishing: ${error.message}`);
            process.exitCode = 1;
          }
    }
import { PubSub } from '@google-cloud/pubsub'

exports.helloPubSub = (data, context) => {   // <---- This function is never triggered
    console.log("FROM CLOUD FUNCTION")
    console.log(data)
    console.log(context)

    const pubSub = new PubSub()
    const subscription = pubSub.subscription("published-events-pull")

    const messageHandler = async message => {
        const data = JSON.parse(Buffer.from(message.data, 'base64').toString())
        console.log('Received data:')
        console.log(data)
        message.ack()
    }
    subscription.on('message', messageHandler)
};
  "scripts": {
    "dev": "yarn run build:dev:init && yarn run build:dev | yarn run start:functions",
    "build:dev:init": "yarn run tsc ",
    "build:dev": "yarn run tsc -w ",
    "start:functions": "functions-framework --port=8888 --source=dist/ --target=helloPubSub --signature-type=event"
  }