Spring Web vs Spring Webflux

当你需要构建Web应用程序时,Spring Web 和 Spring Webflux 是Spring生态系统中的两个框架,但它们之间有一些关键区别。

e095400b3022a9bd59bb47d4b9255749.png

Spring Web 是一个传统的Web框架,它构建在Servlet API之上。它旨在处理阻塞式I/O,即线程在从数据库或其他服务接收到响应之前被阻塞。Spring Web非常适合需要同步通信的传统Web应用程序。

相反,Spring Webflux 是一个基于Reactive Streams的响应式Web框架。它设计用于处理非阻塞I/O,即在等待来自数据库或其他服务的响应时,线程不会被阻塞。相反,应用程序可以在等待响应时继续处理其他请求。Spring Webflux非常适合需要高并发性能的应用程序,例如流式应用程序实时数据处理

下面是两个示例的代码:

Spring Web示例:

@RestController
public class WeatherController {


    @Autowired
    private WeatherService weatherService;


    @GetMapping("/weather")
    public String getWeather() {
        WeatherData weatherData = weatherService.getWeatherData();
        return "The temperature in " + weatherData.getCity() + " is " + weatherData.getTemperature() + " degrees Celsius.";
    }
}


@Service
public class WeatherService {


    @Autowired
    private RestTemplate restTemplate;


    public WeatherData getWeatherData() {
        ResponseEntity<WeatherData> response = restTemplate.getForEntity("https://api.weather.com/weatherdata", WeatherData.class);
        return response.getBody();
    }
}

Spring Webflux示例:

@RestController
public class WeatherController {


    @Autowired
    private WeatherService weatherService;


    @GetMapping("/weather")
    public Mono<String> getWeather() {
        return weatherService.getWeatherData().map(weatherData -> "The temperature in " + weatherData.getCity() + " is " + weatherData.getTemperature() + " degrees Celsius.");
    }
}


@Service
public class WeatherService {


    @Autowired
    private WebClient webClient;


    public Mono<WeatherData> getWeatherData() {
        return webClient.get().uri("https://api.weather.com/weatherdata").retrieve().bodyToMono(WeatherData.class);
    }
}

可以看到,Spring Webflux示例使用了响应式编程概念,如MonoWebClient,来处理非阻塞I/O和并发性能。而Spring Web示例则使用传统的阻塞I/O概念,如RestTemplateResponseEntity

如果Spring Web和Spring Webflux都在相同的系统资源上运行,并且同时有300个请求进入,由于其非阻塞I/O的方法,Spring Webflux的性能会更好。

在Spring Web中,每个请求都将被同步处理,这意味着处理请求的线程将被阻塞,直到接收到响应。对于300个请求,这将导致300个线程被阻塞,可能会导致高CPU和内存使用率。

相比之下,Spring Webflux可以使用单个线程同时处理300个请求,因为它使用非阻塞I/O。处理请求的线程在等待响应时不会被阻塞,而是能够处理其他请求。这导致与Spring Web相比,CPU和内存使用率较低。

总之,如果您需要处理高并发负载,由于其处理非阻塞I/O和更有效地使用系统资源的能力,Spring Webflux将是更好的选择。

以下是Spring Web和Spring Webflux的Maven依赖和配置文件。

Spring Web的Maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.6.2</version>
</dependency>


<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.3.1</version>
</dependency>

这些依赖包括以下库:

•Spring Web•Spring Web MVC•Spring Boot自动配置•Spring Boot Actuator(用于监控和管理应用程序)

application.yml

server:
  port: 8080
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/mydb
    username: myusername
    password: mypassword
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: update

Spring Webflux的Maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>2.6.2</version>
</dependency>


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
    <version>2.6.2</version>
</dependency>

application.yml

server:
  port: 8080
spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: mydb
      username: myusername
      password: mypassword
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小技术君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值