如何在micronaut应用程序中使用GCP模拟器实现后台功能

问题描述 投票:0回答:1

我有两个 micronaut GCP 应用程序

  1. HTTP功能
  2. 后台功能

HTTP 函数是一个常规的 micronuat 应用程序,具有以下依赖项,并且使用 pub/sub 我试图触发后台函数

implementation("io.micronaut.gcp:micronaut-gcp-pubsub")

控制器

@Introspected
public class PubSubMessage {
    String data;
    Map<String, String> attributes;
    String messageId;
    String publishTime;
}

    @Controller("/")
    public record FileUploadController(IFileUploadClient iFileUploadClient){
    
        public String getFileName() {
            iFileUploadClient.pubSubMessage(new PubSubMessage());
            return "THis is file 1";
        }
    }

    @PubSubClient
    public interface IFileUploadClient {
        @Topic("projects/test-project/topics/micronaut-devices")
        void pubSubMessage(PubSubMessage pubSubMessage);
    }

后台功能

public class Function extends GoogleFunctionInitializer
        implements BackgroundFunction<PubSubMessage> {


    @Override
    public void accept(PubSubMessage message, Context context) {
        System.out.println("Hello world");
    }
}

class PubSubMessage {
    String data;
    Map<String, String> attributes;
    String messageId;
    String publishTime;
}

我正在使用

runtime("netty")
作为常规 micronaut 应用程序运行 HTTP 函数,但是对于后台函数,我已经设置了 gradle 任务

tasks.register("runFunction", JavaExec) {
  main = 'com.google.cloud.functions.invoker.runner.Invoker'
  classpath(configurations.invoker)
  inputs.files(configurations.runtimeClasspath, sourceSets.main.output)
  args(
    '--target', project.findProperty('run.functionTarget') ?: 'fete.bird.Function',
    '--port', project.findProperty('run.port') ?: 8081
  )
  doFirst {
    args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)
  }
}

我正在使用带有以下命令的模拟器

Gcloud beta emulators pub sub start --host-port=127.0.0.1:8086 --project=test-project

模拟器有以下主题

发布消息抛出异常

 iFileUploadClient.pubSubMessage(new PubSubMessage());

在 micronaut 文档中提到如果在环境中设置了此变量,则框架支持 TransportChannelProvider 自动切换为使用 PUBSUB_EMULATOR_HOST

确保您还将 GCP_PROJECT_ID 设置为与您配置模拟器使用的项目相同。否则,框架可能会获取默认凭据使用的projectId。

框架以某种方式选择我使用命令设置的不同项目

--project=test-project

错误

ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: java.util.concurrent.ExecutionException: com.google.api.gax.rpc.InvalidArgumentException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Project 'project:adept-button-401609' not found or deleted.
reactor.core.Exceptions$ReactiveException: java.util.concurrent.ExecutionException: com.google.api.gax.rpc.InvalidArgumentException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Project 'project:adept-button-401609' not found or deleted.
    at reactor.core.Exceptions.propagate(Exceptions.java:408)
    at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:101)
    at reactor.core.publisher.Mono.block(Mono.java:1712)
    at io.micronaut.gcp.pubsub.intercept.PubSubClientIntroductionAdvice.intercept(PubSubClientIntroductionAdvice.java:185)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:137)
    at fete.bird.IFileUploadClient$Intercepted.pubSubMessage(Unknown Source)
    at fete.bird.FileUploadController.getFileName(FileUploadController.java:26)
    at fete.bird.$FileUploadController$Definition$Exec.dispatch(Unknown Source)
    at io.micronaut.context.AbstractExecutableMethodsDefinition$DispatchedExecutableMethod.invoke(AbstractExecutableMethodsDefinition.java:442)
    at io.micronaut.context.DefaultBeanContext$BeanContextExecutionHandle.invoke(DefaultBeanContext.java:4282)
    at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:223)
    at io.micronaut.http.context.ServerRequestContext.with(ServerRequestContext.java:74)
    at io.micronaut.http.server.RouteExecutor.executeRouteAndConvertBody(RouteExecutor.java:480)
    at io.micronaut.http.server.RouteExecutor.callRoute(RouteExecutor.java:470)
    at io.micronaut.http.server.RequestLifecycle.lambda$normalFlow$2(RequestLifecycle.java:146)
    at io.micronaut.core.execution.ImperativeExecutionFlowImpl.flatMap(ImperativeExecutionFlowImpl.java:72)
    at io.micronaut.http.server.RequestLifecycle.lambda$normalFlow$4(RequestLifecycle.java:146)

错误是

INVALID_ARGUMENT: Project 'project:adept-button-401609' not found or deleted.

我也公开了以下命令

$(gcloud beta emulators pubsub env-init)

export PUBSUB_EMULATOR_HOST=127.0.0.1:8086
export PUBSUB_PROJECT_ID=test-project

我还导出了项目ID,因为

export GCP_PROJECT_ID="test-project"
仍然是同样的问题

应用程序运行时使用默认凭据

.properties 文件

gcp.project-id=test-project

GitHub - https://github.com/anandjaisy/gcp-background-function

在应用程序中,默认凭据适用于 mac

它正在使用这个json

{
  "client_id": "xxx",
  "client_secret": "xxx",
  "quota_project_id": "adept-button-401609",
  "refresh_token": "xxx",
  "type": "authorized_user"
}

但是,应该使用模拟器版本。由于某些原因,micronaut 应用程序没有启动

java google-cloud-platform google-cloud-pubsub micronaut micronautgcp
1个回答
0
投票

问题是导出环境变量需要与应用程序运行在同一会话中。

终端

export PUBSUB_EMULATOR_HOST=localhost:8085

export PUBSUB_PROJECT_ID=white-stacker-412404

然后

./gradlew run

通过在同一终端会话中设置所有环境变量,应用程序可以正常工作。

Intellj IDE

编辑配置并设置以下环境变量

PUBSUB_PROJECT_ID=white-stacker-412404;PUBSUB_EMULATOR_HOST=localhost:8085
© www.soinside.com 2019 - 2024. All rights reserved.