当前位置:网站首页>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
边栏推荐
猜你喜欢
Qt——如何在QWidget中设置阴影效果
OpenFeign 簡單使用
kubeadm部署kubernetes v1.23.5集群
整理秒杀系统的面试必备!!!
Linux binary installation Oracle database 19C
Installing Oracle database 19C RAC on Linux
Solution of Xiaomi TV's inability to access computer shared files
Minecraft插件服开服
Solution and analysis of Hanoi Tower problem
Googlenet network explanation and model building
随机推荐
gocv opencv exit status 3221225785
Solution and analysis of Hanoi Tower problem
Sentinel 简单使用
Solution of Xiaomi TV's inability to access computer shared files
gocv拆分颜色通道
[flask] ORM one-to-one relationship
gocv图片裁剪并展示
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Sqli labs level 8 (Boolean blind note)
Use of libusb
Gateway 简单使用
Dip1000 implicitly tagged with fields
QT qtimer class
Call Stack
Sqli labs level 1
Kubedm deploys kubernetes v1.23.5 cluster
Nacos download, start and configure MySQL database
[blackmail virus data recovery] suffix Crylock blackmail virus
gocv图片读取并展示
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away