我有简单的 hello world Azure 函数应用程序。我在执行函数应用程序时遇到以下异常:
Executed 'Functions.functiontest' (Failed, Id=1040b836-9136-4f90-b3fb-c130f8e89172)
[12/12/2019 4:20:35 PM] System.Private.CoreLib: Exception while executing function: Functions.functiontest. System.Private.CoreLib: Result: Failure
Exception: NullPointerException:
Stack: java.lang.reflect.InvocationTargetException
[12/12/2019 4:20:35 PM] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[12/12/2019 4:20:35 PM] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[12/12/2019 4:20:35 PM] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[12/12/2019 4:20:35 PM] at java.lang.reflect.Method.invoke(Method.java:483)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:22)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:54)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:53)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
[12/12/2019 4:20:35 PM] at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer$$Lambda$12/1869197502.run(Unknown Source)
[12/12/2019 4:20:35 PM] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[12/12/2019 4:20:35 PM] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[12/12/2019 4:20:35 PM] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[12/12/2019 4:20:35 PM] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[12/12/2019 4:20:35 PM] at java.lang.Thread.run(Thread.java:745)
[12/12/2019 4:20:35 PM] Caused by: java.lang.NullPointerException
[12/12/2019 4:20:35 PM] at service.FunctionApp.carrierMaster(FunctionApp.java:38)
[12/12/2019 4:20:35 PM] ... 17 more
这是一个返回HelloWorld的基本函数napp。我添加了一些代码来使用 Azure 实现分布式跟踪。这是函数应用程序代码:
package service;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.core.dependencies.google.protobuf.Message;
import com.microsoft.applicationinsights.core.dependencies.http.HttpEntity;
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.telemetry.Duration;
import com.microsoft.applicationinsights.telemetry.RemoteDependencyTelemetry;
import com.microsoft.applicationinsights.telemetry.RequestTelemetry;
import com.microsoft.applicationinsights.web.internal.RequestTelemetryContext;
import com.microsoft.applicationinsights.web.internal.ThreadContext;
import com.microsoft.applicationinsights.web.internal.correlation.TelemetryCorrelationUtils;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
public class FunctionApp {
@Bean
public RestTemplate rest() {
return new RestTemplate();
}
private static final Logger log = LogManager.getLogger(FunctionApp.class);
@FunctionName("functiontest")
public HttpResponseMessage carrierMaster(@HttpTrigger(name = "req", methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) throws UnsupportedEncodingException {
log.debug("Inside the function");
RequestTelemetryContext context1 = ThreadContext.getRequestTelemetryContext();
if(context1 == null) {
InternalLogger.INSTANCE.warn("Context is null");
log.info("inside context1 try");
// return request.createResponseBuilder(HttpStatus.OK).body("hello world").build();
}
RequestTelemetry requestTelemetry = context1.getHttpRequestTelemetry();
String dependencyId = TelemetryCorrelationUtils.generateChildDependencyId();
RemoteDependencyTelemetry dependencyTelemetry = new RemoteDependencyTelemetry("Send Data");
dependencyTelemetry.setId(dependencyId);
dependencyTelemetry.getContext().getOperation().setId(requestTelemetry.getContext().getOperation().getId());
dependencyTelemetry.getContext().getOperation().setParentId(requestTelemetry.getId());
Duration duration = new Duration(0,0,0,0,10); // set the duration 10 millisec as an example.
dependencyTelemetry.setDuration(duration);
TelemetryConfiguration configuration = TelemetryConfiguration.getActive();
TelemetryClient telemetryClient = new TelemetryClient();
telemetryClient.trackDependency(dependencyTelemetry);
log.debug(request.createResponseBuilder(HttpStatus.OK).toString());
return request.createResponseBuilder(HttpStatus.OK).body("hello world").build();
}
@GetMapping("/sample")
String callApp() {
return rest().getForObject("http://10.33.92.92:8081/sample/hello", String.class);
}
}
下面是 pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sams.wms</groupId>
<artifactId>fnapcicd</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>fnapcicd</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath></relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<org.apache.camel>2.24.1</org.apache.camel>
<org.apache.solr>3.5.0</org.apache.solr>
<org.springboot.starter.web>2.1.5.RELEASE</org.springboot.starter.web>
<azure.functions.maven.plugin.version>1.3.3</azure.functions.maven.plugin.version>
<azure.functions.java.library.version>1.3.0</azure.functions.java.library.version>
<functionAppName>test-java-fa</functionAppName>
<functionAppRegion>East US</functionAppRegion>
<stagingDirectory>${project.build.directory}/azure-functions/${functionAppName}</stagingDirectory>
<functionResourceGroup>dev-wfm-learning-fns</functionResourceGroup>
<start-class>com.example.ConverterRoute</start-class>
<wrapper.version>1.0.22.RELEASE</wrapper.version>
<jacoco.version>0.7.9</jacoco.version>
<sonar-jacoco-listeners.version>3.2</sonar-jacoco-listeners.version>
<sonar.language>java</sonar.language>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco-ut.exec
</sonar.jacoco.reportPath>
<sonar.jacoco.itReportPath>${project.build.directory}/coverage-reports/jacoco-it.exec
</sonar.jacoco.itReportPath>
</properties>
<repositories>
<repository>
<id>maven.snapshots</id>
<name>Maven Central Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven.snapshots</id>
<name>Maven Central Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<repository>
<id>proximity</id>
<name>Walmart Releases</name>
<url>https://repository.walmart.com/content/repositories/pangaea_releases</url>
</repository>
<snapshotRepository>
<id>proximity</id>
<name>Walmart Releases</name>
<url>https://repository.walmart.com/content/repositories/pangaea_snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bindy</artifactId>
<version>${org.apache.camel}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bindy-starter</artifactId>
<version>${org.apache.camel}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jacksonxml-starter</artifactId>
<version>${org.apache.camel}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${org.apache.camel}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-csv</artifactId>
<version>${org.apache.camel}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xstream</artifactId>
<version>${org.apache.camel}</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-commons-csv</artifactId>
<version>${org.apache.solr}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>${org.apache.camel}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-azure-starter</artifactId>
<version>${org.apache.camel}</version>
<!-- use the same version as your Camel core version -->
</dependency>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-example-aggregate -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-example-aggregate</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<version>${org.apache.camel}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Application Insights dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-logging-log4j2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-web-auto</artifactId>
<!-- or applicationinsights-web for manual web filter registration -->
<!-- or applicationinsights-core for bare API -->
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>${azure.functions.maven.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<configuration>
<resourceGroup>${functionResourceGroup}</resourceGroup>
<appName>${functionAppName}</appName>
<region>${functionAppRegion}</region>
<appSettings>
<!-- Run Azure Function from package file by default -->
<property>
<name>WEBSITE_RUN_FROM_PACKAGE</name>
<value>1</value>
</property>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~2</value>
</property>
<property>
<name>FUNCTIONS_WORKER_RUNTIME</name>
<value>java</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>
${project.build.directory}/azure-functions/${functionAppName}
</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/azure
</directory>
<includes>
<include>**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${stagingDirectory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
<excludeArtifactIds>azure-functions-java-library</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<!--Remove obj folder generated by .NET SDK in maven clean-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<filesets>
<fileset>
<directory>obj</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<propertyName>coverageAgent</propertyName>
<append>true</append>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
<append>true</append>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<outputDirectory>${project.basedir}/../target/coverageReport</outputDirectory>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile> <outputDirectory>${project.basedir}/../target/coverageReport</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!--suppress UnresolvedMavenProperty -->
<argLine>${surefireArgLine}</argLine>
<includes>
<include>**/*Test*</include>
</includes>
<excludes>
<exclude>**/FT/**</exclude>
<exclude>**/*IT*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20</version>
<configuration>
<!--suppress UnresolvedMavenProperty -->
<argLine>${failsafeArgLine}</argLine>
<includes>
<include>**/*IT*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
任何人都可以帮忙吗?
最佳答案
请按照下面的入门步骤操作,看看是否有效。
https://docs.spring.io/spring-cloud-function/docs/3.1.3/reference/html/azure.html
看注释:
The Hello function is quite specific: It is a java.util.function.Function. It contains the business logic, and it uses a standard Java API to transform one object into another. Because it has the @Component annotation, it's a Spring Bean, and by default its name is the same as the class, but starting with a lowercase character: hello. Following this naming convention is important if you want to create other functions in your application. The name must match the Azure Functions name we'll create in the next section.
关于azure - 在 java 中执行 Azure 函数应用程序时获取 NullPointerException : Stack: java. lang.reflect.InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59309653/