springboot webflux
springboot2.0以上版本开始支持webflux,我们传统的是springmvc的方式,也就是内嵌的是tomcat容器进行处理,webflux则不是内嵌tomcat了,内嵌的是netty服务处理器。在并发支持上优于springmvc,下面展示如何使用webflux。
一、引入webflux
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
这里说明下,我springboot版本是2.4.4
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->
需要把web这个依赖去除,否则启动的还是tomcat容器。
二、使用步骤
1.新建类如下
package com.example.springdemo.demo.control;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
@Slf4j
public class WebFluxController {
@RequestMapping(value = "/hello2")
public Mono<String> hello(@RequestParam(name = "name") String name)
{
log.info("hello2");
return Mono.just("xxxx");
}
}
2.启动项目
提示启动netty启动成功如图。则成功使用了webflux。
3.访问
这里我使用get请求
http://localhost:8008/hello2?name=123
如图所示请求成功。
总结
1.这里仅仅是展示了简单的使用,如果涉及数据库操作,可以引入响应式的数据库操作模块。