Skip to content

Commit 8949925

Browse files
committed
# Conflicts: # 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java # 07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java # 07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java
2 parents 6186f94 + bb818e2 commit 8949925

39 files changed

Lines changed: 4854 additions & 77 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,31 @@
3232
<groupId>net.bytebuddy</groupId>
3333
<artifactId>byte-buddy</artifactId>
3434
</dependency>
35+
<dependency>
36+
<groupId>org.projectlombok</groupId>
37+
<artifactId>lombok</artifactId>
38+
<version>1.18.16</version>
39+
</dependency>
40+
3541
<dependency>
3642
<groupId>com.squareup.okhttp3</groupId>
3743
<artifactId>okhttp</artifactId>
3844
<version>3.12.2</version>
3945
</dependency>
4046

47+
48+
<dependency>
49+
<groupId>org.apache.curator</groupId>
50+
<artifactId>curator-client</artifactId>
51+
<version>5.1.0</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.apache.curator</groupId>
56+
<artifactId>curator-framework</artifactId>
57+
<version>5.1.0</version>
58+
</dependency>
59+
4160
<dependency>
4261
<groupId>org.springframework.boot</groupId>
4362
<artifactId>spring-boot-starter</artifactId>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
public interface Filter {
4+
5+
boolean filter(RpcfxRequest request);
6+
7+
// Filter next();
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
import java.util.List;
4+
5+
public interface LoadBalancer {
6+
7+
String select(List<String> urls);
8+
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
import java.util.List;
4+
5+
public interface Router {
6+
7+
List<String> route(List<String> urls);
8+
}
Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
package io.kimmking.rpcfx.api;
22

3-
public class RpcfxRequest {
3+
import lombok.Data;
44

5+
@Data
6+
public class RpcfxRequest {
57
private String serviceClass;
6-
78
private String method;
8-
99
private Object[] params;
10-
11-
public String getServiceClass() {
12-
return serviceClass;
13-
}
14-
15-
public void setServiceClass(String serviceClass) {
16-
this.serviceClass = serviceClass;
17-
}
18-
19-
public String getMethod() {
20-
return method;
21-
}
22-
23-
public void setMethod(String method) {
24-
this.method = method;
25-
}
26-
27-
public Object[] getParams() {
28-
return params;
29-
}
30-
31-
public void setParams(Object[] params) {
32-
this.params = params;
33-
}
3410
}
Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
package io.kimmking.rpcfx.api;
22

3-
public class RpcfxResponse {
3+
import lombok.Data;
44

5+
@Data
6+
public class RpcfxResponse {
57
private Object result;
6-
78
private boolean status;
8-
99
private Exception exception;
10-
11-
public Object getResult() {
12-
return result;
13-
}
14-
15-
public void setResult(Object result) {
16-
this.result = result;
17-
}
18-
19-
public boolean isStatus() {
20-
return status;
21-
}
22-
23-
public void setStatus(boolean status) {
24-
this.status = status;
25-
}
26-
27-
public Exception getException() {
28-
return exception;
29-
}
30-
31-
public void setException(Exception exception) {
32-
this.exception = exception;
33-
}
3410
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
@Data
7+
@Builder
8+
public class ServiceProviderDesc {
9+
10+
private String host;
11+
private Integer port;
12+
private String serviceClass;
13+
14+
// group
15+
// version
16+
}

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

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.bytebuddy.ByteBuddy;
1111
import net.bytebuddy.implementation.InvocationHandlerAdapter;
1212
import net.bytebuddy.matcher.ElementMatchers;
13+
import io.kimmking.rpcfx.api.*;
1314
import okhttp3.MediaType;
1415
import okhttp3.internal.http2.ErrorCode;
1516

@@ -18,20 +19,38 @@
1819
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
2021
import java.lang.reflect.Proxy;
21-
import java.util.HashMap;
22-
import java.util.Map;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
2326

2427
public final class Rpcfx {
2528

2629
static {
2730
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
2831
}
2932

30-
public static <T> T create(final Class<T> serviceClass, final String url)
31-
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
33+
public static <T, filters> T createFromRegistry(final Class<T> serviceClass, final String zkUrl, Router router, LoadBalancer loadBalance, Filter filter) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
34+
35+
// 加filte之一
36+
37+
// curator Provider list from zk
38+
List<String> invokers = new ArrayList<>();
39+
// 1. 简单:从zk拿到服务提供的列表
40+
// 2. 挑战:监听zk的临时节点,根据事件更新这个list(注意,需要做个全局map保持每个服务的提供者List)
41+
42+
List<String> urls = router.route(invokers);
43+
44+
String url = loadBalance.select(urls); // router, loadbalance
45+
46+
return (T) create(serviceClass, url, filter);
47+
48+
}
49+
50+
public static <T> T create(final Class<T> serviceClass, final String url, Filter... filters) {
3251

33-
// 0. 替换动态代理 -> 字节码增强
34-
return serviceClass.cast(getByteBuddyProxy(serviceClass, url));
52+
// 0. 替换动态代理 -> AOP
53+
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url, filters));
3554
}
3655
private static <T> Object getByteBuddyProxy(Class<T> serviceClass, String url)
3756
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
@@ -54,22 +73,42 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
5473

5574
private final Class<?> serviceClass;
5675
private final String url;
57-
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
76+
private final Filter[] filters;
77+
78+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url, Filter... filters) {
5879
this.serviceClass = serviceClass;
5980
this.url = url;
81+
this.filters = filters;
6082
}
6183

6284
// TODO: 2020/12/17 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
6385
// int byte char float double long bool
6486
// [], data class
6587
@Override
6688
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
89+
90+
// 加filter地方之二
91+
// mock == true, new Student("hubao");
92+
6793
RpcfxRequest request = new RpcfxRequest();
6894
request.setServiceClass(this.serviceClass.getName());
6995
request.setMethod(method.getName());
7096
request.setParams(params);
7197

98+
if (null!=filters) {
99+
for (Filter filter : filters) {
100+
if (!filter.filter(request)) {
101+
return null;
102+
}
103+
}
104+
}
105+
72106
RpcfxResponse response = post(request, url);
107+
108+
109+
// 加filter地方之三
110+
// Student.setTeacher("cuijing");
111+
73112
// 这里判断response.status,处理异常
74113
// 考虑封装一个全局的RpcfxException
75114
if(!response.isStatus()){

07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.kimmking.rpcfx.demo.consumer;
22

3+
import io.kimmking.rpcfx.api.Filter;
4+
import io.kimmking.rpcfx.api.LoadBalancer;
5+
import io.kimmking.rpcfx.api.Router;
6+
import io.kimmking.rpcfx.api.RpcfxRequest;
37
import io.kimmking.rpcfx.client.Rpcfx;
48
import io.kimmking.rpcfx.demo.api.Order;
59
import io.kimmking.rpcfx.demo.api.OrderService;
@@ -8,6 +12,7 @@
812
import org.springframework.boot.autoconfigure.SpringBootApplication;
913

1014
import java.lang.reflect.InvocationTargetException;
15+
import java.util.List;
1116

1217
@SpringBootApplication
1318
public class RpcfxClientApplication {
@@ -30,9 +35,36 @@ public static void main(String[] args) throws InvocationTargetException, NoSuchM
3035
Order order = orderService.findOrderById(1992129);
3136
System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount()));
3237

33-
// 新加一个OrderService
38+
//
39+
UserService userService2 = Rpcfx.createFromRegistry(UserService.class, "localhost:2181", new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter());
3440

3541
// SpringApplication.run(RpcfxClientApplication.class, args);
3642
}
3743

44+
private static class TagRouter implements Router {
45+
@Override
46+
public List<String> route(List<String> urls) {
47+
return urls;
48+
}
49+
}
50+
51+
private static class RandomLoadBalancer implements LoadBalancer {
52+
@Override
53+
public String select(List<String> urls) {
54+
return urls.get(0);
55+
}
56+
}
57+
58+
59+
private static class CuicuiFilter implements Filter {
60+
@Override
61+
public boolean filter(RpcfxRequest request) {
62+
System.out.println(this.getClass().getName());
63+
System.out.println(request.toString());
64+
return true;
65+
}
66+
}
3867
}
68+
69+
70+

0 commit comments

Comments
 (0)