我正在尝试使用以下指南创建 spring boot azure 功能
https://github.com/eggboy/springcloud-azurefunction/tree/springboot3
https://medium.com/microsoftazure/create-azure-function-with-spring-cloud-function-ab150216d2bd
我能够在本地成功构建并运行 azure 函数。
但是当我尝试部署并触发 http 端点时,我收到以下错误,其中包含 500 错误。 有人可以帮助理解这里出了什么问题吗?
2024-06-15T04:10:37Z [Information] Executing 'Functions.getUserFunction' (Reason='This function was programmatically called via the host APIs.', Id=57887aec-5989-45c1-bb34-453059bc2c5b)
2024-06-15T04:10:37Z [Verbose] Sending invocation id: '57887aec-5989-45c1-bb34-453059bc2c5b
2024-06-15T04:10:37Z [Verbose] Posting invocation id:57887aec-5989-45c1-bb34-453059bc2c5b on workerId:18a329dc-7f2e-4e0b-8487-c67d912b9eea
2024-06-15T04:10:37Z [Error] Executed 'Functions.getUserFunction' (Failed, Id=57887aec-5989-45c1-bb34-453059bc2c5b, Duration=5ms)
2024-06-15T04:10:37Z [Verbose] [HostMonitor] Checking worker statuses (Count=1)
2024-06-15T04:10:37Z [Verbose] [HostMonitor] Worker status: ID=18a329dc-7f2e-4e0b-8487-c67d912b9eea, Latency=1ms
2024-06-15T04:10:37Z [Verbose] [HostMonitor] Host process CPU stats (PID 68): History=(0,0,0,0,1), AvgCpuLoad=0.2, MaxCpuLoad=1
2024-06-15T04:10:37Z [Verbose] [HostMonitor] Host process CPU stats (PID 26): History=(2,3,2,2,3), AvgCpuLoad=2, MaxCpuLoad=3
2024-06-15T04:10:37Z [Verbose] [HostMonitor] Host aggregate CPU load 3
2024-06-15T04:10:37Z [Information] Executing StatusCodeResult, setting HTTP status code 200
2024-06-15T04:10:39Z [Verbose] Request successfully matched the route with name 'getUserFunction' and template 'api/getUserFunction'
2024-06-15T04:10:39Z [Information] Executing 'Functions.getUserFunction' (Reason='This function was programmatically called via the host APIs.', Id=8d7fe07c-108e-4f3b-886e-8a95750737b8)
2024-06-15T04:10:39Z [Verbose] Sending invocation id: '8d7fe07c-108e-4f3b-886e-8a95750737b8
2024-06-15T04:10:39Z [Verbose] Posting invocation id:8d7fe07c-108e-4f3b-886e-8a95750737b8 on workerId:18a329dc-7f2e-4e0b-8487-c67d912b9eea
2024-06-15T04:10:39Z [Error] Executed 'Functions.getUserFunction' (Failed, Id=8d7fe07c-108e-4f3b-886e-8a95750737b8, Duration=5ms)
2024-06-15T04:10:41Z [Verbose] Request successfully matched the route with name 'getUserFunction' and template 'api/getUserFunction'
2024-06-15T04:10:41Z [Information] Executing 'Functions.getUserFunction' (Reason='This function was programmatically called via the host APIs.', Id=7fba93bb-0744-4af3-8bb3-a604b8a5c4bd)
2024-06-15T04:10:41Z [Verbose] Sending invocation id: '7fba93bb-0744-4af3-8bb3-a604b8a5c4bd
2024-06-15T04:10:41Z [Verbose] Posting invocation id:7fba93bb-0744-4af3-8bb3-a604b8a5c4bd on workerId:18a329dc-7f2e-4e0b-8487-c67d912b9eea
2024-06-15T04:10:41Z [Error] Executed 'Functions.getUserFunction' (Failed, Id=7fba93bb-0744-4af3-8bb3-a604b8a5c4bd, Duration=7ms)
https://xxx/api/getUserFunction?userId=abc
我期望 azure http 触发器通过浏览器和邮递员成功返回 HTTP 200 响应,但两者都失败并出现 500 错误。但在本地它可以工作并返回 HTTP 200 的预期结果
我的触发代码在这里
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 io.jaylee.springcloud.model.UserDTO;
import io.jaylee.springcloud.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
@RequiredArgsConstructor
public class FunctionHandler {
private final UserService userService;
@FunctionName("createUserFunction")
public HttpResponseMessage createUser(
@HttpTrigger(name = "request", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<UserDTO>> request,
ExecutionContext context) {
if (request == null) {
throw new IllegalArgumentException("Request cannot be null");
}
context.getLogger()
.info(request.getBody()
.map(UserDTO::toString).orElse(""));
userService.saveUser(request.getBody().orElseThrow());
return request
.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "application/json")
.build();
}
@FunctionName("getUserFunction")
public HttpResponseMessage getUser(
@HttpTrigger(name = "request", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
ExecutionContext context) {
String userId = request.getQueryParameters().get("userId");
if (userId == null || "".equals(userId)) {
throw new IllegalArgumentException("Request cannot be null");
}
context.getLogger().info(userId);
return request
.createResponseBuilder(HttpStatus.OK)
.body(userService.getUser(userId))
.header("Content-Type", "application/json")
.build();
}
}
本地.setting.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "java",
"FUNCTIONS_EXTENSION_VERSION": "~4",
"AzureWebJobsDashboard": ""
}
}
主机.json
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
用户服务.java
import io.jaylee.springcloud.model.User;
import io.jaylee.springcloud.model.UserDTO;
import io.jaylee.springcloud.repository.UserRepository;
import org.springframework.stereotype.Component;
import java.time.ZonedDateTime;
@Component
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public String saveUser(UserDTO userDTO) {
var user = User.builder()
.userId(userDTO.getUserId())
.lastName(userDTO.getLastName())
.firstName(userDTO.getFirstName())
.email(userDTO.getEmail())
.createdOn(ZonedDateTime.now())
.build();
return userRepository.save(user).getUserId();
}
public UserDTO getUser(String userId) {
var user = userRepository.findById(userId).orElse(new User());
return new UserDTO(user.getUserId(),
user.getLastName(), user.getFirstName(), user.getEmail());
}
}
当您收到 500 状态码时,请检查您的 Azure 函数的调用日志,了解错误原因的详细信息。
我创建了一个 Spring Boot Azure 函数并部署到 Azure。