当前位置:网站首页>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
边栏推荐
猜你喜欢

Sqli labs level 8 (Boolean blind note)

Solution and analysis of Hanoi Tower problem

commands out of sync. did you run multiple statements at once

OpenShift 容器平台社区版 OKD 4.10.0部署

将一串数字顺序后移

Tcp/ip - transport layer

C language replaces spaces in strings with%20

C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)

Minecraft空岛服开服

Web技术发展史
随机推荐
Npoi export word font size correspondence
Connect function and disconnect function of QT
C# 高德地图 根据经纬度获取地址
Introduction to the basic concept of queue and typical application examples
Linux binary installation Oracle database 19C
Sqli labs (post type injection)
Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
libusb的使用
Nacos download, start and configure MySQL database
File upload Labs
Luogu greedy part of the backpack line segment covers the queue to receive water
C# 调用系统声音 嘀~
Sentinel 简单使用
sqli-labs第12关
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
什么是SQL注入
Tensorflow2 keras 分类模型
Linux安装Oracle Database 19c RAC
Minecraft plug-in service opening
Image transformation, transpose