-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestapiSource.java
310 lines (261 loc) · 9.8 KB
/
RestapiSource.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.fs.source;
import com.google.gson.Gson;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ServiceUnavailableRetryStrategy;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
* ParameterTool parameterTool = ParameterTool.fromArgs(args);
* String header = "{'app-token': '$2a$10$W/mKbzvGuYZ'}";
* DataStreamSource<String> source = env.addSource(new RestapiSource(url, "GET",header,null,1000L));
* source.map(s -> s).print();
* env.execute();
*/
public class RestapiSource extends RichParallelSourceFunction<String> {
protected String url;
protected String method;
protected transient CloseableHttpClient httpClient;
protected String header;
protected String requestBody;
protected long delay;
private boolean running;
public RestapiSource(String url, String method, String header, String requestBody, long delay) {
this.url = url;
this.method = method;
this.header = header;
this.requestBody = requestBody;
this.delay = delay;
}
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
running = true;
httpClient = HttpUtil.getHttpClient();
}
@Override
public void run(SourceContext<String> sourceContext) throws Exception {
HttpUriRequest request = HttpUtil.getRequest(method, requestBody, header, url);
while (running) {
try {
CloseableHttpResponse httpResponse = httpClient.execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
String entityData = EntityUtils.toString(entity);
sourceContext.collect(entityData);
} else {
throw new RuntimeException("entity is null");
}
} catch (Exception e) {
throw new RuntimeException("get entity error");
}
Thread.sleep(delay);
}
}
@Override
public void close() throws Exception {
HttpUtil.closeClient(httpClient);
super.close();
}
@Override
public void cancel() {
running = false;
}
}
enum HttpMethod {
// http 请求方式
GET,
POST,
PUT,
PATCH,
DELETE,
COPY,
HEAD,
OPTIONS,
LINK,
UNLINK,
PURGE,
LOCK,
UNLOCK,
PROPFIND,
VIEW
}
class HttpUtil {
protected static final Logger LOG = LoggerFactory.getLogger(HttpUtil.class);
private static final int COUNT = 32;
private static final int TOTAL_COUNT = 1000;
private static final int TIME_OUT = 5000;
private static final int EXECUTION_COUNT = 5;
public static Gson gson = new Gson();
public static CloseableHttpClient getHttpClient() {
// 设置自定义的重试策略
MyServiceUnavailableRetryStrategy strategy = new MyServiceUnavailableRetryStrategy
.Builder()
.executionCount(EXECUTION_COUNT)
.retryInterval(1000)
.build();
// 设置自定义的重试Handler
MyHttpRequestRetryHandler retryHandler = new MyHttpRequestRetryHandler
.Builder()
.executionCount(EXECUTION_COUNT)
.build();
// 设置超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(TIME_OUT)
.setConnectionRequestTimeout(TIME_OUT)
.setSocketTimeout(TIME_OUT)
.build();
// 设置Http连接池
PoolingHttpClientConnectionManager pcm = new PoolingHttpClientConnectionManager();
pcm.setDefaultMaxPerRoute(COUNT);
pcm.setMaxTotal(TOTAL_COUNT);
return HttpClientBuilder.create()
.setServiceUnavailableRetryStrategy(strategy)
.setRetryHandler(retryHandler)
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(pcm)
.build();
// return HttpClientBuilder.create().build();
}
public static HttpRequestBase getRequest(String method,
String requestBody,
String header,
String url) {
LOG.debug("current request url: {} current method:{} \n", url, method);
HttpRequestBase request = null;
if (HttpMethod.GET.name().equalsIgnoreCase(method)) {
request = new HttpGet(url);
} else if (HttpMethod.POST.name().equalsIgnoreCase(method)) {
HttpPost post = new HttpPost(url);
post.setEntity(getEntityData(requestBody));
request = post;
} else {
throw new RuntimeException("Unsupported method:" + method);
}
Map<String, String> headerMap = gson.fromJson(header, Map.class);
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
return request;
}
public static void closeClient(CloseableHttpClient httpClient) {
try {
httpClient.close();
} catch (IOException e) {
throw new RuntimeException("close client error");
}
}
/**
* @param body 为json字符串
* @return
*/
public static StringEntity getEntityData(String body) {
StringEntity stringEntity = new StringEntity(body, StandardCharsets.UTF_8);
stringEntity.setContentEncoding(StandardCharsets.UTF_8.name());
return stringEntity;
}
}
class MyServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy {
private int executionCount;
private long retryInterval;
public MyServiceUnavailableRetryStrategy(MyServiceUnavailableRetryStrategy.Builder builder) {
this.executionCount = builder.executionCount;
this.retryInterval = builder.retryInterval;
}
@Override
public boolean retryRequest(HttpResponse httpResponse, int executionCount, HttpContext httpContext) {
int successCode = 200;
return httpResponse.getStatusLine().getStatusCode() != successCode
&& executionCount < this.executionCount;
}
@Override
public long getRetryInterval() {
return this.retryInterval;
}
public static final class Builder {
private int executionCount;
private long retryInterval;
public Builder() {
executionCount = 5;
retryInterval = 2000;
}
public MyServiceUnavailableRetryStrategy.Builder executionCount(int executionCount) {
this.executionCount = executionCount;
return this;
}
public MyServiceUnavailableRetryStrategy.Builder retryInterval(long retryInterval) {
this.retryInterval = retryInterval;
return this;
}
public MyServiceUnavailableRetryStrategy build() {
return new MyServiceUnavailableRetryStrategy(this);
}
}
}
class MyHttpRequestRetryHandler implements HttpRequestRetryHandler {
protected static final Logger LOG = LoggerFactory.getLogger(MyHttpRequestRetryHandler.class);
private int executionMaxCount;
public MyHttpRequestRetryHandler(MyHttpRequestRetryHandler.Builder builder) {
this.executionMaxCount = builder.executionMaxCount;
}
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
LOG.info("第" + executionCount + "次重试");
if (executionCount >= this.executionMaxCount) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
return true;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return true;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return true;
}
if (exception instanceof NoHttpResponseException) {
// No response
return true;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
// Retry if the request is considered idempotent
return !idempotent;
}
public static final class Builder {
private int executionMaxCount;
public Builder() {
executionMaxCount = 5;
}
public MyHttpRequestRetryHandler.Builder executionCount(int executionCount) {
this.executionMaxCount = executionCount;
return this;
}
public MyHttpRequestRetryHandler build() {
return new MyHttpRequestRetryHandler(this);
}
}
}