Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
嗝屁小孩纸
guide-rpc-framework
提交
e6746a46
G
guide-rpc-framework
项目概览
嗝屁小孩纸
/
guide-rpc-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
guide-rpc-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e6746a46
编写于
6月 03, 2020
作者:
S
shuang.kou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[v3.0]refractor optimize some code
上级
e2e26dbe
变更
11
显示空白变更内容
内联
并排
Showing
11 changed file
with
38 addition
and
43 deletion
+38
-43
rpc-framework-simple/src/main/java/github/javaguide/handler/RpcRequestHandler.java
...main/java/github/javaguide/handler/RpcRequestHandler.java
+3
-5
rpc-framework-simple/src/main/java/github/javaguide/provider/ServiceProviderImpl.java
...n/java/github/javaguide/provider/ServiceProviderImpl.java
+2
-0
rpc-framework-simple/src/main/java/github/javaguide/proxy/RpcClientProxy.java
.../src/main/java/github/javaguide/proxy/RpcClientProxy.java
+2
-1
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/client/ChannelProvider.java
...hub/javaguide/transport/netty/client/ChannelProvider.java
+3
-8
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/client/NettyClient.java
.../github/javaguide/transport/netty/client/NettyClient.java
+2
-2
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/client/NettyClientClientTransport.java
...de/transport/netty/client/NettyClientClientTransport.java
+19
-20
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/codec/kyro/NettyKryoDecoder.java
...avaguide/transport/netty/codec/kyro/NettyKryoDecoder.java
+1
-1
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/codec/kyro/NettyKryoEncoder.java
...avaguide/transport/netty/codec/kyro/NettyKryoEncoder.java
+1
-1
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/server/NettyServer.java
.../github/javaguide/transport/netty/server/NettyServer.java
+2
-2
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/server/NettyServerHandler.java
.../javaguide/transport/netty/server/NettyServerHandler.java
+3
-2
rpc-framework-simple/src/main/java/github/javaguide/transport/socket/SocketRpcClient.java
...va/github/javaguide/transport/socket/SocketRpcClient.java
+0
-1
未找到文件。
rpc-framework-simple/src/main/java/github/javaguide/handler/RpcRequestHandler.java
浏览文件 @
e6746a46
...
@@ -25,12 +25,9 @@ public class RpcRequestHandler {
...
@@ -25,12 +25,9 @@ public class RpcRequestHandler {
* 处理 rpcRequest :调用对应的方法,然后返回方法执行结果
* 处理 rpcRequest :调用对应的方法,然后返回方法执行结果
*/
*/
public
Object
handle
(
RpcRequest
rpcRequest
)
{
public
Object
handle
(
RpcRequest
rpcRequest
)
{
Object
result
;
//通过注册中心获取到目标类(客户端需要调用类)
//通过注册中心获取到目标类(客户端需要调用类)
Object
service
=
serviceProvider
.
getServiceProvider
(
rpcRequest
.
getInterfaceName
());
Object
service
=
serviceProvider
.
getServiceProvider
(
rpcRequest
.
getInterfaceName
());
result
=
invokeTargetMethod
(
rpcRequest
,
service
);
return
invokeTargetMethod
(
rpcRequest
,
service
);
log
.
info
(
"service:{} successful invoke method:{}"
,
rpcRequest
.
getInterfaceName
(),
rpcRequest
.
getMethodName
());
return
result
;
}
}
/**
/**
...
@@ -38,7 +35,7 @@ public class RpcRequestHandler {
...
@@ -38,7 +35,7 @@ public class RpcRequestHandler {
*
*
* @param rpcRequest 客户端请求
* @param rpcRequest 客户端请求
* @param service 提供服务的对象
* @param service 提供服务的对象
* @return
* @return
目标方法执行的结果
*/
*/
private
Object
invokeTargetMethod
(
RpcRequest
rpcRequest
,
Object
service
)
{
private
Object
invokeTargetMethod
(
RpcRequest
rpcRequest
,
Object
service
)
{
Object
result
;
Object
result
;
...
@@ -48,6 +45,7 @@ public class RpcRequestHandler {
...
@@ -48,6 +45,7 @@ public class RpcRequestHandler {
return
RpcResponse
.
fail
(
RpcResponseCode
.
NOT_FOUND_METHOD
);
return
RpcResponse
.
fail
(
RpcResponseCode
.
NOT_FOUND_METHOD
);
}
}
result
=
method
.
invoke
(
service
,
rpcRequest
.
getParameters
());
result
=
method
.
invoke
(
service
,
rpcRequest
.
getParameters
());
log
.
info
(
"service:[{}] successful invoke method:[{}]"
,
rpcRequest
.
getInterfaceName
(),
rpcRequest
.
getMethodName
());
}
catch
(
NoSuchMethodException
|
IllegalArgumentException
|
InvocationTargetException
|
IllegalAccessException
e
)
{
}
catch
(
NoSuchMethodException
|
IllegalArgumentException
|
InvocationTargetException
|
IllegalAccessException
e
)
{
throw
new
RpcException
(
e
.
getMessage
(),
e
);
throw
new
RpcException
(
e
.
getMessage
(),
e
);
}
}
...
...
rpc-framework-simple/src/main/java/github/javaguide/provider/ServiceProviderImpl.java
浏览文件 @
e6746a46
...
@@ -9,6 +9,8 @@ import java.util.Set;
...
@@ -9,6 +9,8 @@ import java.util.Set;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
/**
/**
* 实现了 ServiceProvider 接口,可以将其看做是一个保存和提供服务实例对象的示例
*
* @author shuang.kou
* @author shuang.kou
* @createTime 2020年05月13日 11:23:00
* @createTime 2020年05月13日 11:23:00
*/
*/
...
...
rpc-framework-simple/src/main/java/github/javaguide/proxy/RpcClientProxy.java
浏览文件 @
e6746a46
...
@@ -10,7 +10,8 @@ import java.lang.reflect.Proxy;
...
@@ -10,7 +10,8 @@ import java.lang.reflect.Proxy;
import
java.util.UUID
;
import
java.util.UUID
;
/**
/**
* 动态代理类。当动态代理对象调用一个方法的时候,实际调用的是下面的 invoke 方法
* 动态代理类。当动态代理对象调用一个方法的时候,实际调用的是下面的 invoke 方法。
* 正是因为动态代理才让客户端调用的远程方法像是调用本地方法一样(屏蔽了中间过程)
*
*
* @author shuang.kou
* @author shuang.kou
* @createTime 2020年05月10日 19:01:00
* @createTime 2020年05月10日 19:01:00
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/client/ChannelProvider.java
浏览文件 @
e6746a46
...
@@ -28,18 +28,13 @@ public class ChannelProvider {
...
@@ -28,18 +28,13 @@ public class ChannelProvider {
*/
*/
private
static
final
int
MAX_RETRY_COUNT
=
5
;
private
static
final
int
MAX_RETRY_COUNT
=
5
;
public
static
Channel
get
(
InetSocketAddress
inetSocketAddress
)
{
public
static
Channel
get
(
InetSocketAddress
inetSocketAddress
)
throws
InterruptedException
{
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
1
);
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
1
);
try
{
connect
(
bootstrap
,
inetSocketAddress
,
countDownLatch
);
connect
(
bootstrap
,
inetSocketAddress
,
countDownLatch
);
countDownLatch
.
await
();
countDownLatch
.
await
();
}
catch
(
InterruptedException
e
)
{
log
.
error
(
"occur exception when get channel:"
,
e
);
}
return
channel
;
return
channel
;
}
}
private
static
void
connect
(
Bootstrap
bootstrap
,
InetSocketAddress
inetSocketAddress
,
CountDownLatch
countDownLatch
)
{
private
static
void
connect
(
Bootstrap
bootstrap
,
InetSocketAddress
inetSocketAddress
,
CountDownLatch
countDownLatch
)
{
connect
(
bootstrap
,
inetSocketAddress
,
MAX_RETRY_COUNT
,
countDownLatch
);
connect
(
bootstrap
,
inetSocketAddress
,
MAX_RETRY_COUNT
,
countDownLatch
);
}
}
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/client/NettyClient.java
浏览文件 @
e6746a46
...
@@ -3,8 +3,8 @@ package github.javaguide.transport.netty.client;
...
@@ -3,8 +3,8 @@ package github.javaguide.transport.netty.client;
import
github.javaguide.dto.RpcRequest
;
import
github.javaguide.dto.RpcRequest
;
import
github.javaguide.dto.RpcResponse
;
import
github.javaguide.dto.RpcResponse
;
import
github.javaguide.serialize.kyro.KryoSerializer
;
import
github.javaguide.serialize.kyro.KryoSerializer
;
import
github.javaguide.transport.netty.codec.NettyKryoDecoder
;
import
github.javaguide.transport.netty.codec.
kyro.
NettyKryoDecoder
;
import
github.javaguide.transport.netty.codec.NettyKryoEncoder
;
import
github.javaguide.transport.netty.codec.
kyro.
NettyKryoEncoder
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.ChannelOption
;
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/client/NettyClientClientTransport.java
浏览文件 @
e6746a46
...
@@ -34,7 +34,10 @@ public class NettyClientClientTransport implements ClientTransport {
...
@@ -34,7 +34,10 @@ public class NettyClientClientTransport implements ClientTransport {
try
{
try
{
InetSocketAddress
inetSocketAddress
=
serviceDiscovery
.
lookupService
(
rpcRequest
.
getInterfaceName
());
InetSocketAddress
inetSocketAddress
=
serviceDiscovery
.
lookupService
(
rpcRequest
.
getInterfaceName
());
Channel
channel
=
ChannelProvider
.
get
(
inetSocketAddress
);
Channel
channel
=
ChannelProvider
.
get
(
inetSocketAddress
);
if
(
channel
.
isActive
())
{
if
(!
channel
.
isActive
())
{
NettyClient
.
close
();
return
null
;
}
channel
.
writeAndFlush
(
rpcRequest
).
addListener
((
ChannelFutureListener
)
future
->
{
channel
.
writeAndFlush
(
rpcRequest
).
addListener
((
ChannelFutureListener
)
future
->
{
if
(
future
.
isSuccess
())
{
if
(
future
.
isSuccess
())
{
log
.
info
(
"client send message: {}"
,
rpcRequest
);
log
.
info
(
"client send message: {}"
,
rpcRequest
);
...
@@ -50,13 +53,9 @@ public class NettyClientClientTransport implements ClientTransport {
...
@@ -50,13 +53,9 @@ public class NettyClientClientTransport implements ClientTransport {
//校验 RpcResponse 和 RpcRequest
//校验 RpcResponse 和 RpcRequest
RpcMessageChecker
.
check
(
rpcResponse
,
rpcRequest
);
RpcMessageChecker
.
check
(
rpcResponse
,
rpcRequest
);
result
.
set
(
rpcResponse
.
getData
());
result
.
set
(
rpcResponse
.
getData
());
}
else
{
NettyClient
.
close
();
System
.
exit
(
0
);
}
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
log
.
error
(
"occur exception when send rpc message from client:"
,
e
);
log
.
error
(
e
.
getMessage
(),
e
);
Thread
.
currentThread
().
interrupt
();
}
}
return
result
.
get
();
return
result
.
get
();
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/codec/NettyKryoDecoder.java
→
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/codec/
kyro/
NettyKryoDecoder.java
浏览文件 @
e6746a46
package
github.javaguide.transport.netty.codec
;
package
github.javaguide.transport.netty.codec
.kyro
;
import
github.javaguide.serialize.Serializer
;
import
github.javaguide.serialize.Serializer
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBuf
;
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/codec/NettyKryoEncoder.java
→
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/codec/
kyro/
NettyKryoEncoder.java
浏览文件 @
e6746a46
package
github.javaguide.transport.netty.codec
;
package
github.javaguide.transport.netty.codec
.kyro
;
import
github.javaguide.serialize.Serializer
;
import
github.javaguide.serialize.Serializer
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBuf
;
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/server/NettyServer.java
浏览文件 @
e6746a46
...
@@ -7,8 +7,8 @@ import github.javaguide.provider.ServiceProviderImpl;
...
@@ -7,8 +7,8 @@ import github.javaguide.provider.ServiceProviderImpl;
import
github.javaguide.registry.ServiceRegistry
;
import
github.javaguide.registry.ServiceRegistry
;
import
github.javaguide.registry.ZkServiceRegistry
;
import
github.javaguide.registry.ZkServiceRegistry
;
import
github.javaguide.serialize.kyro.KryoSerializer
;
import
github.javaguide.serialize.kyro.KryoSerializer
;
import
github.javaguide.transport.netty.codec.NettyKryoDecoder
;
import
github.javaguide.transport.netty.codec.
kyro.
NettyKryoDecoder
;
import
github.javaguide.transport.netty.codec.NettyKryoEncoder
;
import
github.javaguide.transport.netty.codec.
kyro.
NettyKryoEncoder
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.ChannelInitializer
;
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/netty/server/NettyServerHandler.java
浏览文件 @
e6746a46
...
@@ -4,13 +4,14 @@ import github.javaguide.dto.RpcRequest;
...
@@ -4,13 +4,14 @@ import github.javaguide.dto.RpcRequest;
import
github.javaguide.dto.RpcResponse
;
import
github.javaguide.dto.RpcResponse
;
import
github.javaguide.handler.RpcRequestHandler
;
import
github.javaguide.handler.RpcRequestHandler
;
import
github.javaguide.utils.concurrent.ThreadPoolFactory
;
import
github.javaguide.utils.concurrent.ThreadPoolFactory
;
import
github.javaguide.utils.factory.SingletonFactory
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelFutureListener
;
import
io.netty.channel.ChannelFutureListener
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
io.netty.util.ReferenceCountUtil
;
import
io.netty.util.ReferenceCountUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
io.netty.channel.SimpleChannelInboundHandler
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.ExecutorService
;
...
@@ -31,7 +32,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
...
@@ -31,7 +32,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
private
final
ExecutorService
threadPool
;
private
final
ExecutorService
threadPool
;
public
NettyServerHandler
()
{
public
NettyServerHandler
()
{
this
.
rpcRequestHandler
=
new
RpcRequestHandler
(
);
this
.
rpcRequestHandler
=
SingletonFactory
.
getInstance
(
RpcRequestHandler
.
class
);
this
.
threadPool
=
ThreadPoolFactory
.
createDefaultThreadPool
(
THREAD_NAME_PREFIX
);
this
.
threadPool
=
ThreadPoolFactory
.
createDefaultThreadPool
(
THREAD_NAME_PREFIX
);
}
}
...
...
rpc-framework-simple/src/main/java/github/javaguide/transport/socket/SocketRpcClient.java
浏览文件 @
e6746a46
...
@@ -46,7 +46,6 @@ public class SocketRpcClient implements ClientTransport {
...
@@ -46,7 +46,6 @@ public class SocketRpcClient implements ClientTransport {
RpcMessageChecker
.
check
(
rpcResponse
,
rpcRequest
);
RpcMessageChecker
.
check
(
rpcResponse
,
rpcRequest
);
return
rpcResponse
.
getData
();
return
rpcResponse
.
getData
();
}
catch
(
IOException
|
ClassNotFoundException
e
)
{
}
catch
(
IOException
|
ClassNotFoundException
e
)
{
log
.
error
(
"occur exception when send sendRpcRequest"
);
throw
new
RpcException
(
"调用服务失败:"
,
e
);
throw
new
RpcException
(
"调用服务失败:"
,
e
);
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录