Skip to content

Commit d360a51

Browse files
committed
complete
1 parent 3a852c8 commit d360a51

File tree

15 files changed

+251
-58
lines changed

15 files changed

+251
-58
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,4 @@
1717
<java.version>1.8</java.version>
1818
</properties>
1919

20-
<build>
21-
<plugins>
22-
<plugin>
23-
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-maven-plugin</artifactId>
25-
</plugin>
26-
</plugins>
27-
</build>
28-
2920
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packageio.kimmking.rpcfx.api;
2+
3+
publicclassRpcfxRequest{
4+
5+
privateStringserviceClass;
6+
7+
privateStringmethod;
8+
9+
privateObject[] params;
10+
11+
publicStringgetServiceClass(){
12+
returnserviceClass;
13+
}
14+
15+
publicvoidsetServiceClass(StringserviceClass){
16+
this.serviceClass = serviceClass;
17+
}
18+
19+
publicStringgetMethod(){
20+
returnmethod;
21+
}
22+
23+
publicvoidsetMethod(Stringmethod){
24+
this.method = method;
25+
}
26+
27+
publicObject[] getParams(){
28+
returnparams;
29+
}
30+
31+
publicvoidsetParams(Object[] params){
32+
this.params = params;
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packageio.kimmking.rpcfx.api;
2+
3+
publicclassRpcfxResponse{
4+
5+
privateObjectresult;
6+
privatebooleanstatus;
7+
privateExceptionexception;
8+
9+
publicObjectgetResult(){
10+
returnresult;
11+
}
12+
13+
publicvoidsetResult(Objectresult){
14+
this.result = result;
15+
}
16+
17+
publicbooleanisStatus(){
18+
returnstatus;
19+
}
20+
21+
publicvoidsetStatus(booleanstatus){
22+
this.status = status;
23+
}
24+
25+
publicExceptiongetException(){
26+
returnexception;
27+
}
28+
29+
publicvoidsetException(Exceptionexception){
30+
this.exception = exception;
31+
}
32+
}

‎07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/User.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
publicclassUser{
44

5+
publicUser(){}
6+
7+
publicUser(intid, Stringname){
8+
this.id = id;
9+
this.name = name;
10+
}
11+
512
privateintid;
613
privateStringname;
714

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
<artifactId>spring-boot-starter-web</artifactId>
3535
</dependency>
3636

37+
<dependency>
38+
<groupId>com.alibaba</groupId>
39+
<artifactId>fastjson</artifactId>
40+
<version>1.2.70</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.squareup.okhttp3</groupId>
45+
<artifactId>okhttp</artifactId>
46+
<version>3.12.2</version>
47+
</dependency>
48+
3749
<dependency>
3850
<groupId>org.springframework.boot</groupId>
3951
<artifactId>spring-boot-starter-test</artifactId>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
packageio.kimmking.rpcfx.client;
2+
3+
4+
importcom.alibaba.fastjson.JSON;
5+
importio.kimmking.rpcfx.api.RpcfxRequest;
6+
importio.kimmking.rpcfx.api.RpcfxResponse;
7+
importokhttp3.MediaType;
8+
importokhttp3.OkHttpClient;
9+
importokhttp3.Request;
10+
importokhttp3.RequestBody;
11+
12+
importjava.io.IOException;
13+
importjava.lang.reflect.InvocationHandler;
14+
importjava.lang.reflect.Method;
15+
importjava.lang.reflect.Proxy;
16+
17+
publicclassRpcfx{
18+
publicstatic <T> Tcreate(finalClass<T> serviceClass, finalStringurl){
19+
20+
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), newClass[]{serviceClass}, newRpcfxInvocationHandler(serviceClass, url));
21+
22+
}
23+
24+
publicstaticclassRpcfxInvocationHandlerimplementsInvocationHandler{
25+
26+
publicstaticfinalMediaTypeJSONTYPE = MediaType.get("application/json; charset=utf-8");
27+
28+
29+
privatefinalClass<?> serviceClass;
30+
privatefinalStringurl;
31+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, Stringurl){
32+
this.serviceClass = serviceClass;
33+
this.url = url;
34+
}
35+
36+
@Override
37+
publicObjectinvoke(Objectproxy, Methodmethod, Object[] params) throwsThrowable{
38+
RpcfxRequestrequest = newRpcfxRequest();
39+
request.setServiceClass(this.serviceClass.getName());
40+
request.setMethod(method.getName());
41+
request.setParams(params);
42+
43+
RpcfxResponseresponse = post(request, url);
44+
returnJSON.parse(response.getResult().toString());
45+
}
46+
47+
privateRpcfxResponsepost(RpcfxRequestreq, Stringurl) throwsIOException{
48+
StringreqJson = JSON.toJSONString(req);
49+
OkHttpClientclient = newOkHttpClient();
50+
finalRequestrequest = newRequest.Builder()
51+
.url(url)
52+
.post(RequestBody.create(JSONTYPE, reqJson))
53+
.build();
54+
StringrespJson = client.newCall(request).execute().body().string();
55+
returnJSON.parseObject(respJson, RpcfxResponse.class);
56+
}
57+
}
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packageio.kimmking.rpcfx.client;
2+
3+
importcom.alibaba.fastjson.parser.ParserConfig;
4+
importio.kimmking.rpcfx.api.User;
5+
importio.kimmking.rpcfx.api.UserService;
6+
importorg.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@SpringBootApplication
9+
publicclassRpcfxClientApplication{
10+
11+
static{
12+
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
13+
}
14+
15+
publicstaticvoidmain(String[] args){
16+
17+
UserServiceservice = Rpcfx.create(UserService.class, "http://localhost:8080/");
18+
Useruser = service.findById(1);
19+
System.out.println("find user id=1 from server: " + user.getName());
20+
21+
// SpringApplication.run(RpcfxClientApplication.class, args);
22+
}
23+
24+
}

‎07rpc/rpc01/rpcfx-client/src/main/java/io/kimmking/springboot01/Springboot01Application.java‎

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
server:
2-
port: 8080
1+
#server:
2+
# port: 8080
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//package io.kimmking.springboot01;
2+
//
3+
//import org.junit.Test;
4+
//import org.springframework.boot.test.context.SpringBootTest;
5+
//
6+
//@SpringBootTest
7+
//class Springboot01ApplicationTests{
8+
//
9+
// @Test
10+
// void contextLoads(){
11+
// }
12+
//
13+
//}

0 commit comments

Comments
(0)