Skip to content

Commit ef939af

Browse files
committed
refactoring for fx and biz
1 parent 714088e commit ef939af

File tree

19 files changed

+313
-257
lines changed

19 files changed

+313
-257
lines changed

07rpc/rpc01/.DS_Store

0 Bytes
Binary file not shown.

07rpc/rpc01/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
<modules>
1919
<module>rpcfx-core</module>
20-
<module>rpcfx-server</module>
21-
<module>rpcfx-client</module>
2220
<module>rpcfx-demo-api</module>
21+
<module>rpcfx-demo-consumer</module>
22+
<module>rpcfx-demo-provider</module>
2323
</modules>
2424

2525
<properties>

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

Lines changed: 0 additions & 76 deletions
This file was deleted.

07rpc/rpc01/rpcfx-client/src/main/resources/application.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

07rpc/rpc01/rpcfx-client/src/test/java/io/kimmking/rpcfx/client/Springboot01ApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

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

20+
<dependencies>
21+
<dependency>
22+
<groupId>com.alibaba</groupId>
23+
<artifactId>fastjson</artifactId>
24+
<version>1.2.70</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.squareup.okhttp3</groupId>
29+
<artifactId>okhttp</artifactId>
30+
<version>3.12.2</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
<exclusions>
48+
<exclusion>
49+
<groupId>org.junit.vintage</groupId>
50+
<artifactId>junit-vintage-engine</artifactId>
51+
</exclusion>
52+
</exclusions>
53+
</dependency>
54+
</dependencies>
55+
2056
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
public interface RpcfxResolver {
4+
5+
Object resolve(String serviceClass);
6+
7+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.alibaba.fastjson.JSON;
5+
import com.alibaba.fastjson.parser.ParserConfig;
56
import io.kimmking.rpcfx.api.RpcfxRequest;
67
import io.kimmking.rpcfx.api.RpcfxResponse;
78
import okhttp3.MediaType;
@@ -14,7 +15,12 @@
1415
import java.lang.reflect.Method;
1516
import java.lang.reflect.Proxy;
1617

17-
public class Rpcfx {
18+
public final class Rpcfx {
19+
20+
static {
21+
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
22+
}
23+
1824
public static <T> T create(final Class<T> serviceClass, final String url) {
1925

2026
// 0. 替换动态代理 -> AOP
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.kimmking.rpcfx.server;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.serializer.SerializerFeature;
5+
import io.kimmking.rpcfx.api.RpcfxRequest;
6+
import io.kimmking.rpcfx.api.RpcfxResolver;
7+
import io.kimmking.rpcfx.api.RpcfxResponse;
8+
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.lang.reflect.Method;
11+
import java.util.Arrays;
12+
13+
public class RpcfxInvoker {
14+
15+
private RpcfxResolver resolver;
16+
17+
public RpcfxInvoker(RpcfxResolver resolver){
18+
this.resolver = resolver;
19+
}
20+
21+
public RpcfxResponse invoke(RpcfxRequest request) {
22+
RpcfxResponse response = new RpcfxResponse();
23+
String serviceClass = request.getServiceClass();
24+
25+
// 作业1:改成泛型和反射
26+
Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass);
27+
28+
try {
29+
Method method = resolveMethodFromClass(service.getClass(), request.getMethod());
30+
Object result = method.invoke(service, request.getParams()); // dubbo, fastjson,
31+
// 两次json序列化能否合并成一个
32+
response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName));
33+
response.setStatus(true);
34+
return response;
35+
} catch ( IllegalAccessException | InvocationTargetException e) {
36+
37+
// 3.Xstream
38+
39+
// 2.封装一个统一的RpcfxException
40+
// 客户端也需要判断异常
41+
e.printStackTrace();
42+
response.setException(e);
43+
response.setStatus(false);
44+
return response;
45+
}
46+
}
47+
48+
private Method resolveMethodFromClass(Class<?> klass, String methodName) {
49+
return Arrays.stream(klass.getMethods()).filter(m -> methodName.equals(m.getName())).findFirst().get();
50+
}
51+
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>rpcfx</artifactId>
7+
<groupId>io.kimmking</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>rpcfx-demo-consumer</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.kimmking</groupId>
22+
<artifactId>rpcfx-core</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>io.kimmking</groupId>
27+
<artifactId>rpcfx-demo-api</artifactId>
28+
<version>${project.version}</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
<exclusions>
46+
<exclusion>
47+
<groupId>org.junit.vintage</groupId>
48+
<artifactId>junit-vintage-engine</artifactId>
49+
</exclusion>
50+
</exclusions>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
</project>

0 commit comments

Comments
 (0)