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+
package io.kimmking.rpcfx.api;
2+
3+
public class RpcfxRequest {
4+
5+
private String serviceClass;
6+
7+
private String method;
8+
9+
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+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
public class RpcfxResponse {
4+
5+
private Object result;
6+
private boolean status;
7+
private Exception exception;
8+
9+
public Object getResult() {
10+
return result;
11+
}
12+
13+
public void setResult(Object result) {
14+
this.result = result;
15+
}
16+
17+
public boolean isStatus() {
18+
return status;
19+
}
20+
21+
public void setStatus(boolean status) {
22+
this.status = status;
23+
}
24+
25+
public Exception getException() {
26+
return exception;
27+
}
28+
29+
public void setException(Exception exception) {
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
public class User {
44

5+
public User(){}
6+
7+
public User(int id, String name) {
8+
this.id = id;
9+
this.name = name;
10+
}
11+
512
private int id;
613
private String name;
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+
package io.kimmking.rpcfx.client;
2+
3+
4+
import com.alibaba.fastjson.JSON;
5+
import io.kimmking.rpcfx.api.RpcfxRequest;
6+
import io.kimmking.rpcfx.api.RpcfxResponse;
7+
import okhttp3.MediaType;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.RequestBody;
11+
12+
import java.io.IOException;
13+
import java.lang.reflect.InvocationHandler;
14+
import java.lang.reflect.Method;
15+
import java.lang.reflect.Proxy;
16+
17+
public class Rpcfx {
18+
public static <T> T create(final Class<T> serviceClass, final String url) {
19+
20+
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
21+
22+
}
23+
24+
public static class RpcfxInvocationHandler implements InvocationHandler {
25+
26+
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
27+
28+
29+
private final Class<?> serviceClass;
30+
private final String url;
31+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
32+
this.serviceClass = serviceClass;
33+
this.url = url;
34+
}
35+
36+
@Override
37+
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
38+
RpcfxRequest request = new RpcfxRequest();
39+
request.setServiceClass(this.serviceClass.getName());
40+
request.setMethod(method.getName());
41+
request.setParams(params);
42+
43+
RpcfxResponse response = post(request, url);
44+
return JSON.parse(response.getResult().toString());
45+
}
46+
47+
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
48+
String reqJson = JSON.toJSONString(req);
49+
OkHttpClient client = new OkHttpClient();
50+
final Request request = new Request.Builder()
51+
.url(url)
52+
.post(RequestBody.create(JSONTYPE, reqJson))
53+
.build();
54+
String respJson = client.newCall(request).execute().body().string();
55+
return JSON.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+
package io.kimmking.rpcfx.client;
2+
3+
import com.alibaba.fastjson.parser.ParserConfig;
4+
import io.kimmking.rpcfx.api.User;
5+
import io.kimmking.rpcfx.api.UserService;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@SpringBootApplication
9+
public class RpcfxClientApplication {
10+
11+
static {
12+
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
13+
}
14+
15+
public static void main(String[] args) {
16+
17+
UserService service = Rpcfx.create(UserService.class, "http://localhost:8080/");
18+
User user = 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)