forked from yida-lxw/Doraemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fa7e32
commit e4ea9ed
Showing
6 changed files
with
240 additions
and
71 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
rpc-client/src/main/java/com/example/rpcclient/client/MessageFuture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.rpcclient.client; | ||
|
||
import Domain.Response; | ||
|
||
/** | ||
* @program: springBootPractice | ||
* @description: | ||
* @author: hu_pf | ||
* @create: 2019-06-18 18:39 | ||
**/ | ||
public class MessageFuture { | ||
|
||
private volatile boolean success = false; | ||
private Response response; | ||
private final Object object = new Object(); | ||
|
||
public Response getMessage(){ | ||
synchronized (object){ | ||
while (!success){ | ||
try { | ||
object.wait(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return response; | ||
} | ||
} | ||
|
||
public void setMessage(Response response){ | ||
synchronized (object){ | ||
this.response = response; | ||
this.success = true; | ||
object.notify(); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
rpc-client/src/main/java/com/example/rpcclient/client/NettyClientConnect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.example.rpcclient.client; | ||
|
||
import Domain.Response; | ||
import com.google.gson.Gson; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import io.netty.handler.codec.string.StringDecoder; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
/** | ||
* @program: springBootPractice | ||
* @description:客户端连接 | ||
* @author: hu_pf | ||
* @create: 2019-06-18 18:31 | ||
**/ | ||
public class NettyClientConnect { | ||
|
||
private final Map<Long,MessageFuture> futureMap = new ConcurrentHashMap<>(); | ||
private CountDownLatch countDownLatch = new CountDownLatch(1); | ||
|
||
public void connect(String requestJson,Long threadId){ | ||
Bootstrap bootstrap = new Bootstrap(); | ||
NioEventLoopGroup group = new NioEventLoopGroup(); | ||
bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() { | ||
@Override | ||
protected void initChannel(Channel ch) { | ||
ch.pipeline(). | ||
addLast(new StringDecoder()). | ||
addLast(new SimpleChannelInboundHandler<String>() { | ||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { | ||
Gson gson = new Gson(); | ||
Response response = gson.fromJson(msg, Response.class); | ||
MessageFuture messageFuture = futureMap.get(threadId); | ||
messageFuture.setMessage(response); | ||
} | ||
|
||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
futureMap.put(threadId,new MessageFuture()); | ||
countDownLatch.countDown(); | ||
ByteBuf encoded = ctx.alloc().buffer(4 * requestJson.length()); | ||
encoded.writeBytes(requestJson.getBytes()); | ||
ctx.writeAndFlush(encoded); | ||
} | ||
}); | ||
} | ||
}).connect("127.0.0.1", 20006); | ||
} | ||
|
||
public Response getResponse(Long threadId){ | ||
MessageFuture messageFuture = null; | ||
try { | ||
countDownLatch.await(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
messageFuture = futureMap.get(threadId); | ||
return messageFuture.getMessage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
rpc-server/src/main/java/com/example/rpcserver/rpcHandle/CommonDeal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.example.rpcserver.rpcHandle; | ||
|
||
import Domain.ClassTypeAdapterFactory; | ||
import Domain.Request; | ||
import Domain.Response; | ||
import com.example.rpcserver.configuration.InitRpcConfig; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import lombok.extern.log4j.Log4j; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @program: springBootPractice | ||
* @description: | ||
* @author: hu_pf | ||
* @create: 2019-06-17 17:40 | ||
**/ | ||
@Log4j | ||
public class CommonDeal { | ||
|
||
public static String getInvokeMethodMes(String str){ | ||
GsonBuilder gsonBuilder = new GsonBuilder(); | ||
gsonBuilder.registerTypeAdapterFactory(new ClassTypeAdapterFactory()); | ||
Gson gson = gsonBuilder.create(); | ||
Request request = gson.fromJson(str, Request.class); | ||
return gson.toJson(invokeMethod(request)); | ||
} | ||
|
||
private static Response invokeMethod(Request request) { | ||
String className = request.getClassName(); | ||
String methodName = request.getMethodName(); | ||
Object[] parameters = request.getParameters(); | ||
Class<?>[] parameTypes = request.getParameTypes(); | ||
Object o = InitRpcConfig.rpcServiceMap.get(className); | ||
Response response = new Response(); | ||
try { | ||
Method method = o.getClass().getDeclaredMethod(methodName, parameTypes); | ||
Object invokeMethod = method.invoke(o, parameters); | ||
response.setResult(invokeMethod); | ||
} catch (NoSuchMethodException e) { | ||
log.info("没有找到" + methodName); | ||
} catch (IllegalAccessException e) { | ||
log.info("执行错误" + parameters); | ||
} catch (InvocationTargetException e) { | ||
log.info("执行错误" + parameters); | ||
} | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters