Skip to content

Commit 02c958a

Browse files
committed
update
1 parent 7fc6668 commit 02c958a

File tree

11 files changed

+143
-10
lines changed

11 files changed

+143
-10
lines changed
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+
}

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
import com.alibaba.fastjson.JSON;
55
import com.alibaba.fastjson.parser.ParserConfig;
6-
import io.kimmking.rpcfx.api.RpcfxRequest;
7-
import io.kimmking.rpcfx.api.RpcfxResponse;
6+
import io.kimmking.rpcfx.api.*;
87
import okhttp3.MediaType;
98
import okhttp3.OkHttpClient;
109
import okhttp3.Request;
@@ -14,17 +13,34 @@
1413
import java.lang.reflect.InvocationHandler;
1514
import java.lang.reflect.Method;
1615
import java.lang.reflect.Proxy;
16+
import java.util.ArrayList;
17+
import java.util.List;
1718

1819
public final class Rpcfx {
1920

2021
static {
2122
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
2223
}
2324

24-
public static <T> T create(final Class<T> serviceClass, final String url) {
25+
public static <T, filters> T createFromRegistry(final Class<T> serviceClass, final String zkUrl, Router router, LoadBalancer loadBalance, Filter filter) {
26+
27+
// 加filte之一
28+
29+
// curator Provider list from zk
30+
List<String> invokers = new ArrayList<>(); // 监听zk的临时节点,根据事件更新这个list
31+
32+
List<String> urls = router.route(invokers);
33+
34+
String url = loadBalance.select(urls); // router, loadbalance
35+
36+
return (T) create(serviceClass, url, filter);
37+
38+
}
39+
40+
public static <T> T create(final Class<T> serviceClass, final String url, Filter filter) {
2541

2642
// 0. 替换动态代理 -> AOP
27-
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
43+
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url, filter));
2844

2945
}
3046

@@ -34,9 +50,12 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
3450

3551
private final Class<?> serviceClass;
3652
private final String url;
37-
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
53+
private final Filter[] filters;
54+
55+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url, Filter... filters) {
3856
this.serviceClass = serviceClass;
3957
this.url = url;
58+
this.filters = filters;
4059
}
4160

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

4665
@Override
4766
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
67+
68+
// 加filter地方之二
69+
// mock == true, new Student("hubao");
70+
4871
RpcfxRequest request = new RpcfxRequest();
4972
request.setServiceClass(this.serviceClass.getName());
5073
request.setMethod(method.getName());
5174
request.setParams(params);
5275

76+
for (Filter filter : filters) {
77+
if(!filter.filter(request)) {
78+
return null;
79+
}
80+
}
81+
5382
RpcfxResponse response = post(request, url);
5483

84+
// 加filter地方之三
85+
// Student.setTeacher("cuijing");
86+
5587
// 这里判断response.status,处理异常
5688
// 考虑封装一个全局的RpcfxException
5789

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import io.kimmking.rpcfx.demo.api.UserService;
88
import org.springframework.boot.autoconfigure.SpringBootApplication;
99

10+
import java.util.Random;
11+
1012
@SpringBootApplication
1113
public class RpcfxClientApplication {
1214

@@ -28,9 +30,13 @@ public static void main(String[] args) {
2830
Order order = orderService.findOrderById(1992129);
2931
System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount()));
3032

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

3336
// SpringApplication.run(RpcfxClientApplication.class, args);
3437
}
3538

3639
}
40+
41+
42+

07rpc/rpc01/rpcfx-demo-provider/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
<artifactId>spring-boot-starter-web</artifactId>
3939
</dependency>
4040

41+
<dependency>
42+
<groupId>org.apache.curator</groupId>
43+
<artifactId>curator-client</artifactId>
44+
<version>5.1.0</version>
45+
</dependency>
46+
4147
<dependency>
4248
<groupId>org.springframework.boot</groupId>
4349
<artifactId>spring-boot-starter-test</artifactId>

07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@
1414
import org.springframework.web.bind.annotation.RequestBody;
1515
import org.springframework.web.bind.annotation.RestController;
1616

17+
import java.net.InetAddress;
18+
import java.net.InterfaceAddress;
19+
import java.net.UnknownHostException;
20+
1721
@SpringBootApplication
1822
@RestController
1923
public class RpcfxServerApplication {
2024

21-
public static void main(String[] args) {
25+
public static void main(String[] args) throws Exception {
26+
27+
// xxx "io.kimmking.rpcfx.demo.api.UserService"
28+
29+
ServiceProviderDesc desc = new ServiceProviderDesc();
30+
desc.setHost(InetAddress.getLocalHost().getHostAddress());
31+
desc.setPort(8080);
32+
desc.setServiceClass("io.kimmking.rpcfx.demo.api.UserService");
33+
34+
// Curator.
35+
2236
SpringApplication.run(RpcfxServerApplication.class, args);
2337
}
2438

@@ -42,6 +56,10 @@ public RpcfxResolver createResolver(){
4256

4357
// 能否去掉name
4458
//
59+
60+
// annotation
61+
62+
4563
@Bean(name = "io.kimmking.rpcfx.demo.api.UserService")
4664
public UserService createUserService(){
4765
return new UserServiceImpl();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.kimmking.rpcfx.demo.provider;
2+
3+
public class ServiceProviderDesc {
4+
5+
private String host;
6+
private Integer port;
7+
private String serviceClass;
8+
9+
// group
10+
// version
11+
12+
public String getHost() {
13+
return host;
14+
}
15+
16+
public void setHost(String host) {
17+
this.host = host;
18+
}
19+
20+
public Integer getPort() {
21+
return port;
22+
}
23+
24+
public void setPort(Integer port) {
25+
this.port = port;
26+
}
27+
28+
public String getServiceClass() {
29+
return serviceClass;
30+
}
31+
32+
public void setServiceClass(String serviceClass) {
33+
this.serviceClass = serviceClass;
34+
}
35+
}

07rpc/rpc02/dubbo-demo-consumer/src/main/java/io/kimmking/dubbo/demo/consumer/DubboClientApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
@SpringBootApplication
1414
public class DubboClientApplication {
1515

16-
@DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")
16+
@DubboReference(version = "1.0.0") //, url = "dubbo://127.0.0.1:12345")
1717
private UserService userService;
1818

19-
@DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")
19+
@DubboReference(version = "1.0.0") //, url = "dubbo://127.0.0.1:12345")
2020
private OrderService orderService;
2121

2222
public static void main(String[] args) {

07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/OrderServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.apache.dubbo.config.annotation.DubboService;
66

77

8-
@DubboService(version = "1.0.0")
8+
@DubboService(version = "1.0.0", tag = "red", weight = 100)
99
public class OrderServiceImpl implements OrderService {
1010

1111
@Override

0 commit comments

Comments
 (0)