• Spring Webflux 后端处理前端请求的 4 种方式


    Spring Webflux 是一个无响应系统。它具有某些特性,使其成为低延迟、高吞吐量工作负载的理想选择。Project Reactor 和 Spring 组合一起工作,使开发人员能够构建响应性、弹性、弹性和消息驱动的企业级反应系统。
    在这里插入图片描述
    Spring组合提供了两个并行栈。一种是基于带有Spring MVC和Spring Data构造的Servlet API。另一种是完全响应式栈,它利用了Spring WebFlux和Spring Data的响应式存储库。在这两种情况下,Spring Security 都为您提供了对两个栈的本机支持。

    开发人员从阻塞代码转向非阻塞代码的主要原因之一是效率。响应式代码用更少的资源做更多的工作。Project Reactor和Spring WebFlux让开发人员利用多核、下一代处理器来处理潜在的大量并发连接。使用响应式处理,您可以用更少的微服务实例满足更多的并发用户。

    Spring Webflux 支持 4 种方式来处理前端的请求。

    1、Spring MVC Controller

    1.1 pom.xml

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.2version>
            <relativePath/> 
        parent>
        <groupId>cn.carlgodd.webfluxgroupId>
        <artifactId>spring-webflux-demoartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>spring-webflux-demoname>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>11java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webfluxartifactId>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>1.2.60version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
        dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    1.2 applicaiton.properties

    spring.main.allow-bean-definition-overriding=true
    
    • 1

    1.3 启动类

    @SpringBootApplication
    public class SpringWebfluxDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringWebfluxDemoApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.4 实体类

    @Data
    public class Product {
    
        private Long id;
    
        private String name;
    
        private String desc;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.5 Controller

    @RestController
    @RequestMapping("rest")
    public class EchoController {
    
        @RequestMapping("echo")
        public Product hello() {
            Product product = new Product();
            product.setId(1L);
            product.setName("IPhone 13");
            product.setDesc("rest controller");
            return product;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    1.6 postman 测试

    在这里插入图片描述

    2、Websocket

    2.1 websocket 处理类

    @Component
    public class EchoWeSocketHandler implements WebSocketHandler {
    
        @Override
        public Mono<Void> handle(final WebSocketSession session) {
            return session.send(session.receive().map(
                    msg -> session.textMessage("[websocket] 服务端回声 : " + msg.getPayloadAsText())
                )
            );
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.2 websocket 配置类

    @Configuration
    public class WebSocketConfiguration {
    
        @Bean
        public HandlerMapping webSocketMapping(EchoWeSocketHandler echoHandler) {
            final Map<String, WebSocketHandler> map = new HashMap<>();
            map.put("/websocket/echo", echoHandler);
    
            final SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
            mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
            mapping.setUrlMap(map);
            return mapping;
        }
    
        @Bean
        public WebSocketHandlerAdapter webSocketHandlerAdapter() {
            return new WebSocketHandlerAdapter();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.3 postman 测试

    在这里插入图片描述

    3、简单请求处理

    3.1 WebHandler 处理类

    @Component
    public class EchoWebHandler implements WebHandler {
    
        @SneakyThrows
        @Override
        public Mono<Void> handle(ServerWebExchange exchange) {
            ServerHttpResponse response = exchange.getResponse();
            DataBufferFactory bufferFactory = response.bufferFactory();
            ObjectMapper objectMapper = new ObjectMapper();
            Product product = new Product();
            product.setId(1L);
            product.setName("IPhone 13");
            product.setDesc("web handler");
            DataBuffer wrap = bufferFactory.wrap(objectMapper.writeValueAsBytes(product));
            return response.writeWith(Mono.fromSupplier(() -> wrap));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.2 WebHandler 配置类

    @Configuration
    public class WebHandlerConfiguration {
    
        @Bean
        public HandlerMapping simpleWebHandlerMapping(final EchoWebHandler webHandler) {
            final Map<String, WebHandler> map = new HashMap<>();
            map.put("/web/echo", webHandler);
    
            final SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
            mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
            mapping.setUrlMap(map);
            return mapping;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.3 postman 测试

    在这里插入图片描述

    4、Function Handler

    4.1 Function Handler 处理类

    @Component
    public class EchoFunctionHandler {
    
        public Mono<ServerResponse> echo(ServerRequest request) {
            Product product = new Product();
            product.setId(1L);
            product.setName("IPhone 13");
            product.setDesc("function handler");
            return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
                    .body(BodyInserters.fromValue(product));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.2 Function Handler 配置类

    @Configuration
    public class FunctionHandlerConfiguration {
    
        @Bean
        public RouterFunction<ServerResponse> route(EchoFunctionHandler echoFunctionHandler) {
            return RouterFunctions
                    .route(RequestPredicates.GET("/function/echo")
                            .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), echoFunctionHandler::echo);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4.3 postman 测试

    在这里插入图片描述

    参考文章:

  • 相关阅读:
    Linux命令(88)之echo
    2023济南大学计算机考研信息汇总
    计算机网络——数据链路层の选择题整理
    Redis集群安装之分片集群
    红绿正方形染色问题
    Go语言学习笔记-A Tour of Go 练习笔记-Maps
    使用 strace、tcpdump、nlmon、wireshark 探索 ethtool netlink 框架的原理
    万字逐行解析与实现Transformer,并进行德译英实战(一)
    (六)正点原子STM32MP135移植——内核移植
    2021.09青少年软件编程(Python)等级考试试卷(一级)
  • 原文地址:https://blog.csdn.net/u012410733/article/details/126082591