• RestTemplate配置和使用


    在项目中,如果要调用第三方的http服务,就需要发起http请求,常用的请求方式:第一种,使用java原生发起http请求,这种方式不需要引入第三方库,但是连接不可复用,如果要实现连接复用,需要自己实现管理,相对来说比较麻烦;第二种就是使用第三方的库,比较常用的就是apache的httpclient和okhttp两个包,他们都对http请求进行了封装并且可以管理连接,对于重复使用的连接会有比较好的性能;第三种就是在springboot中使用RestTemplate进行请求,它是对http请求的封装,默认使用的java原生进行请求,也支持整合httpclient和okhttp库,提供很好的封装和扩展性。下面就介绍一下如何在项目中使用和相关配置:
    要在项目中使用RestTemplate,首先需要定义一个Bean将它注入到系统中:

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    • 1
    • 2
    • 3
    • 4

    这种方式注入的对象默认使用SimpleClientHttpRequestFactory工厂,它使用java原生方式发起http请求,性能并不好,还有一种注入方式是:

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
    
    • 1
    • 2
    • 3
    • 4

    RestTemplateBuilder这种注入方式会根据系统中是否引入第三方的http库来灵活选择httpclient工厂,通过跟踪源码发现,目前支持httpclient和okhttp3两个库,如下图:
    判断factory类型
    默认支持的两种factory

    虽然通过上面的方式注入的RestTemplate会自动选择创建httpclient的工厂,但是该工厂相关参数都是默认配置,有时候并不能满足我们系统的需求,比如要设置读写超时时间,默认的配置中是永远不超时,这样如果请求的第三方系统响应慢会导致整个系统崩溃。
    不加任何配置的RestTemplate内容如下:
    restTemplate的默认工厂和参数
    要调整超时时间,就需要使用下面这种方式注入工厂,并配置相关参数:

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(3000);
        factory.setReadTimeout(3000);
        return factory;
    }
    
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    再次查看RestTemplate的内容:
    设置参数后的restTemplate
    通常情况下我们都不会使用java原生的方式发起http请求,因为每次发起http请求都要重新建立连接,这样会大大降低系统性能,在这个示例中我选择使用apache下的httpclient库扩展,首先需要在项目中引入相关依赖:

    <dependency>
        <groupId>org.apache.httpcomponentsgroupId>
        <artifactId>httpclientartifactId>
        <version>4.5.12version>
    dependency>
    <dependency>
        <groupId>org.apache.httpcomponentsgroupId>
        <artifactId>httpmimeartifactId>
        <version>4.5.12version>
    dependency>
    <dependency>
        <groupId>org.apache.httpcomponentsgroupId>
        <artifactId>httpcoreartifactId>
        <version>4.4.13version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    注意包的版本要匹配,否则会有报错,接下来就是将httpclient注入到RestTemplate中,这里有两种方式,第一种是使用下面的配置

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
    
    • 1
    • 2
    • 3
    • 4

    它可以检测到系统中引入了httpclient依赖,会自动将原生的工厂类替换为apacheHttpClient的工厂:
    请添加图片描述
    但是这种方式使用的配置都是默认的,如连接池大小为20个,超时时间永不超时等。这种配置不能满足我们的需求,这时候就需要根据自己系统的要求调整配置参数了,下面的示例代码是我的一个简单配置:

    import org.apache.http.HttpEntityEnclosingRequest;
    import org.apache.http.HttpRequest;
    import org.apache.http.NoHttpResponseException;
    import org.apache.http.client.HttpRequestRetryHandler;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.protocol.HttpClientContext;
    import org.apache.http.config.Registry;
    import org.apache.http.config.RegistryBuilder;
    import org.apache.http.conn.ConnectTimeoutException;
    import org.apache.http.conn.UnsupportedSchemeException;
    import org.apache.http.conn.socket.ConnectionSocketFactory;
    import org.apache.http.conn.socket.PlainConnectionSocketFactory;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.client.ClientHttpRequestFactory;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
    
    import javax.net.ssl.SSLException;
    import java.io.InterruptedIOException;
    import java.net.SocketException;
    import java.net.SocketTimeoutException;
    import java.net.UnknownHostException;
    import java.util.concurrent.TimeUnit;
    
    @Configuration
    public class RestTemplateConfig {
    
        @Bean
        public ClientHttpRequestFactory apacheHttpRequestFactory() {
            // 重试配置
            HttpRequestRetryHandler retryHandler = (e, count, context) -> {
                System.out.println("连接失败次数|" + count);
                e.printStackTrace();
    
                if (count >= 5) {       // 假设已经重试了5次,就放弃
                    return false;
                }
                // 返回true需要重试
                if (e instanceof NoHttpResponseException            // 请求无响应就重试
                        || e instanceof ConnectTimeoutException     // 连接超时
                        || e instanceof SocketException             // 连接异常
                        || e instanceof SocketTimeoutException      // socket超时
                ) {
                    try {
                        // 重试时间间隔:1s
                        TimeUnit.MILLISECONDS.sleep(1000);
                    } catch (InterruptedException ex) {
                        e.printStackTrace();
                    }
                    return true;
                }
                // 返回false不需要重试
                if (e instanceof SSLException                       // SSL握手异常不要重试
                        || e instanceof InterruptedIOException      // 中断
                        || e instanceof UnknownHostException        // 目标server不可达
                        || e instanceof UnsupportedSchemeException  // 协议不支持
                ) {
                    return false;
                }
    
                HttpClientContext clientContext = HttpClientContext.adapt(context);
                HttpRequest request = clientContext.getRequest();
                // 假设请求是幂等的,就再次尝试
                return !(request instanceof HttpEntityEnclosingRequest);
            };
    
            Registry<ConnectionSocketFactory> registry = RegistryBuilder
                    .<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.INSTANCE)
                    .register("https", SSLConnectionSocketFactory.getSocketFactory())
                    .build();
    
            // 连接池配置
            PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
            // 最大连接数
            connManager.setMaxTotal(1000);
            // 每个路由最大连接数
            connManager.setDefaultMaxPerRoute(200);
    
            // 超时配置:都为5s
            RequestConfig reqConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(5000)      // 从连接池中获取连接超时时间
                    .setConnectTimeout(5000)                // 连接建立超时时间,也就是三次握手完成时间
                    .setSocketTimeout(5000)                 // 等待服务器响应超时时间
                    .build();
    
            // 构建httpclient
            CloseableHttpClient client = HttpClients.custom()
                    .setConnectionManager(connManager)
                    .setDefaultRequestConfig(reqConfig)
                    .setRetryHandler(retryHandler)
                    .build();
    
            return new HttpComponentsClientHttpRequestFactory(client);
        }
    
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate(apacheHttpRequestFactory());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    通过上面的配置就将自定义的连接参数注入到RestTemplate中了。在使用的位置,只需要引入restTemplate就可以正常使用了:

    @Autowired
    private RestTemplate restTemplate;
    
    • 1
    • 2

    使用restTemplate发起http请求的方法封装为两种形式:一种是ForObject(),如getForObject()、postForObject(),这种只会返回数据;另一种是ForEntity(),如getForEntity()、postForEntity(),这种方式会返回数据和状态信息。
    get请求比较简单,下面主要演示一下post请求中的提交表单数据和json数据的示例:

    1. 提交表单数据:
    // 请求地址
    String url = "http://localhost:8081/test/add";
    // form表单数据
    MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();
    data.add("name", "james");
    data.add("age", 38);
    // 请求头部
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    // 发起请求
    ResponseEntity<String> result = restTemplate.postForEntity(url, new HttpEntity<>(data, headers), String.class);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 提交json数据:
    // 请求地址
    String url = "http://localhost:8081/test/add";
    // json数据
    String data = "{\"name\":\"james\",\"age\":38}";
    // 请求头部
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    // 发起请求
    ResponseEntity<String> result = restTemplate.postForEntity(url, new HttpEntity<>(data, headers), String.class);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    【编程之路】面试必刷TOP101:链表(11-16,Python实现)
    springboot 简单配置mongodb多数据源
    Windows系统下环境安装
    第四章 串【24王道数据结构笔记】
    RabbitMQ系列【2】核心概念
    2741. 特别的排列 Medium
    Kafka磁盘写满日志清理操作
    人工智能之双色球预测系统(指针)
    Java String,char,int类型的相互转换
    MQTT- emqx 安装指南
  • 原文地址:https://blog.csdn.net/dream_xin2013/article/details/134294017