zoukankan      html  css  js  c++  java
  • WebFlux系列(六)WebClient VS RestTemplate

    #Java#Spring#WebFlux#WebClient#RestTemplate#

    WebClient和RestTemplate性能比较

    视频讲解: https://www.bilibili.com/video/av82675791/

    服务器端
    WebfluxServerApplication.java
    package com.example.webfluxserver;
    
    import lombok.extern.log4j.Log4j2;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @Log4j2
    @SpringBootApplication
    public class WebfluxServerApplication extends BaseApplication {
        public static void main(String[] args) {
            SpringApplication.run(WebfluxServerApplication.class, args);
        }
        @RestController
        class EmployeeController {
            @GetMapping("employees")
            public List<Employee> findAll() throws InterruptedException {
                Thread.sleep(5000);
                return list;
            }
        }
    }
    客户端
    WebfluxConsumerApplication.java
    package com.example.webfluxconsumer;
    
    import lombok.extern.log4j.Log4j2;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    import org.springframework.web.reactive.function.client.WebClient;
    import reactor.core.publisher.Flux;
    import reactor.core.publisher.Mono;
    
    import java.util.Arrays;
    import java.util.List;
    
    @Log4j2
    @SpringBootApplication
    public class WebfluxConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(WebfluxConsumerApplication.class, args);
        }
    
        @RestController
        class EmployeeController {
    
            @GetMapping(value = "web", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
            public Flux<Employee> findWithWebClient() {
                long startTime = System.currentTimeMillis();
                Flux<Employee> employeeFlux = WebClient.create("http://localhost:8080/employees")
                        .get().retrieve().bodyToFlux(Employee.class);
                long endTime = System.currentTimeMillis();
                long duartion = endTime - startTime;
                log.info("webclient took times:{}", duartion);
                return employeeFlux;
            }
    
    
            @GetMapping("rest")
            public List<Employee> findWithRestTemplate() {
                long startTime = System.currentTimeMillis();
                RestTemplate restTemplate = new RestTemplate();
                ResponseEntity<Employee[]> responseEntity = restTemplate
                        .getForEntity("http://localhost:8080/employees", Employee[].class);
                List<Employee> employeeList = Arrays.asList(responseEntity.getBody());
                long endTime = System.currentTimeMillis();
                long duartion = endTime - startTime;
                log.info("restTemplate took times:{}", duartion);
                return employeeList;
            }
    
    
        }
    }

    公众号,坚持每天3分钟视频学习

  • 相关阅读:
    【SpringBoot/Oracle】如何解决 “[error code]17056 不支持的字符集,在类路径添加orai18n.jar”
    Spring Initializr
    【SpringBoot】如何在SpringBoot工程启动时建表和填充数据
    最简SpringBoot工程,仅有Oracle支持,可作为起始工程
    【SpringBoot/MyBatis/Oracle】如何在SpringBoot工程中配置编撰一个访问Oracle数据库的MyBatis
    【MyBatis/Oracle】通过MyBatis执行Oracle的批量插入语句,插入千万数据进一表用时1h22m59s151ms
    ASP处理多语言版本的商务网站
    XML指南——XML CDATA
    .NET环境下五种邮件发送解决方案
    什麼是WPF?
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12173192.html
Copyright © 2011-2022 走看看