Skip to content

Commit 88c1299

Browse files
committed
第三周作业
1 parent dccd797 commit 88c1299

3 files changed

Lines changed: 137 additions & 1 deletion

File tree

02nio/nio02/pom.xml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,33 @@
5757
<groupId>org.projectlombok</groupId>
5858
<artifactId>lombok</artifactId>
5959
</dependency>
60-
60+
61+
62+
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
63+
<dependency>
64+
<groupId>org.apache.httpcomponents</groupId>
65+
<artifactId>httpcore</artifactId>
66+
<version>4.4.10</version>
67+
</dependency>
68+
69+
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
70+
<dependency>
71+
<groupId>org.apache.httpcomponents</groupId>
72+
<artifactId>httpclient</artifactId>
73+
<version>4.5.6</version>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>com.squareup.okhttp3</groupId>
78+
<artifactId>okhttp</artifactId>
79+
<version>3.3.0</version>
80+
</dependency>
81+
82+
<dependency>
83+
<groupId>cn.hutool</groupId>
84+
<artifactId>hutool-all</artifactId>
85+
<version>5.7.3</version>
86+
</dependency>
6187
<!--
6288
<dependency>
6389
<groupId>org.springframework.boot</groupId>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.kimmking.gateway.filter;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.handler.codec.http.FullHttpRequest;
5+
6+
/**
7+
* @author yangcheng
8+
* @version 1.0
9+
* @date 2021-07-11 23:21
10+
* @description
11+
*/
12+
public class ProxyRequestFilter implements HttpRequestFilter {
13+
@Override
14+
public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
15+
//获取url
16+
String uri = fullRequest.uri();
17+
System.out.println("当前请求的"+uri);
18+
//设置过滤规则
19+
//例如 只有anxing 开头的请求才能通过
20+
if(!uri.startsWith("anxing")){
21+
throw new RuntimeException("不支持的请求,请重试");
22+
}else{
23+
//解析请求并调用
24+
//之前做过一次通过反射调用指定的类
25+
//纯路由功能可以调用okhttp或者httpclient来远程调用请求
26+
}
27+
28+
29+
}
30+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,84 @@
11
package io.github.kimmking.gateway.outbound.okhttp;
22

3+
import cn.hutool.json.JSONUtil;
4+
import okhttp3.*;
5+
6+
import java.io.IOException;
7+
38
public class OkhttpOutboundHandler {
9+
private OkHttpClient okHttpClient = new OkHttpClient();
10+
11+
public String base_url = "http://localhost:8801";
12+
13+
/**
14+
* get 请求,常用于获取请求
15+
*
16+
* @param api
17+
*/
18+
public void testGet(String api) throws IOException {
19+
Request request = new Request.Builder()
20+
.url(base_url + api)
21+
.get()
22+
.build();
23+
Call call = okHttpClient.newCall(request);
24+
Response response = call.execute();
25+
System.out.println(response.body().toString());
26+
27+
}
28+
29+
/**
30+
* put 请求,常用于更新请求
31+
*
32+
* @param api
33+
*/
34+
public void testPut(String api) throws IOException {
35+
TestModel testModel = new TestModel();
36+
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
37+
JSONUtil.toJsonStr(testModel));
38+
Request request = new Request.Builder()
39+
.url(base_url + api)
40+
.put(requestBody)
41+
.build();
42+
Call call = okHttpClient.newCall(request);
43+
Response response = call.execute();
44+
System.out.println(response.body().toString());
45+
}
46+
47+
/**
48+
* POST 请求,常用于新增或复杂的获取请求
49+
*
50+
* @param api
51+
*/
52+
public void testPost(String api) throws IOException {
53+
TestModel testModel = new TestModel();
54+
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
55+
JSONUtil.toJsonStr(testModel));
56+
Request request = new Request.Builder()
57+
.url(base_url + api)
58+
.post(requestBody)
59+
.build();
60+
Call call = okHttpClient.newCall(request);
61+
Response response = call.execute();
62+
System.out.println(response.body().toString());
63+
}
64+
65+
/**
66+
* DELETE 请求,常用于删除请求
67+
*
68+
* @param api
69+
*/
70+
public void testDelete(String api) throws IOException {
71+
Request request = new Request.Builder()
72+
.url(base_url + api)
73+
.delete()
74+
.build();
75+
Call call = okHttpClient.newCall(request);
76+
Response response = call.execute();
77+
System.out.println(response.body().toString());
78+
}
79+
80+
private class TestModel {
81+
private Integer testVo;
82+
}
83+
484
}

0 commit comments

Comments
 (0)