|
| 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 | +} |
0 commit comments