# 079. 在 storm 拓扑中加入 nginx 反向推送缓存热点与缓存数据的代码逻辑

# httpClient 工具封装

compile 'org.apache.httpcomponents:httpclient:4.4'
1

这个工具应该都会的

package cn.mrcode.cachepdp.eshop.storm.http;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * HttpClient工具类
 *
 * @author lixuerui
 */
@SuppressWarnings("deprecation")
public class HttpClientUtils {

    /**
     * 发送GET请求
     *
     * @param url 请求URL
     * @return 响应结果
     */
    @SuppressWarnings("resource")
    public static String sendGetRequest(String url) {
        String httpResponse = null;

        HttpClient httpclient = null;
        InputStream is = null;
        BufferedReader br = null;

        try {
            // 发送GET请求
            httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);

            // 处理响应
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                is = entity.getContent();
                br = new BufferedReader(new InputStreamReader(is));

                StringBuffer buffer = new StringBuffer("");
                String line = null;

                while ((line = br.readLine()) != null) {
                    buffer.append(line + "\n");
                }

                httpResponse = buffer.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

        return httpResponse;
    }

    /**
     * 发送post请求
     *
     * @param url URL
     * @param map 参数Map
     */
    @SuppressWarnings({"rawtypes", "unchecked", "resource"})
    public static String sendPostRequest(String url, Map<String, String> map) {
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;

        try {
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(url);

            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
                httpPost.setEntity(entity);
            }

            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, "utf-8");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }

        return result;
    }

}


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

# 热点商品推送 nginx 代码逻辑

private void pushHotToNginx(Long pid) {
    // 降级策略推送到分发层 nginx
    String distributeNginxURL = "http://eshop-cache03/hot?productId=" + pid;
    HttpClientUtils.sendGetRequest(distributeNginxURL);

    // 获取商品信息
    String cacheServiceURL = "http://192.168.0.99:6002/getProductInfo?productId=" + pid;
    String response = HttpClientUtils.sendGetRequest(cacheServiceURL);
    try {
        response = URLEncoder.encode(response, "utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    // 推送到应用层 nginx
    String[] appNginxURLs = new String[]{
            "http://eshop-cache01/hot?productId=" + pid + "&productInfo=" + response,
            "http://eshop-cache02/hot?productId=" + pid + "&productInfo=" + response
    };
    for (String appNginxURL : appNginxURLs) {
        HttpClientUtils.sendGetRequest(appNginxURL);
    }
}

public static void main(String[] args) throws UnsupportedEncodingException {
    // 获取商品信息
    String cacheServiceURL = "http://192.168.0.99:6002/getProductInfo?productId=" + 1;
    String response = HttpClientUtils.sendGetRequest(cacheServiceURL);

    String url = "http://192.168.0.99:6002/test?productId=" + 1 + "&productInfo=" + URLEncoder.encode(response, "UTF-8");
    HttpClientUtils.sendGetRequest(url);
}
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