Skip to content

Commit 8d5ea10

Browse files
committed
remove some sout
1 parent c6a07a3 commit 8d5ea10

File tree

2 files changed

+95
-81
lines changed

2 files changed

+95
-81
lines changed

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -103,85 +103,4 @@ private static <T> Object create(Class<T> serviceClass, List<String> invokers, R
103103
new RpcfxInvocationHandler(serviceClass, invokers, router,loadBalance, filters));
104104
}
105105

106-
public static class RpcfxInvocationHandler implements InvocationHandler {
107-
108-
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
109-
110-
private final Class<?> serviceClass;
111-
private final List<String> invokers;
112-
private final Router router;
113-
private final LoadBalancer loadBalance;
114-
private final Filter[] filters;
115-
116-
public <T> RpcfxInvocationHandler(Class<T> serviceClass, List<String> invokers, Router router, LoadBalancer loadBalance, Filter... filters) {
117-
this.serviceClass = serviceClass;
118-
this.invokers = invokers;
119-
this.router = router;
120-
this.loadBalance = loadBalance;
121-
this.filters = filters;
122-
}
123-
124-
// 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
125-
// int byte char float double long bool
126-
// [], data class
127-
128-
@Override
129-
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
130-
131-
List<String> urls = router.route(invokers);
132-
// System.out.println("router.route => ");
133-
// urls.forEach(System.out::println);
134-
String url = loadBalance.select(urls); // router, loadbalance
135-
// System.out.println("loadBalance.select => ");
136-
// System.out.println("final => " + url);
137-
138-
if (url == null) {
139-
throw new RuntimeException("No available providers from registry center.");
140-
}
141-
142-
// 加filter地方之二
143-
// mock == true, new Student("hubao");
144-
145-
RpcfxRequest request = new RpcfxRequest();
146-
request.setServiceClass(this.serviceClass.getName());
147-
request.setMethod(method.getName());
148-
request.setParams(params);
149-
150-
if (null!=filters) {
151-
for (Filter filter : filters) {
152-
if (!filter.filter(request)) {
153-
return null;
154-
}
155-
}
156-
}
157-
158-
RpcfxResponse response = post(request, url);
159-
160-
// 加filter地方之三
161-
// Student.setTeacher("cuijing");
162-
163-
// 这里判断response.status,处理异常
164-
// 考虑封装一个全局的RpcfxException
165-
166-
return JSON.parse(response.getResult().toString());
167-
}
168-
169-
OkHttpClient client = new OkHttpClient();
170-
171-
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
172-
String reqJson = JSON.toJSONString(req);
173-
// System.out.println("req json: "+reqJson);
174-
175-
// 1.可以复用client
176-
// 2.尝试使用httpclient或者netty client
177-
178-
final Request request = new Request.Builder()
179-
.url(url)
180-
.post(RequestBody.create(JSONTYPE, reqJson))
181-
.build();
182-
String respJson = client.newCall(request).execute().body().string();
183-
// System.out.println("resp json: "+respJson);
184-
return JSON.parseObject(respJson, RpcfxResponse.class);
185-
}
186-
}
187106
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.kimmking.rpcfx.client;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import io.kimmking.rpcfx.api.*;
5+
import okhttp3.MediaType;
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.RequestBody;
9+
10+
import java.io.IOException;
11+
import java.lang.reflect.InvocationHandler;
12+
import java.lang.reflect.Method;
13+
import java.util.List;
14+
15+
public class RpcfxInvocationHandler implements InvocationHandler {
16+
17+
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
18+
19+
private final Class<?> serviceClass;
20+
private final List<String> invokers;
21+
private final Router router;
22+
private final LoadBalancer loadBalance;
23+
private final Filter[] filters;
24+
25+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, List<String> invokers, Router router, LoadBalancer loadBalance, Filter... filters) {
26+
this.serviceClass = serviceClass;
27+
this.invokers = invokers;
28+
this.router = router;
29+
this.loadBalance = loadBalance;
30+
this.filters = filters;
31+
}
32+
33+
// 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
34+
// int byte char float double long bool
35+
// [], data class
36+
37+
@Override
38+
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
39+
40+
List<String> urls = router.route(invokers);
41+
// System.out.println("router.route => ");
42+
// urls.forEach(System.out::println);
43+
String url = loadBalance.select(urls); // router, loadbalance
44+
// System.out.println("loadBalance.select => ");
45+
// System.out.println("final => " + url);
46+
47+
if (url == null) {
48+
throw new RuntimeException("No available providers from registry center.");
49+
}
50+
51+
// 加filter地方之二
52+
// mock == true, new Student("hubao");
53+
54+
RpcfxRequest request = new RpcfxRequest();
55+
request.setServiceClass(this.serviceClass.getName());
56+
request.setMethod(method.getName());
57+
request.setParams(params);
58+
59+
if (null!=filters) {
60+
for (Filter filter : filters) {
61+
if (!filter.filter(request)) {
62+
return null;
63+
}
64+
}
65+
}
66+
67+
RpcfxResponse response = post(request, url);
68+
69+
// 加filter地方之三
70+
// Student.setTeacher("cuijing");
71+
72+
// 这里判断response.status,处理异常
73+
// 考虑封装一个全局的RpcfxException
74+
75+
return JSON.parse(response.getResult().toString());
76+
}
77+
78+
OkHttpClient client = new OkHttpClient();
79+
80+
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
81+
String reqJson = JSON.toJSONString(req);
82+
// System.out.println("req json: "+reqJson);
83+
84+
// 1.可以复用client
85+
// 2.尝试使用httpclient或者netty client
86+
87+
final Request request = new Request.Builder()
88+
.url(url)
89+
.post(RequestBody.create(JSONTYPE, reqJson))
90+
.build();
91+
String respJson = client.newCall(request).execute().body().string();
92+
// System.out.println("resp json: "+respJson);
93+
return JSON.parseObject(respJson, RpcfxResponse.class);
94+
}
95+
}

0 commit comments

Comments
 (0)