Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the webflux knowledge points in SpringBoot?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the webflux knowledge points in SpringBoot". In daily operation, I believe many people have doubts about webflux knowledge points in SpringBoot. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the webflux knowledge points in SpringBoot?" Next, please follow the editor to study!

Webflux introduction

Spring Boot 2.0

There is a striking saying on spring.io 's official website:

BUILD ANYTHING WITH SPRING BOOT

Spring Boot (Boot, as its name implies, means boot) framework is used to simplify the process of Spring applications from building to development.

Applications are available right out of the box, and you can start the application with an instruction, including command line java-jar, SpringApplication application startup class, Spring Boot Maven plug-in, etc.

In addition, Spring Boot emphasizes that very few configuration files are needed, so it makes development more efficient and easier in developing production-level Spring applications.

Currently, the Spring Boot version is 2.x. Spring Boot includes WebFlux.

The traditional webmvc technology represented by SpringMVC uses synchronous blocking IO model.

Spring WebFlux is an asynchronous non-blocking IO model, which can support a large number of concurrent access with a small number of container threads, so Spring WebFlux can improve throughput and scalability, but the response time of the interface will not be shortened, and the processing result must be processed by the worker thread and returned to the request.

Webflux application scenario

Suitable for IO-intensive, disk IO-intensive, network IO-intensive service scenarios, such as micro-service gateways, webflux technology can be used to significantly improve the throughput of the gateway to downstream services. Spring cloud gateway uses webflux technology.

Spring Boot 2.0 WebFlux

To learn about WebFlux, first understand what Reactive Streams is. Reactive Streams is the library standard and specification for face flow in JVM:

Deal with a potentially unlimited number of elements

Deal with it sequentially

Asynchronous transfer between components

Forced nonblocking back pressure (Backpressure)

Backpressure (back pressure)

Back pressure is a common strategy that allows publishers to have unlimited buffer storage elements to ensure that when publishers publish elements too fast, they do not suppress subscribers.

Reactive Streams (responsive streaming)

It generally consists of the following:

It generally consists of the following:

Publisher: publisher, publish elements to subscribers

Subscriber: subscribers, consumption elements

Subscription: subscription, which is shared with subscribers when a subscription is created in the publisher

Processor: processors, publishers and subscribers process data between them, including a community of publishers and subscribers

Publisher interface specification

Public interface Publisher {void subscribe (Subscriber routerFunction) {this.routerFunction = routerFunction;} @ Nullable public RouterFunction getRouterFunction () {return this.routerFunction;} public void setMessageReaders (List > routerFunctions) {/ / determine whether the current log level is Debug if (logger.isDebugEnabled ()) {int total = routerFunctions.size () String message = total + "RouterFunction (s) in" + formatMappingName (); if (logger.isTraceEnabled ()) {if (total > 0) {routerFunctions.forEach (routerFunction-> logger.trace ("Mapped" + routerFunction));} else {logger.trace (message) Else if (total > 0) {logger.debug (message);}.}

A new HandlerAdapter, HandlerFunctionAdapter, has been introduced into webflux

A new HandlerResultHandler, ServerResponseResultHandler, has been introduced into webflux

ServerResponseResultHandler implements InitializingBean, so when it is instantiated, the afterPropertiesSet method is called

Streaming request handler ()

@ Overridepublic Mono handle (ServerWebExchange exchange) {/ / handlerMappings has constructed if (this.handlerMappings = = null) {return createNotFoundError () in the initStrategies () method } / / construct Flux, and the data source is handlerMappings collection return Flux.fromIterable (this.handlerMappings) / / get Mono object Ensure that the order is consistent with the handlerMappings order through concatMap / strictly guarantee the order because there may be a situation in a system where a Url has multiple HandlerMapping that can be processed. Concatmap (mapping-> mapping.getHandler (exchange)). Next () / throw an error if next () cannot get a value. SwitchIfEmpty (createNotFoundError ()) / / trigger the handle method of HandlerApter. FlatMap (handler-> invokeHandler (exchange) Handler)) / / handleResult method that triggers HandlerResultHandler. FlatMap (result-> handleResult (exchange, result)) }

Handle method that triggers HandlerApter

Private Mono handleResult (ServerWebExchange exchange, HandlerResult result) {return getResultHandler (result) .handleResult (exchange, result) .onErrorResume (ex-> result.applyExceptionHandler (ex) .flatMap (exceptionResult-> getResultHandler (exceptionResult) .handleResult (exchange, exceptionResult) } private HandlerResultHandler getResultHandler (HandlerResult handlerResult) {if (this.resultHandlers! = null) {for (HandlerResultHandler resultHandler: this.resultHandlers) {if (resultHandler.supports (handlerResult)) {return resultHandler;} throw new IllegalStateException ("No HandlerResultHandler for" + handlerResult.getReturnValue ()) At this point, the study of "what are the knowledge points of webflux in SpringBoot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report