Skip to content

Commit 331483d

Browse files
committed
httpClient Netty Client 未实现
1 parent 780a12c commit 331483d

14 files changed

Lines changed: 170 additions & 52 deletions

File tree

07rpc/rpc01/rpcfx-core/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
</exclusion>
5252
</exclusions>
5353
</dependency>
54-
</dependencies>
54+
<dependency>
55+
<groupId>net.bytebuddy</groupId>
56+
<artifactId>byte-buddy</artifactId>
57+
<version>1.7.11</version>
58+
</dependency>
59+
</dependencies>
5560

5661
</project>

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class RpcfxRequest {
88

99
private Object[] params;
1010

11+
private Class<?>[] parameterTypes;
12+
1113
public String getServiceClass() {
1214
return serviceClass;
1315
}
@@ -31,4 +33,12 @@ public Object[] getParams() {
3133
public void setParams(Object[] params) {
3234
this.params = params;
3335
}
36+
37+
public Class<?>[] getParameterTypes() {
38+
return parameterTypes;
39+
}
40+
41+
public void setParameterTypes(Class<?>[] parameterTypes) {
42+
this.parameterTypes = parameterTypes;
43+
}
3444
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.kimmking.rpcfx.api;
22

3-
public interface RpcfxResolver {
3+
public interface RpcfxResolver<T> {
44

5-
Object resolve(String serviceClass);
5+
<T> T resolve(Class<T> t);
66

77
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.kimmking.rpcfx.client;
19+
20+
import java.util.concurrent.ConcurrentHashMap;
21+
22+
/**
23+
* @author lw1243925457
24+
*/
25+
public class RpcProxyCache {
26+
27+
private static ConcurrentHashMap<String, Object> proxyCache = new ConcurrentHashMap<>();
28+
29+
Object getProxy(String className) {
30+
return proxyCache.get(className);
31+
}
32+
33+
Boolean isExit(String className) {
34+
return proxyCache.containsKey(className);
35+
}
36+
37+
void add(String className, Object proxy) {
38+
proxyCache.put(className, proxy);
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.kimmking.rpcfx.client;
2+
3+
import com.alibaba.fastjson.parser.ParserConfig;
4+
import io.kimmking.rpcfx.proxy.RpcfxInvocationHandler;
5+
import net.bytebuddy.ByteBuddy;
6+
import net.bytebuddy.implementation.InvocationHandlerAdapter;
7+
import net.bytebuddy.matcher.ElementMatchers;
8+
9+
public final class RpcfxByteBuddy extends RpcProxyCache implements RpcfxProxy{
10+
11+
@Override
12+
public <T> T create(final Class<T> serviceClass, final String url) {
13+
if (isExit(serviceClass.getName())) {
14+
return (T) getProxy(serviceClass.getName());
15+
}
16+
T proxy = newProxy(serviceClass, url);
17+
add(serviceClass.getName(), proxy);
18+
return proxy;
19+
}
20+
21+
public <T> T newProxy(final Class<T> serviceClass, final String url) {
22+
// 使用 ByteBuddy
23+
// 0. 替换动态代理 -> AOP
24+
T byteBuddy = null;
25+
try {
26+
byteBuddy = new ByteBuddy()
27+
.subclass(serviceClass)
28+
.method(ElementMatchers.any())
29+
.intercept(InvocationHandlerAdapter.of(new RpcfxInvocationHandler(serviceClass, url)))
30+
.make()
31+
.load(serviceClass.getClassLoader())
32+
.getLoaded().newInstance();
33+
} catch (InstantiationException e) {
34+
e.printStackTrace();
35+
} catch (IllegalAccessException e) {
36+
e.printStackTrace();
37+
}
38+
return byteBuddy;
39+
}
40+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.kimmking.rpcfx.client;
2+
3+
public interface RpcfxProxy {
4+
5+
<T> T create(final Class<T> serviceClass, final String url);
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.kimmking.rpcfx.proxy;
2+
3+
public class ByteBuddy {
4+
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java renamed to 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/proxy/RpcfxInvocationHandler.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
package io.kimmking.rpcfx.client;
2-
1+
package io.kimmking.rpcfx.proxy;
32

43
import com.alibaba.fastjson.JSON;
54
import com.alibaba.fastjson.parser.ParserConfig;
@@ -9,26 +8,13 @@
98
import okhttp3.OkHttpClient;
109
import okhttp3.Request;
1110
import okhttp3.RequestBody;
11+
import org.aopalliance.intercept.MethodInterceptor;
1212

1313
import java.io.IOException;
1414
import java.lang.reflect.InvocationHandler;
1515
import java.lang.reflect.Method;
16-
import java.lang.reflect.Proxy;
17-
18-
public final class Rpcfx {
19-
20-
static {
21-
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
22-
}
2316

24-
public static <T> T create(final Class<T> serviceClass, final String url) {
25-
26-
// 0. 替换动态代理 -> AOP
27-
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
28-
29-
}
30-
31-
public static class RpcfxInvocationHandler implements InvocationHandler {
17+
public class RpcfxInvocationHandler implements InvocationHandler {
3218

3319
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
3420

@@ -37,6 +23,7 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
3723
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
3824
this.serviceClass = serviceClass;
3925
this.url = url;
26+
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
4027
}
4128

4229
// 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
@@ -45,20 +32,25 @@ public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
4532

4633
@Override
4734
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
48-
RpcfxRequest request = new RpcfxRequest();
49-
request.setServiceClass(this.serviceClass.getName());
50-
request.setMethod(method.getName());
51-
request.setParams(params);
35+
return invokePost(method, params);
36+
}
5237

53-
RpcfxResponse response = post(request, url);
38+
private Object invokePost(Method method, Object[] params) throws IOException {
39+
RpcfxRequest request = new RpcfxRequest();
40+
request.setServiceClass(this.serviceClass.getName());
41+
request.setMethod(method.getName());
42+
request.setParams(params);
43+
request.setParameterTypes(method.getParameterTypes());
5444

55-
// 这里判断response.status,处理异常
56-
// 考虑封装一个全局的RpcfxException
45+
RpcfxResponse response = post(request, url);
5746

58-
return JSON.parse(response.getResult().toString());
59-
}
47+
// 这里判断response.status,处理异常
48+
// 考虑封装一个全局的RpcfxException
6049

61-
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
50+
return JSON.parse(response.getResult().toString());
51+
}
52+
53+
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
6254
String reqJson = JSON.toJSONString(req);
6355
System.out.println("req json: "+reqJson);
6456

@@ -73,5 +65,4 @@ private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
7365
System.out.println("resp json: "+respJson);
7466
return JSON.parseObject(respJson, RpcfxResponse.class);
7567
}
76-
}
77-
}
68+
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,31 @@ public class RpcfxInvoker {
1414

1515
private RpcfxResolver resolver;
1616

17-
public RpcfxInvoker(RpcfxResolver resolver){
17+
18+
public RpcfxInvoker(RpcfxResolver resolver) {
1819
this.resolver = resolver;
1920
}
2021

21-
public RpcfxResponse invoke(RpcfxRequest request) {
22+
public RpcfxResponse invoke(RpcfxRequest request){
2223
RpcfxResponse response = new RpcfxResponse();
2324
String serviceClass = request.getServiceClass();
24-
25-
// 作业1:改成泛型和反射
26-
Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass);
27-
25+
Object[] params = request.getParams();
26+
// 获取参数类型
2827
try {
29-
Method method = resolveMethodFromClass(service.getClass(), request.getMethod());
30-
Object result = method.invoke(service, request.getParams()); // dubbo, fastjson,
28+
// 作业1:改成泛型和反射
29+
// 先反射获取Class
30+
Class<?> aClass = Class.forName(serviceClass);
31+
// 通过 class 获取注入的bean,就可以实现去掉 @Bean(name = "XXX")中的name
32+
Object service = resolver.resolve(aClass);//this.applicationContext.getBean(serviceClass);
33+
34+
Method method = aClass.getMethod(request.getMethod(),request.getParameterTypes());
35+
// Method method = resolveMethodFromClass(service.getClass(), request.getMethod());
36+
Object result = method.invoke(service, params); // dubbo, fastjson,
3137
// 两次json序列化能否合并成一个
3238
response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName));
3339
response.setStatus(true);
3440
return response;
35-
} catch ( IllegalAccessException | InvocationTargetException e) {
41+
} catch (IllegalAccessException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException e) {
3642

3743
// 3.Xstream
3844

@@ -45,6 +51,13 @@ public RpcfxResponse invoke(RpcfxRequest request) {
4551
}
4652
}
4753

54+
/**
55+
* 此处使用反射替换
56+
*
57+
* @param klass
58+
* @param methodName
59+
* @return
60+
*/
4861
private Method resolveMethodFromClass(Class<?> klass, String methodName) {
4962
return Arrays.stream(klass.getMethods()).filter(m -> methodName.equals(m.getName())).findFirst().get();
5063
}

07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/Order.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class Order {
88

99
private float amount;
1010

11+
public Order(){}
12+
1113
public Order(int id, String name, float amount) {
1214
this.id = id;
1315
this.name = name;

0 commit comments

Comments
 (0)