前言
Spring WebFlux 是 Spring Framework 5.0中引入的新的响应式web框架。与Spring MVC不同,它不需要Servlet API,是完全异步且非阻塞的,并且通过Reactor项目实现了Reactive Streams规范。
Reactive Streams是一套用于构建高吞吐量、低延迟应用的规范。而Reactor项目是基于这套规范的实现,它是一个完全非阻塞的基础,且支持背压。Spring WebFlux基于Reactor实现了完全异步非阻塞的一套web框架,是一套响应式堆栈。
要在WebFlux中调用FastGPT接口,可以使用WebClient
来构建异步请求。
1、搭建项目,导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、配置application.properties
server.port=8081
spring.r2dbc.url=r2dbc:mysql://localhost:3306/test
spring.r2dbc.username=账号
spring.r2dbc.password=密码
3、config
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient() {
return WebClient.builder().baseUrl("https://cloud.fastgpt.cn").build();
}
}
4、实体类
public class FastGPTRequest {
private String chatId;
private String appId;
private boolean stream;
private boolean detail;
private List<Message> messages;
// 默认构造函数
public FastGPTRequest() {}
// Getters 和 Setters
public String getChatId() {
return chatId;
}
public void setChatId(String chatId) {
this.chatId = chatId;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public boolean isStream() {
return stream;
}
public void setStream(boolean stream) {
this.stream = stream;
}
public boolean isDetail() {
return detail;
}
public void setDetail(boolean detail) {
this.detail = detail;
}
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
public static class Message {
private String role;
private String content;
// 默认构造函数
public Message() {}
// Getters 和 Setters
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
5、controller
@RestController
@RequestMapping("/fastgpt")
public class FastGPTController {
@Autowired
private FastGPTRestService fastGPTRestService;
@PostMapping(value = "/test")
public Mono<String> callFastGPT(@RequestBody FastGPTRequest request) {
return fastGPTRestService.callFastGPT(request);
}
}
6、service
@Service
public class FastGPTRestService {
private final WebClient webClient;
public FastGPTRestService(WebClient webClient) {
this.webClient = webClient;
}
public Mono<String> callFastGPT(FastGPTRequest request) {
return webClient.post()
.uri("/api/v1/chat/completions")
.header("Authorization", "Bearer 你的bearer")
.bodyValue(request)
.retrieve()
.bodyToMono(String.class);
}
}
7、完成后即可启动项目,进行测试,结束
附言:WebFlux 并不能使接口的响应时间缩短,它仅仅能够提升吞吐量和伸缩性。
Spring WebFlux 是一个异步非阻塞的 Web 框架,所以,它特别适合应用在 IO 密集型的服务中。