当前位置:网站首页>Webflux responsive programming
Webflux responsive programming
2022-07-02 08:52:00 【BBinChina】
List of articles
Summary
stay SpringMvc Within the framework of ,http Your request is synchronized , In some scenarios, in order to provide performance , It can be optimized asynchronously .WebFlux It provides an asynchronous technology stack
Responsive programming (reactive programming) It's based on data flow (data stream) And the transmission of change (propagation of change) The declarative form of (declarative) Programming paradigm
WebFlux Often linked to responsive programming , In fact, it is only part of the technology of responsive programming , It stands for web The control end realizes asynchronous so as to realize state change flow .
introduction WebFlux
WebFlux The reactive flow used is called Reactor Responsive stream Library
. therefore , introduction WebFlux In fact, more about how to use Reactor Of API, Let's take a look at ~
Reactor It's a reactive flow , It also has corresponding publishers (Publisher ),Reactor The publisher of is represented by two classes :
- Mono( return 0 or 1 Elements )
- Flux( return 0-n Elements )
And subscribers are Spring Framework to complete
Let's look at a simple example ( be based on WebFlux Environment construction ):
@EnableDiscoveryClient
@SpringBootApplication
public class WebFluxClient {
public static void main( String[] args )
{
SpringApplication.run(WebFluxClient.class, args);
}
}
@RestController
@RequestMapping("/WebFluxClient")
public class WebFluxClientController {
@Autowired
WebFluxClientService service;
@GetMapping("/{id}")
public Mono<String> getById(@PathVariable long id) {
return service.getOrder(id);
}
}
public class WebFluxClientService {
WebClient webClient = WebClient.create();
private <T> Mono<T> getMono(String url, Class<T> resType) {
return webClient.get().uri(url).retrieve().bodyToMono(resType);
}
public Mono<String> getOrder(long id) {
Mono<String> userMono = getMono("http://user-service/user/mock/",String.class).onErrorReturn(new String());
return userMono;
}
}
pom introduce webflux rely on
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
structure WebFlux When the environment starts , The default application server is Netty Of , Different from SpringMvc Adopted Servlet The container runs on tomcat in .
Compared with SpringMvc Synchronous blocking ,WebFlux The return is Mono, This is what WebFlux The advantage of can fix threads , There is no need to open a new thread for each request, causing thread overhead .
WebClient
In production , We use WebClient To send asynchronous requests , And support responsive programming .
WebClient By calling uri Will return Mono Data is converted to our receiving type , And its uri Access to can be requested Eureka Services registered on
The following is a WebClient Some configuration of
Underlying framework
WebClient Used at the bottom Netty Achieve asynchrony Http request , We can switch the underlying Library , Such as Jetty
@Bean
public JettyResourceFactory resourceFactory() {
return new JettyResourceFactory();
}
@Bean
public WebClient webClient() {
HttpClient httpClient = HttpClient.create();
ClientHttpConnector connector =
new JettyClientHttpConnector(httpClient, resourceFactory());
return WebClient.builder().clientConnector(connector).build();
}
Connection pool
WebClient The default is to create one connection per request .
We can configure the connection pool to reuse the connection , To improve performance .
ConnectionProvider provider = ConnectionProvider.builder("order")
.maxConnections(100)
.maxIdleTime(Duration.ofSeconds(30))
.pendingAcquireTimeout(Duration.ofMillis(100))
.build();
return WebClient
.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create(provider)));
maxConnections: The maximum number of connections allowed
pendingAcquireTimeout: When no connection is available , The maximum waiting time for a request
maxIdleTime: Connection maximum idle time
边栏推荐
猜你喜欢
Data asset management function
File upload Labs
Openshift build image
Chrome debugging
Installing Oracle database 19C RAC on Linux
Minecraft群組服開服
[blackmail virus data recovery] suffix Crylock blackmail virus
Minecraft插件服开服
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Linux安装Oracle Database 19c
随机推荐
C Gaode map obtains the address according to longitude and latitude
Kubernetes deploys Loki logging system
Network security - summary and thinking of easy-to-use fuzzy tester
gocv opencv exit status 3221225785
Qunhui NAS configuring iSCSI storage
Makefile Fundamentals
Sqli labs (post type injection)
Detailed explanation of NIN network
QT drag event
Oracle 相关统计
gocv边界填充
Web技术发展史
HCIA—应用层
sqli-labs第8关(布尔盲注)
C language replaces spaces in strings with%20
Solution and analysis of Hanoi Tower problem
ARP and ARP Spoofing
Minecraft安装资源包
Qt的connect函数和disconnect函数
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it