admin 发表于 2023-3-17 17:59:22

spring boot controller 构造函数

<img src="https://www.88m2.com/tu/uploads/2023-03-17/1679047157407.jpeg" style="width: 100%; margin-bottom: 20px;">
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">在 HTTP/1.1 及从前的版本里,顾客端、服务项目端之间的通信商业模式只全力支持一类允诺-积极响应商业模式,这是一类点对点通信商业模式 所以,在那个数学模型中,服务项目端是“消极方”,根本无法积极响应使用者的允诺,不能积极主动地发送最新消息给顾客端。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"> 为了化解那个难题,出现了全力支持动态通信的 WebSocket 协定,以及 HTTP/2、HTTP/3 中的 Stream、Server Push 等优点 但是,在 HTTP/1.1 版本中,要同时实现“动态通信”的效用根本无法透过HTTP(Polling)技术。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">所以,HTTP也根本无法达到近似于“动态通信”效用那时,我将如是说怎样在 Spring Boot 中透过HTTP同时实现服务项目端统计数据发生变动后通告顾客端01-短HTTP和长HTTP短HTTP较为好认知,指于顾客端透过循环式的形式内要一两年就允诺呵呵服务项目端,与否有统计数据预览。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"> 短HTTP的伪标识符近似于于:while(true) {// 允诺服务项目端resposne = query(Request);// 假如服务项目端有统计数据回到,则处置;不然,继续允诺服务项目端if (hasData(response)) {process(response);} else {// 隔 100ms 再度允诺sleep(100);</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">短HTTP计划中最大的难题是,频密的允诺对服务项目端的阻力太大,所以也节约网络频宽天然资源 长HTTP是对短HTTP计划的一类改良,意在增加对服务项目器天然资源的节约 长HTTP在开发工具中使用较为常用,比如 Nacos 配置服务中心,RocketMQ 最新消息堆栈等。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">长HTTP与短HTTP的监督机制是类似于的,顾客端的方传奇服务端维持不变,主要在服务项目端强化 在长HTTP中,服务项目端在没统计数据预览时并不会立刻积极响应顾客端的允诺,而要会 hold 住一两年 在这几天中,假如统计数据有预览,则立刻回到;假如没预览,延时后顾客端会再度发动允诺。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">01.1-堵塞长HTTP同时实现形式一类国际标准的同时实现形式是,在服务项目端间接等候统计数据预览,等候一两年后再积极响应允诺@RestController@RequestMapping("/polling")public class BlockingController {@GetMapping("/blocking")public ResponseEntity processBlocking(Model model) {try {// 演示较长时间等候统计数据预览TimeUnit.SECONDS.sleep(30);} catch (InterruptedException ie) { }return ResponseEntity.ok("ok");。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">这种形式有一类明显的优点,Servlet 罐子中处置允诺的缓存会被堵塞,引致服务项目端的客运量增加,无法应付高mammalian情景01.2-非堵塞长HTTP同时实现形式对前序中的一类改良是把 Servlet 中处置缓存的工作交予其他的缓存去做。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">@RestController@RequestMapping("/polling")public class NonBlockingController {private ExecutorService pool = Executors.newFixedThreadPool(5);@GetMapping("/nonblocking")public DeferredResult processNonblocking(Model model) {DeferredResult output = new DeferredResult&lt;&gt;();pool.execute(() -&gt; {try {// 演示较长时间等候统计数据预览TimeUnit.SECONDS.sleep(30);output.setResult("ok");} catch (InterruptedException e) {output.setErrorResult("Something went wrong with your order!");return output;</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">可以看到,在 handler 中,创建了一个任务提交到了缓存池里面去执行,Servlet 允诺处置缓存立刻回到了我们来看下 Spring 的日志. o.s.web.servlet.DispatcherServlet : GET "/polling/nonblocking", parameters={} s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to self.samson.example.polling.controller.NonBlockingController#processNonblocking(Model) o.s.w.c.request.async.WebAsyncManager : Started async request o.s.web.servlet.DispatcherServlet : Exiting but response remains open for further handling o.s.w.c.request.async.WebAsyncManager : Async result set, dispatch to /polling/nonblocking o.s.web.servlet.DispatcherServlet : "ASYNC" dispatch for GET "/polling/nonblocking", parameters={} s.w.s.m.m.a.RequestMappingHandlerAdapter : Resume with async result ["ok"] m.m.a.RequestResponseBodyMethodProcessor : Using text/html, given and supported m.m.a.RequestResponseBodyMethodProcessor : Writing ["ok"] o.s.web.servlet.DispatcherServlet : Exiting from "ASYNC" dispatch, status 200。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">第 4 行时,Servlet 允诺处置缓存执行完 NonBlockingController#processNonblocking 方法,但是此时 Servlet 罐子并没回复顾客端的允诺,直到缓存池执行完任务,透过 output.setResult 回到结果,积极响应才发送给顾客端。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"> 可以看到,这里的 DeferredResult 是同时实现长HTTP监督机制的关键,那个我将在下一节中如是说这种异步积极响应优点需要 Servlet 3.0 及以上罐子才能全力支持,所以 Spring 的版本要高于 3.2。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">在上面的同时实现中,允诺的处置过程是在另外一个缓存(缓存池中)完成的,并且在处置完成后,调用了 DeferredResult#setResult 方法 在那个过程中,底层 Servlet 罐子会保持住与顾客端之间的连接,直到积极响应完成,或者允诺延时(默认为 60s)。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">02. Spring 中的 DeferredResultDeferredResult 是 Spring 提供的一个异步允诺处置接口,在 I/O 密集的情景中非常有用 它全力支持三种类型的回调:onCompletion,当异步允诺处置完毕后,会执行这部分标识符。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">onError,当异步执行遇到难题后,会执行这部分标识符onTimeout,当异步执行延时后,会执行这部分标识符接下来,我将透过一个例子来演示下这三类回调的使用情景假设我经营了一家面包店,顾客可以透过 /bakery/order/{something} 来点单,点的单都会放到一个订单表中。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">面包师会根据订单制作,制作好了透过 /bakery/finish/{order} 来完成订单 假如顾客较为心急,等候一定时间后,会取消订单 我们来看下怎样同时实现:@RestController@RequestMapping("/bakery")public class BakeryController {// 存储订单,假设目前因为人手原因,每种商品最多同时接一单private Map&gt; orders = new HashMap&lt;&gt;();@GetMapping("/order/{something}")public DeferredResult order(@PathVariable("something") String something) {DeferredResult result = new DeferredResult&lt;&gt;(20 * 1000L);result.onCompletion(() -&gt; {orders.remove(something);System.out.println("顾客,您的" + something + "做好了!");result.onTimeout(() -&gt; {orders.remove(something);System.out.println("做得太慢了,我不要了");result.onError((e) -&gt; {orders.remove(something);System.out.println("出错了,好吧,我换一家");orders.put(something, result);return result;@GetMapping("/finish/{order}")public String finish(@PathVariable("order") String order) {if (orders.containsKey(order)) {DeferredResult result = orders.get(order);result.setResult("我完成了一单 " + order);return "success";</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">顾客点单后会等候,此时假如面包师能够在顾客耐心消失前做好他的订单,就会通告他取商品(onCompletion); 假如顾客等候的耐心全无,就会取消订单离开(onTimeout); 假如制作过程中,遇到难题,顾客会取消订单(onError)。</p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;">03-总结那时如是说了透过HTTP形式向服务项目端“动态”发送最新消息的同时实现形式。 这实际上是一类伪动态形式。假如要同时实现真正的动态通信,需要用到其他的技术,比如 WebSocket、更高版本的 HTTP 协定等。</p>






页: [1]
查看完整版本: spring boot controller 构造函数