Spring Web vs Spring Webflux

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

Spring Web vs Spring Webflux_第1张图片

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 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 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 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依赖:


    org.springframework.boot
    spring-boot-starter-web
    2.6.2




    org.postgresql
    postgresql
    42.3.1

这些依赖包括以下库:

•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依赖:


    org.springframework.boot
    spring-boot-starter-webflux
    2.6.2




    org.springframework.boot
    spring-boot-starter-data-mongodb-reactive
    2.6.2

application.yml

server:
  port: 8080
spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: mydb
      username: myusername
      password: mypassword

你可能感兴趣的:(spring,前端,java,servlet,后端)