Skip to content

Commit 2bb1888

Browse files
authored
Merge pull request #4 from JavaCourse00/main
2 parents f3c41cf + 7fc6668 commit 2bb1888

36 files changed

Lines changed: 1177 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ hs_err_pid*
2727
classes/
2828
target/
2929
build/
30+
31+
.DS_Store
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package java0.conc0302.threadpool;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Future;
6+
7+
public class ExceptionDemo {
8+
9+
public static void main(String[] args) {
10+
ExecutorService executorService = Executors.newFixedThreadPool(1);
11+
12+
try {
13+
Future<Double> future = executorService.submit(() -> {
14+
int a = 1;
15+
return 10.0/(a-1);
16+
});
17+
18+
double b = future.get();
19+
System.out.println(b);
20+
21+
} catch (Exception ex) {
22+
System.out.println("catch execute");
23+
ex.printStackTrace();
24+
}
25+
26+
try {
27+
executorService.execute(() -> {
28+
int a = 1;
29+
float b = 10/(a-1);
30+
});
31+
} catch (Exception ex) {
32+
System.out.println("catch execute");
33+
ex.printStackTrace();
34+
}
35+
36+
executorService.shutdown();
37+
System.out.println("Main Thread End!");
38+
}
39+
40+
}

07rpc/rpc01/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.0.9.RELEASE</version>
9+
<!-- <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
10+
</parent>
11+
<groupId>io.kimmking</groupId>
12+
<artifactId>rpcfx</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>rpcfx</name>
15+
<packaging>pom</packaging>
16+
<description>RPC demo project for Spring Boot</description>
17+
18+
<modules>
19+
<module>rpcfx-core</module>
20+
<module>rpcfx-demo-api</module>
21+
<module>rpcfx-demo-consumer</module>
22+
<module>rpcfx-demo-provider</module>
23+
</modules>
24+
25+
<properties>
26+
<java.version>1.8</java.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-web</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
<exclusions>
45+
<exclusion>
46+
<groupId>org.junit.vintage</groupId>
47+
<artifactId>junit-vintage-engine</artifactId>
48+
</exclusion>
49+
</exclusions>
50+
</dependency>
51+
</dependencies>
52+
53+
</project>

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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-core</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>rpcfx-core</name>
15+
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
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+
56+
</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: 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+
}
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 RpcfxResponse {
4+
5+
private Object result;
6+
7+
private boolean status;
8+
9+
private Exception exception;
10+
11+
public Object getResult() {
12+
return result;
13+
}
14+
15+
public void setResult(Object result) {
16+
this.result = result;
17+
}
18+
19+
public boolean isStatus() {
20+
return status;
21+
}
22+
23+
public void setStatus(boolean status) {
24+
this.status = status;
25+
}
26+
27+
public Exception getException() {
28+
return exception;
29+
}
30+
31+
public void setException(Exception exception) {
32+
this.exception = exception;
33+
}
34+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.kimmking.rpcfx.client;
2+
3+
4+
import com.alibaba.fastjson.JSON;
5+
import com.alibaba.fastjson.parser.ParserConfig;
6+
import io.kimmking.rpcfx.api.RpcfxRequest;
7+
import io.kimmking.rpcfx.api.RpcfxResponse;
8+
import okhttp3.MediaType;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.RequestBody;
12+
13+
import java.io.IOException;
14+
import java.lang.reflect.InvocationHandler;
15+
import java.lang.reflect.Method;
16+
import java.lang.reflect.Proxy;
17+
18+
public final class Rpcfx {
19+
20+
static {
21+
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
22+
}
23+
24+
public static <T> T create(final Class<T> serviceClass, final String url) {
25+
26+
// 0. 替换动态代理 -> AOP
27+
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
28+
29+
}
30+
31+
public static class RpcfxInvocationHandler implements InvocationHandler {
32+
33+
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
34+
35+
private final Class<?> serviceClass;
36+
private final String url;
37+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
38+
this.serviceClass = serviceClass;
39+
this.url = url;
40+
}
41+
42+
// 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
43+
// int byte char float double long bool
44+
// [], data class
45+
46+
@Override
47+
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
48+
RpcfxRequest request = new RpcfxRequest();
49+
request.setServiceClass(this.serviceClass.getName());
50+
request.setMethod(method.getName());
51+
request.setParams(params);
52+
53+
RpcfxResponse response = post(request, url);
54+
55+
// 这里判断response.status,处理异常
56+
// 考虑封装一个全局的RpcfxException
57+
58+
return JSON.parse(response.getResult().toString());
59+
}
60+
61+
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
62+
String reqJson = JSON.toJSONString(req);
63+
System.out.println("req json: "+reqJson);
64+
65+
// 1.可以复用client
66+
// 2.尝试使用httpclient或者netty client
67+
OkHttpClient client = new OkHttpClient();
68+
final Request request = new Request.Builder()
69+
.url(url)
70+
.post(RequestBody.create(JSONTYPE, reqJson))
71+
.build();
72+
String respJson = client.newCall(request).execute().body().string();
73+
System.out.println("resp json: "+respJson);
74+
return JSON.parseObject(respJson, RpcfxResponse.class);
75+
}
76+
}
77+
}
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+
}

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>

0 commit comments

Comments
 (0)