如何使用多个端点在 Azure 中编写 Spring Cloud Function?
How to code Spring Cloud Function in Azure with multiple endpoints?
我正在尝试使用 Spring Cloud 创建 2 个 Azure Functions,但我无法让它工作。
@Configuration
public class FirstFunction extends AzureSpringBootRequestHandler<Optional<Void>, String>
{
@FunctionName("firstFunction")
public void run(
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context)
{
handleRequest(Optional.empty(), context);
}
@Bean
@Lazy
Function<Optional<Void>, String> firstFunction()
{
return context ->
{
// do firstFunction stuff;
};
}
}
@Configuration
public class SecondFunction extends AzureSpringBootRequestHandler<Optional<Void>, String>
{
@FunctionName("secondFunction")
public void run(
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context)
{
handleRequest(Optional.empty(), context);
}
@Bean
@Lazy
Function<Optional<Void>, String> secondFunction()
{
return context ->
{
// do secondFunction stuff;
};
}
}
@SpringBootApplication
public class Application
{
public static void main(final String[] args)
{
SpringApplication.run(Application.class, args);
}
}
使用上面的代码与 spring-cloud-function-dependencies 2.0.1.RELEASE
的依赖关系,它总是在调用 firstFunction
和 secondFunction
端点时命中 firstFunction Bean
。
谷歌搜索后,我发现 SO answer 建议移动到 2.1
。
然而,当我尝试更改为 2.1.1.RELEASE
时,出现异常,它无法找到主 class:
System.Private.CoreLib: Exception while executing function: Functions.extractContent. System.Private.CoreLib: Result: Failure
Exception: IllegalArgumentException: Failed to locate main class
Stack: java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry
in META-INF/MANIFEST.MF (in that order).
我做错了什么需要一些帮助。
我在身边测试过,一切正常。
您可以在以下位置获得我的演示:https://github.com/AI-EVO/azuresptingfunction.git . The project is based on the official demo: https://github.com/Azure-Samples/hello-spring-function-azure
我的改动:
HelloFunction.java
@SpringBootApplication
public class HelloFunction {
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloFunction.class, args);
}
@Bean("hello")
public Function<User, Greeting> hello() {
return user -> new Greeting("Hello! Welcome, " + user.getName());
}
@Bean("hi")
public Function<User, Greeting> hi() {
return user -> new Greeting("Hi! Welcome, " + user.getName());
}
}
修改HelloHandler.java
public class HelloHandler extends AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("hello")
public Greeting execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
return handleRequest(request.getBody().get(), context);
}
}
添加HiHandler.java
public class HiHandler extends AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("hi")
public Greeting execute(@HttpTrigger(name = "request", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
return handleRequest(request.getBody().get(), context);
}
}
运行 函数:
mvn azure-functions:run
用邮递员测试
来自函数 hello:
来自函数 hi:
我正在尝试使用 Spring Cloud 创建 2 个 Azure Functions,但我无法让它工作。
@Configuration
public class FirstFunction extends AzureSpringBootRequestHandler<Optional<Void>, String>
{
@FunctionName("firstFunction")
public void run(
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context)
{
handleRequest(Optional.empty(), context);
}
@Bean
@Lazy
Function<Optional<Void>, String> firstFunction()
{
return context ->
{
// do firstFunction stuff;
};
}
}
@Configuration
public class SecondFunction extends AzureSpringBootRequestHandler<Optional<Void>, String>
{
@FunctionName("secondFunction")
public void run(
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context)
{
handleRequest(Optional.empty(), context);
}
@Bean
@Lazy
Function<Optional<Void>, String> secondFunction()
{
return context ->
{
// do secondFunction stuff;
};
}
}
@SpringBootApplication
public class Application
{
public static void main(final String[] args)
{
SpringApplication.run(Application.class, args);
}
}
使用上面的代码与 spring-cloud-function-dependencies 2.0.1.RELEASE
的依赖关系,它总是在调用 firstFunction
和 secondFunction
端点时命中 firstFunction Bean
。
谷歌搜索后,我发现 SO answer 建议移动到 2.1
。
然而,当我尝试更改为 2.1.1.RELEASE
时,出现异常,它无法找到主 class:
System.Private.CoreLib: Exception while executing function: Functions.extractContent. System.Private.CoreLib: Result: Failure
Exception: IllegalArgumentException: Failed to locate main class
Stack: java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry
in META-INF/MANIFEST.MF (in that order).
我做错了什么需要一些帮助。
我在身边测试过,一切正常。
您可以在以下位置获得我的演示:https://github.com/AI-EVO/azuresptingfunction.git . The project is based on the official demo: https://github.com/Azure-Samples/hello-spring-function-azure
我的改动:
HelloFunction.java
@SpringBootApplication
public class HelloFunction {
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloFunction.class, args);
}
@Bean("hello")
public Function<User, Greeting> hello() {
return user -> new Greeting("Hello! Welcome, " + user.getName());
}
@Bean("hi")
public Function<User, Greeting> hi() {
return user -> new Greeting("Hi! Welcome, " + user.getName());
}
}
修改HelloHandler.java
public class HelloHandler extends AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("hello")
public Greeting execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
return handleRequest(request.getBody().get(), context);
}
}
添加HiHandler.java
public class HiHandler extends AzureSpringBootRequestHandler<User, Greeting> {
@FunctionName("hi")
public Greeting execute(@HttpTrigger(name = "request", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
context.getLogger().info("Greeting user name: " + request.getBody().get().getName());
return handleRequest(request.getBody().get(), context);
}
}
运行 函数:
mvn azure-functions:run
用邮递员测试
来自函数 hello:
来自函数 hi: