SpringBoot + Spring Cloud Function:可配置的功能组合问题



关于SpringBoot + Spring Cloud功能应用的小问题。

我有一个非常简单的应用程序,它接受一个单词作为输入。

业务逻辑是通过函数编写的。"difficulty"是:功能需要可配置。这意味着,只需一个application.properties更改+重启,web应用程序应该能够选择正确的业务逻辑,即正确的功能组合应用到输入,而无需更改代码。

下面是一个例子:

application.properties:

spring.cloud.function.definition=upper
#spring.cloud.function.definition=reverse
#spring.cloud.function.definition=upper|reverse

pom:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.function.Function;
@Service
public class ReverseFunction {
@Bean
Function<String, String> reverse() {
return s -> new StringBuilder(s).reverse().toString();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.function.Function;
@Service
public class UpperFunction {
@Bean
Function<String, String> upper() {
return String::toUpperCase;
}
}

业务逻辑如下:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.function.Function;
@RestController
public class FunctionController {
@GetMapping("/applyFunctionToWord")
public String applyFunctionToWord(@RequestParam String word) {
Function<String, String> function = ??? //configurable from application.properties
return function.apply(word);
}
}

在网上找到的教程和Spring的官方文档中,说:

Function Composition
Function composition is a feature of SCF that lets you compose several functions together in a declarative way.
The following example shows how to do so:
--spring.cloud.function.definition=uppercase|reverse
Here, we effectively provided a definition of a single function that is itself a composition of a function named uppercase and a function named reverse. You can also argue that we’ve orchestrated a simple pipeline consisting of running the uppercase function and then sending its output to the reverse function. The term orchestration is important here, and we cover it in more detail later in the post.

https://spring.io/blog/2019/11/04/spring-cloud-stream-composed-functions-or-eip: ~:文本=函数% 20成分% 20 % 20 % 20特性% 20 % 20的自洽场% 20,% % 20 20例子展示了% 20后如何% 20 % 20 % 20所以% % 20 - 3 spring.cloud.function.definition % 3 duppercase % 7 creverse

What I tried:

我确实在application.properties中配置了这个属性。

正如您所看到的,我确实遵循了命名属性的指南:spring.cloud.function.definition。请问正确的机制是什么?

我是否被迫后退到@Value的东西?

我的应用程序不是Spring Cloud Stream应用程序。它不是基于Kafka的。它只是一个基本的web应用程序,其中的业务逻辑是一个可配置的组合功能。

问题:

如何使web应用程序生效spring.cloud.function.definition=中配置的值,并将其应用于输入?

谢谢

我认为它应该使用RoutingFunction工作,像这样:

@RestController
public class FunctionController {

@Autowired
RoutingFunction function;
@GetMapping("/applyFunctionToWord")
public String applyFunctionToWord(@RequestParam String word) {
return (String)function.apply(word);
}
}

最新更新