Skip to content

Commit 5b21e20

Browse files
committed
week09 作业
1 parent 331483d commit 5b21e20

7 files changed

Lines changed: 60 additions & 38 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@
5656
<artifactId>byte-buddy</artifactId>
5757
<version>1.7.11</version>
5858
</dependency>
59+
<dependency>
60+
<groupId>commons-logging</groupId>
61+
<artifactId>commons-logging</artifactId>
62+
<version>1.2</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.projectlombok</groupId>
66+
<artifactId>lombok</artifactId>
67+
</dependency>
68+
5969
</dependencies>
6070

6171
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.kimmking.rpcfx.client;
2+
3+
public class HttpClient {
4+
5+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.kimmking.rpcfx.client;
22

3-
import com.alibaba.fastjson.parser.ParserConfig;
43
import io.kimmking.rpcfx.proxy.RpcfxInvocationHandler;
54
import net.bytebuddy.ByteBuddy;
65
import net.bytebuddy.implementation.InvocationHandlerAdapter;

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/proxy/RpcfxInvocationHandler.java

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,37 @@
1414
import java.lang.reflect.InvocationHandler;
1515
import java.lang.reflect.Method;
1616

17-
public class RpcfxInvocationHandler implements InvocationHandler {
17+
public class RpcfxInvocationHandler implements InvocationHandler {
1818

19-
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
19+
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
2020

21-
private final Class<?> serviceClass;
22-
private final String url;
23-
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
24-
this.serviceClass = serviceClass;
25-
this.url = url;
26-
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
27-
}
21+
private final Class<?> serviceClass;
22+
private final String url;
2823

29-
// 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
30-
// int byte char float double long bool
31-
// [], data class
24+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
25+
this.serviceClass = serviceClass;
26+
this.url = url;
27+
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
28+
}
29+
30+
// 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
31+
// int byte char float double long bool
32+
// [], data class
3233

33-
@Override
34-
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
35-
return invokePost(method, params);
36-
}
34+
@Override
35+
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
36+
return invokePost(method, params);
37+
}
3738

39+
40+
/**
41+
* 执行请求
42+
*
43+
* @param method
44+
* @param params
45+
* @return
46+
* @throws IOException
47+
*/
3848
private Object invokePost(Method method, Object[] params) throws IOException {
3949
RpcfxRequest request = new RpcfxRequest();
4050
request.setServiceClass(this.serviceClass.getName());
@@ -51,18 +61,18 @@ private Object invokePost(Method method, Object[] params) throws IOException {
5161
}
5262

5363
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
54-
String reqJson = JSON.toJSONString(req);
55-
System.out.println("req json: "+reqJson);
64+
String reqJson = JSON.toJSONString(req);
65+
System.out.println("req json: " + reqJson);
5666

57-
// 1.可以复用client
58-
// 2.尝试使用httpclient或者netty client
59-
OkHttpClient client = new OkHttpClient();
60-
final Request request = new Request.Builder()
61-
.url(url)
62-
.post(RequestBody.create(JSONTYPE, reqJson))
63-
.build();
64-
String respJson = client.newCall(request).execute().body().string();
65-
System.out.println("resp json: "+respJson);
66-
return JSON.parseObject(respJson, RpcfxResponse.class);
67-
}
68-
}
67+
// 1.可以复用client
68+
// 2.尝试使用httpclient或者netty client
69+
OkHttpClient client = new OkHttpClient();
70+
final Request request = new Request.Builder()
71+
.url(url)
72+
.post(RequestBody.create(JSONTYPE, reqJson))
73+
.build();
74+
String respJson = client.newCall(request).execute().body().string();
75+
System.out.println("resp json: " + respJson);
76+
return JSON.parseObject(respJson, RpcfxResponse.class);
77+
}
78+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public RpcfxResponse invoke(RpcfxRequest request){
4242

4343
// 3.Xstream
4444

45-
// 2.封装一个统一的RpcfxException
45+
// 2.封装一个统一的 RpcfxException
4646
// 客户端也需要判断异常
4747
e.printStackTrace();
4848
response.setException(e);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ public class RpcfxClientApplication {
2020
//
2121
public static void main(String[] args) {
2222

23-
// UserService service = new xxx();
24-
// service.findById
2523
RpcfxProxy rpcfxProxy = new RpcfxByteBuddy();
26-
UserService userService = rpcfxProxy.create(UserService.class, "http://localhost:8080/");
24+
UserService userService = rpcfxProxy.create(UserService.class, "http://localhost:8082/");
2725
User user = userService.findById(1);
2826
System.out.println("find user id=1 from server: " + user.getName());
2927

30-
OrderService orderService = rpcfxProxy.create(OrderService.class, "http://localhost:8080/");
28+
OrderService orderService = rpcfxProxy.create(OrderService.class, "http://localhost:8082/");
3129
Order order = orderService.findOrderById(1992129);
3230
System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount()));
3331

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public RpcfxResolver createResolver(){
4242

4343
// 能否去掉name
4444
//
45-
@Bean()
45+
@Bean
4646
public UserService createUserService(){
4747
return new UserServiceImpl();
4848
}
4949

50-
@Bean()
50+
@Bean
5151
public OrderService createOrderService(){
5252
return new OrderServiceImpl();
5353
}

0 commit comments

Comments
 (0)