1. 程式人生 > >【SSM分散式架構電商專案-15】Httpclient訪問介面服務

【SSM分散式架構電商專案-15】Httpclient訪問介面服務

對外的介面服務

package com.taotao.manage.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation
.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.taotao.common.bean.EasyUIResult; import com.taotao.manage.service.ContentService; @RequestMapping("api/content") @Controller public class ApiContentController { @Autowired private ContentService contentService;
/** * 根據內容分類id查詢分類列表 * * @param categoryId * @param page * @param rows * @return */ @RequestMapping(method = RequestMethod.GET) public ResponseEntity<EasyUIResult> queryListByCategoryId(@RequestParam("categoryId") Long categoryId, @RequestParam(value = "page"
, defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "10") Integer rows) { try { EasyUIResult easyUIResult = this.contentService.queryListByCategoryId(categoryId, page, rows); return ResponseEntity.ok(easyUIResult); } catch (Exception e) { e.printStackTrace(); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); } }

測試:
這裡寫圖片描述

訪問介面服務的方式

方式有2種:
1、 js訪問
a) 有跨域 – jsonp解決
b) 無跨域 – ajax解決
2、 Java程式碼訪問
a) Httpclient

Httpclient

這裡寫圖片描述

匯入依賴

這裡寫圖片描述

DoGET

這裡寫圖片描述

package cn.itcast.httpclient;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoGET {

    public static void main(String[] args) throws Exception {

        // 建立Httpclient物件
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 建立http GET請求
        HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java");

        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = httpclient.execute(httpGet);
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("內容:"+content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }

    }

}

帶有引數的GET請求

這裡寫圖片描述

package cn.itcast.httpclient;

import java.net.URI;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoGETParam {

    public static void main(String[] args) throws Exception {

        // 建立Httpclient物件
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 定義請求的引數
        URI uri = new URIBuilder("http://manage.taotao.com/rest/api/content").setParameter("categoryId", "33")
                .setParameter("page", "1").setParameter("rows", "1").build();

        System.out.println(uri);

        // 建立http GET請求
        HttpGet httpGet = new HttpGet(uri);

        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = httpclient.execute(httpGet);
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }

    }

}

DoPOST

這裡寫圖片描述

package cn.itcast.httpclient;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class DoPOSTParam {

    public static void main(String[] args) throws Exception {

        // 建立Httpclient物件
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 建立http POST請求
        HttpPost httpPost = new HttpPost("http://www.oschina.net/search");

        // 偽裝成瀏覽器
        httpPost.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

        // 設定2個post引數,一個是scope、一個是q
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
        parameters.add(new BasicNameValuePair("scope", "project"));
        parameters.add(new BasicNameValuePair("q", "java"));
        parameters.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
        // 構造一個form表單式的實體
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 將請求實體設定到httpPost物件中
        httpPost.setEntity(formEntity);

        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = httpclient.execute(httpPost);
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }

    }

}

帶有引數的POST請求

 public static void main(String[] args) throws Exception {

        // 建立Httpclient物件
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 建立http POST請求
        HttpPost httpPost = new HttpPost("http://www.oschina.net/search");

     // 偽裝成瀏覽器
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

        // 設定2個post引數,一個是scope、一個是q
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
        parameters.add(new BasicNameValuePair("scope", "project"));
        parameters.add(new BasicNameValuePair("q", "java"));
        parameters.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
        // 構造一個form表單式的實體
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 將請求實體設定到httpPost物件中
        httpPost.setEntity(formEntity); 

        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = httpclient.execute(httpPost);
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }

    }

連線管理器

這裡寫圖片描述
注意:
這裡寫圖片描述

package cn.itcast.httpclient;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

public class HttpConnectManager {

    public static void main(String[] args) throws Exception {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // 設定最大連線數
        cm.setMaxTotal(200);
        // 設定每個主機地址的併發數
        cm.setDefaultMaxPerRoute(20);

        doGet(cm);
        doGet(cm);
    }

    public static void doGet(HttpClientConnectionManager cm) throws Exception {
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        // 建立http GET請求
        HttpGet httpGet = new HttpGet("http://www.baidu.com/");

        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = httpClient.execute(httpGet);
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("內容長度:" + content.length());
            }
        } finally {
            if (response != null) {
                response.close();
            }
            // 此處不能關閉httpClient,如果關閉httpClient,連線池也會銷燬
            // httpClient.close();
        }
    }

}

定期關閉無效連線

public class ClientEvictExpiredConnections {

    public static void main(String[] args) throws Exception {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // 設定最大連線數
        cm.setMaxTotal(200);
        // 設定每個主機地址的併發數
        cm.setDefaultMaxPerRoute(20);

        new IdleConnectionEvictor(cm).start();
    }

    public static class IdleConnectionEvictor extends Thread {

        private final HttpClientConnectionManager connMgr;

        private volatile boolean shutdown;

        public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
            this.connMgr = connMgr;
        }

        @Override
        public void run() {
            try {
                while (!shutdown) {
                    synchronized (this) {
                        wait(5000);
                        // 關閉失效的連線
                        connMgr.closeExpiredConnections();
                    }
                }
            } catch (InterruptedException ex) {
                // 結束
            }
        }

        public void shutdown() {
            shutdown = true;
            synchronized (this) {
                notifyAll();
            }
        }
    }

}

設定請求引數

這裡寫圖片描述

Httpclient和Spring的整合

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <bean id="connectionManager"
        class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
        <!-- 最大連線數 -->
        <property name="maxTotal" value="${http.maxTotal}" />
        <!-- 設定每個主機地址的併發數 -->
        <property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
    </bean>

    <!-- Httpclient物件的構建器 -->
    <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
        <property name="connectionManager" ref="connectionManager" />
    </bean>

    <!-- Httpclient物件 -->
    <!-- 注意:該物件為多例 -->
    <bean class="org.apache.http.impl.client.CloseableHttpClient"
        factory-bean="httpClientBuilder" factory-method="build" scope="prototype">
    </bean>

    <!-- 請求配置的構建器 -->
    <bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
        <!-- 建立連線的最長時間 -->
        <property name="connectTimeout" value="${http.connectTimeout}" />
        <!-- 從連線池中獲取到連線的最長時間 -->
        <property name="connectionRequestTimeout" value="${http.connectionRequestTimeout}" />
        <!-- 資料傳輸的最長時間 -->
        <property name="socketTimeout" value="${http.socketTimeout}" />
        <!-- 提交請求前測試連線是否可用 -->
        <property name="staleConnectionCheckEnabled" value="${http.staleConnectionCheckEnabled}" />
    </bean>

    <!-- 請求配置物件 -->
    <bean class="org.apache.http.client.config.RequestConfig"
        factory-bean="requestConfigBuilder" factory-method="build" />

    <!-- 定期清理無效連線 -->
    <bean class="com.taotao.common.httpclient.IdleConnectionEvictor">
        <constructor-arg index="0" ref="connectionManager"/>
    </bean>

</beans>

這裡寫圖片描述

分析:
我們先配置連線管理器:PoolingHttpClientConnectionManager
這裡寫圖片描述
之後我們需要拿到CloseableHttpClient:

 CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

這裡分兩步,先是HttpClients.custom(),然後並設定setConnectionManager(cm)。
第二步使用build()方法生成CloseableHttpClient 物件。

第一步:
這裡寫圖片描述

這裡寫圖片描述

我們可以看到在第一步HttpClients.custom().setConnectionManager(cm)最後返回的是HttpClientBuilder物件,所以我們先來配置HttpClientBuilder的bean
這裡寫圖片描述

第二步:使用build()方法:
這裡寫圖片描述

這裡寫圖片描述

接下來我們要配置請求引數:

   // 構建請求配置資訊
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 建立連線的最長時間
                .setConnectionRequestTimeout(500) // 從連線池中獲取到連線的最長時間
                .setSocketTimeout(10 * 1000) // 資料傳輸的最長時間
                .setStaleConnectionCheckEnabled(true) // 提交請求前測試連線是否可用
                .build();

這裡我們也分兩步:第一步先是RequestConfig.custom()得到org.apache.http.client.config.RequestConfig.Builder,並且設定其屬性,第二步通過build()方法得到RequestConfig 物件。

這裡寫圖片描述
所以我們先配置 org.apache.http.client.config.RequestConfig.Builder
這裡寫圖片描述
之後我們在給它設定屬性:
這裡寫圖片描述
第二步:通過build方法得到RequestConfig
這裡寫圖片描述

接下來我們還要配置定期清理無用連線,我們在common中加入:
這裡寫圖片描述

package com.taotao.common.httpclient;

import org.apache.http.conn.HttpClientConnectionManager;

public class IdleConnectionEvictor extends Thread {

    private final HttpClientConnectionManager connMgr;

    private volatile boolean shutdown;

    public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
        this.connMgr = connMgr;
        // 啟動當前執行緒
        this.start();
    }

    @Override
    public void run() {
        try {
            while (!shutdown) {
                synchronized (this) {
                    wait(5000);
                    // 關閉失效的連線
                    connMgr.closeExpiredConnections();
                }
            }
        } catch (InterruptedException ex) {
            // 結束
        }
    }

    public void shutdown() {
        shutdown = true;
        synchronized (this) {
            notifyAll();
        }
    }
}

然後我們在配置檔案中配置:
這裡寫圖片描述
然後我們要加入外部配置檔案:
這裡寫圖片描述
在Spring配置中,載入外部配置檔案:
這裡寫圖片描述

封裝ApiService

我們來封裝使用HttpClient的通用Service,我們把它放在common中。
這裡寫圖片描述

package com.taotao.common.service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.httpclient.HttpResult;

@Service
public class ApiService implements BeanFactoryAware {

    @Autowired(required = false)
    private RequestConfig requestConfig;

    private BeanFactory beanFactory;

    /**
     * GET請求地址,響應200,返回響應的內容,響應為404、500返回null
     * 
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String doGet(String url) throws ClientProtocolException, IOException {
        // 建立http GET請求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(this.requestConfig);
        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = getHttpclient().execute(httpGet);
            // 判斷返回狀態是否為200
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;
    }

    /**
     * 帶有引數的GET請求
     * 
     * @param url
     * @param params
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws URISyntaxException
     */
    public String doGet(String url, Map<String, String> params) throws ClientProtocolException, IOException,
            URISyntaxException {
        // 定義請求的引數
        URIBuilder builder = new URIBuilder(url);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.setParameter(entry.getKey(), entry.getValue());
        }
        return this.doGet(builder.build().toString());
    }

    /**
     * 帶有引數的POST請求
     * 
     * @param url
     * @param params
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public HttpResult doPost(String url, Map<String, String> params) throws ClientProtocolException,
            IOException {
        // 建立http POST請求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(this.requestConfig);
        if (null != params) {
            List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
            for (Map.Entry<String, String> entry : params.entrySet()) {
                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            // 構造一個form表單式的實體
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
            // 將請求實體設定到httpPost物件中
            httpPost.setEntity(formEntity);
        }

        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = getHttpclient().execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (null == entity) {
                return new HttpResult(response.getStatusLine().getStatusCode(), null);
            }
            return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(
                    response.getEntity(), "UTF-8"));
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }

    /**
     * 帶有json引數的POST請求
     * 
     * @param url
     * @param params
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public HttpResult doPostJson(String url, String json) throws ClientProtocolException,
            IOException {
        // 建立http POST請求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(this.requestConfig);
        if (null != json) {
            StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
            // 將請求實體設定到httpPost物件中
            httpPost.setEntity(stringEntity);
        }

        CloseableHttpResponse response = null;
        try {
            // 執行請求
            response = getHttpclient().execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (null == entity) {
                return new HttpResult(response.getStatusLine().getStatusCode(), null);
            }
            return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(
                    response.getEntity(), "UTF-8"));
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }

    /**
     * 沒有引數的POST請求
     * 
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public HttpResult doPost(String url) throws ClientProtocolException, IOException {
        return this.doPost(url, null);
    }

    private CloseableHttpClient getHttpclient() {
        return this.beanFactory.getBean(CloseableHttpClient.class);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        // 該方法是在Spring容器初始化時會呼叫該方法,傳入beanFactory
        this.beanFactory = beanFactory;
    }
}

異常處理:
這裡寫圖片描述

get返回值是根據狀態碼為200則返回資料,狀態碼不為200則返回null。post的返回值必須返回響應狀態碼和內容,比如返回狀態碼為201的時候同時也會返回內容,所以我們構造一個物件來返回:
這裡寫圖片描述

package com.taotao.common.httpclient;

public class HttpResult {

    private Integer code;

    private String body;

    public HttpResult() {

    }
    public HttpResult(Integer code, String body) {
        this.code = code;
        this.body = body;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

}

然後我們再來看一個單例物件中如何使用多例的物件的問題:
這裡寫圖片描述

原本我們應該這樣寫:
這裡寫圖片描述
然後我們通過注入的httpclient使用httpclient.execute(httpGet);得到CloseableHttpResponse物件。但是因為我們註解@Service的ApiService是單例的,只會例項化一次,例項化一次的時候只會注入一次httpclient,那麼我們的httpclient就變成單例的了。然而我們的httpclient必須是個多例物件。那我們可能會想把ApiService變成多例的,加@Scope(“prototype”)就可以了。那麼如果有其他的Service或者Controller要呼叫這個ApiService是不是也是同樣的情況,也是在單例物件中使用多例物件。所以這個方法不可行。那麼我們如何在單例物件中使用多例物件?
解決方法就是:httpclient這個物件不能注入,而是每次使用的時候,通過容器裡面去獲取,因為在容器裡面指定httpclient是多例的。
所以主要的程式碼截圖下:
這裡寫圖片描述
這裡寫圖片描述
通過以上步驟就可以達到在單例物件中使用多例物件。