Skip to content

Commit 714088e

Browse files
committed
update homeworks
1 parent d360a51 commit 714088e

File tree

17 files changed

+175
-24
lines changed

17 files changed

+175
-24
lines changed

07rpc/rpc01/.DS_Store

2 KB
Binary file not shown.

07rpc/rpc01/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
<description>RPC demo project for Spring Boot</description>
1717

1818
<modules>
19-
<module>rpcfx-api</module>
19+
<module>rpcfx-core</module>
2020
<module>rpcfx-server</module>
2121
<module>rpcfx-client</module>
22+
<module>rpcfx-demo-api</module>
2223
</modules>
2324

2425
<properties>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
<dependencies>
2121
<dependency>
2222
<groupId>io.kimmking</groupId>
23-
<artifactId>rpcfx-api</artifactId>
23+
<artifactId>rpcfx-core</artifactId>
24+
<version>${project.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.kimmking</groupId>
28+
<artifactId>rpcfx-demo-api</artifactId>
2429
<version>${project.version}</version>
2530
</dependency>
2631

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public class Rpcfx {
1818
public static <T> T create(final Class<T> serviceClass, final String url) {
1919

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

2223
}
@@ -25,14 +26,17 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
2526

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

28-
2929
private final Class<?> serviceClass;
3030
private final String url;
3131
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
3232
this.serviceClass = serviceClass;
3333
this.url = url;
3434
}
3535

36+
// 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
37+
// int byte char float double long bool
38+
// [], data class
39+
3640
@Override
3741
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
3842
RpcfxRequest request = new RpcfxRequest();
@@ -41,17 +45,26 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
4145
request.setParams(params);
4246

4347
RpcfxResponse response = post(request, url);
48+
49+
// 这里判断response.status,处理异常
50+
// 考虑封装一个全局的RpcfxException
51+
4452
return JSON.parse(response.getResult().toString());
4553
}
4654

4755
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
4856
String reqJson = JSON.toJSONString(req);
57+
System.out.println("req json: "+reqJson);
58+
59+
// 1.可以复用client
60+
// 2.尝试使用httpclient或者netty client
4961
OkHttpClient client = new OkHttpClient();
5062
final Request request = new Request.Builder()
5163
.url(url)
5264
.post(RequestBody.create(JSONTYPE, reqJson))
5365
.build();
5466
String respJson = client.newCall(request).execute().body().string();
67+
System.out.println("resp json: "+respJson);
5568
return JSON.parseObject(respJson, RpcfxResponse.class);
5669
}
5770
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.kimmking.rpcfx.client;
22

33
import com.alibaba.fastjson.parser.ParserConfig;
4+
import io.kimmking.rpcfx.api.Order;
5+
import io.kimmking.rpcfx.api.OrderService;
46
import io.kimmking.rpcfx.api.User;
57
import io.kimmking.rpcfx.api.UserService;
68
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -12,12 +14,26 @@ public class RpcfxClientApplication {
1214
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
1315
}
1416

17+
// 二方库
18+
// 三方库 lib
19+
// nexus, userserivce -> userdao -> user
20+
//
21+
1522
public static void main(String[] args) {
1623

17-
UserService service = Rpcfx.create(UserService.class, "http://localhost:8080/");
18-
User user = service.findById(1);
24+
// UserService service = new xxx();
25+
// service.findById
26+
27+
UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/");
28+
User user = userService.findById(1);
1929
System.out.println("find user id=1 from server: " + user.getName());
2030

31+
OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/");
32+
Order order = orderService.findOrderById(1992129);
33+
System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount()));
34+
35+
// 新加一个OrderService
36+
2137
// SpringApplication.run(RpcfxClientApplication.class, args);
2238
}
2339

07rpc/rpc01/rpcfx-api/pom.xml renamed to 07rpc/rpc01/rpcfx-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<!-- <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
1010
</parent>
1111
<groupId>io.kimmking</groupId>
12-
<artifactId>rpcfx-api</artifactId>
12+
<artifactId>rpcfx-core</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
14-
<name>rpcfx-api</name>
14+
<name>rpcfx-core</name>
1515

1616
<properties>
1717
<java.version>1.8</java.version>
File renamed without changes.

07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java renamed to 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
public class RpcfxResponse {
44

55
private Object result;
6+
67
private boolean status;
8+
79
private Exception exception;
810

911
public Object getResult() {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>io.kimmking</groupId>
7+
<artifactId>rpcfx</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<!-- <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
10+
</parent>
11+
<groupId>io.kimmking</groupId>
12+
<artifactId>rpcfx-demo-api</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>rpcfx-demo-api</name>
15+
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.kimmking.rpcfx.demo.api;
2+
3+
public class Order {
4+
5+
private int id;
6+
7+
private String name;
8+
9+
private float amount;
10+
11+
public Order(int id, String name, float amount) {
12+
this.id = id;
13+
this.name = name;
14+
this.amount = amount;
15+
}
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public float getAmount() {
34+
return amount;
35+
}
36+
37+
public void setAmount(float amount) {
38+
this.amount = amount;
39+
}
40+
}

0 commit comments

Comments
 (0)