Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
嗝屁小孩纸
guide-rpc-framework
提交
7090a428
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,发现更多精彩内容 >>
提交
7090a428
编写于
6月 04, 2020
作者:
S
shuang.kou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[v3.0]add CustomShutdownHook used for do something when system shutdown
上级
467b2d8d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
60 addition
and
9 deletion
+60
-9
rpc-framework-common/src/main/java/github/javaguide/utils/zk/CuratorUtils.java
...src/main/java/github/javaguide/utils/zk/CuratorUtils.java
+20
-3
rpc-framework-simple/src/main/java/github/javaguide/config/CustomShutdownHook.java
...main/java/github/javaguide/config/CustomShutdownHook.java
+31
-0
rpc-framework-simple/src/main/java/github/javaguide/registry/ZkServiceDiscovery.java
...in/java/github/javaguide/registry/ZkServiceDiscovery.java
+1
-1
rpc-framework-simple/src/main/java/github/javaguide/remoting/transport/netty/server/NettyServer.java
...avaguide/remoting/transport/netty/server/NettyServer.java
+5
-3
rpc-framework-simple/src/main/java/github/javaguide/remoting/transport/socket/SocketRpcServer.java
.../javaguide/remoting/transport/socket/SocketRpcServer.java
+3
-2
未找到文件。
rpc-framework-common/src/main/java/github/javaguide/utils/zk/CuratorUtils.java
浏览文件 @
7090a428
...
@@ -12,6 +12,7 @@ import org.apache.zookeeper.CreateMode;
...
@@ -12,6 +12,7 @@ import org.apache.zookeeper.CreateMode;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
/**
/**
...
@@ -25,6 +26,7 @@ public final class CuratorUtils {
...
@@ -25,6 +26,7 @@ public final class CuratorUtils {
private
static
final
String
CONNECT_STRING
=
"127.0.0.1:2181"
;
private
static
final
String
CONNECT_STRING
=
"127.0.0.1:2181"
;
public
static
final
String
ZK_REGISTER_ROOT_PATH
=
"/my-rpc"
;
public
static
final
String
ZK_REGISTER_ROOT_PATH
=
"/my-rpc"
;
private
static
Map
<
String
,
List
<
String
>>
serviceAddressMap
=
new
ConcurrentHashMap
<>();
private
static
Map
<
String
,
List
<
String
>>
serviceAddressMap
=
new
ConcurrentHashMap
<>();
private
static
Set
<
String
>
registeredPathSet
=
ConcurrentHashMap
.
newKeySet
();
private
static
CuratorFramework
zkClient
;
private
static
CuratorFramework
zkClient
;
static
{
static
{
...
@@ -41,13 +43,14 @@ public final class CuratorUtils {
...
@@ -41,13 +43,14 @@ public final class CuratorUtils {
*/
*/
public
static
void
createPersistentNode
(
String
path
)
{
public
static
void
createPersistentNode
(
String
path
)
{
try
{
try
{
if
(
zkClient
.
checkExists
().
forPath
(
path
)
==
null
)
{
if
(
registeredPathSet
.
contains
(
path
)
||
zkClient
.
checkExists
().
forPath
(
path
)
!=
null
)
{
log
.
info
(
"节点已经存在,节点为:[{}]"
,
path
);
}
else
{
//eg: /my-rpc/github.javaguide.HelloService/127.0.0.1:9999
//eg: /my-rpc/github.javaguide.HelloService/127.0.0.1:9999
zkClient
.
create
().
creatingParentsIfNeeded
().
withMode
(
CreateMode
.
PERSISTENT
).
forPath
(
path
);
zkClient
.
create
().
creatingParentsIfNeeded
().
withMode
(
CreateMode
.
PERSISTENT
).
forPath
(
path
);
log
.
info
(
"节点创建成功,节点为:[{}]"
,
path
);
log
.
info
(
"节点创建成功,节点为:[{}]"
,
path
);
}
else
{
log
.
info
(
"节点已经存在,节点为:[{}]"
,
path
);
}
}
registeredPathSet
.
add
(
path
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RpcException
(
e
.
getMessage
(),
e
.
getCause
());
throw
new
RpcException
(
e
.
getMessage
(),
e
.
getCause
());
}
}
...
@@ -75,6 +78,20 @@ public final class CuratorUtils {
...
@@ -75,6 +78,20 @@ public final class CuratorUtils {
return
result
;
return
result
;
}
}
/**
* 清空注册中心的数据
*/
public
static
void
clearRegistry
()
{
registeredPathSet
.
stream
().
parallel
().
forEach
(
p
->
{
try
{
zkClient
.
delete
().
forPath
(
p
);
}
catch
(
Exception
e
)
{
throw
new
RpcException
(
e
.
getMessage
(),
e
.
getCause
());
}
});
log
.
info
(
"服务端(Provider)所有注册的服务都被清空:[{}]"
,
registeredPathSet
.
toString
());
}
private
static
CuratorFramework
getZkClient
()
{
private
static
CuratorFramework
getZkClient
()
{
// 重试策略。重试3次,并且会增加重试之间的睡眠时间。
// 重试策略。重试3次,并且会增加重试之间的睡眠时间。
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
BASE_SLEEP_TIME
,
MAX_RETRIES
);
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
BASE_SLEEP_TIME
,
MAX_RETRIES
);
...
...
rpc-framework-simple/src/main/java/github/javaguide/config/CustomShutdownHook.java
0 → 100644
浏览文件 @
7090a428
package
github.javaguide.config
;
import
github.javaguide.utils.concurrent.ThreadPoolFactoryUtils
;
import
github.javaguide.utils.zk.CuratorUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.concurrent.ExecutorService
;
/**
* 当服务端(provider)关闭的时候做一些事情比如取消注册所有服务
*
* @author shuang.kou
* @createTime 2020年06月04日 13:11:00
*/
@Slf4j
public
class
CustomShutdownHook
{
private
final
ExecutorService
threadPool
=
ThreadPoolFactoryUtils
.
createDefaultThreadPool
(
"custom-shutdown-hook-rpc-pool"
);
private
static
final
CustomShutdownHook
CUSTOM_SHUTDOWN_HOOK
=
new
CustomShutdownHook
();
public
static
CustomShutdownHook
getCustomShutdownHook
()
{
return
CUSTOM_SHUTDOWN_HOOK
;
}
public
void
clearAll
()
{
log
.
info
(
"addShutdownHook for clearAll"
);
Runtime
.
getRuntime
().
addShutdownHook
(
new
Thread
(()
->
{
CuratorUtils
.
clearRegistry
();
threadPool
.
shutdown
();
}));
}
}
rpc-framework-simple/src/main/java/github/javaguide/registry/ZkServiceDiscovery.java
浏览文件 @
7090a428
...
@@ -17,7 +17,7 @@ public class ZkServiceDiscovery implements ServiceDiscovery {
...
@@ -17,7 +17,7 @@ public class ZkServiceDiscovery implements ServiceDiscovery {
@Override
@Override
public
InetSocketAddress
lookupService
(
String
serviceName
)
{
public
InetSocketAddress
lookupService
(
String
serviceName
)
{
// TODO(shuang.kou):feat: 负载均衡
// TODO(shuang.kou):feat: 负载均衡
// 这里直接去了第一个找到的服务地址,eg:127.0.0.1:9999
0000000017
// 这里直接去了第一个找到的服务地址,eg:127.0.0.1:9999
String
serviceAddress
=
CuratorUtils
.
getChildrenNodes
(
serviceName
).
get
(
0
);
String
serviceAddress
=
CuratorUtils
.
getChildrenNodes
(
serviceName
).
get
(
0
);
log
.
info
(
"成功找到服务地址:{}"
,
serviceAddress
);
log
.
info
(
"成功找到服务地址:{}"
,
serviceAddress
);
String
[]
socketAddressArray
=
serviceAddress
.
split
(
":"
);
String
[]
socketAddressArray
=
serviceAddress
.
split
(
":"
);
...
...
rpc-framework-simple/src/main/java/github/javaguide/remoting/transport/netty/server/NettyServer.java
浏览文件 @
7090a428
package
github.javaguide.remoting.transport.netty.server
;
package
github.javaguide.remoting.transport.netty.server
;
import
github.javaguide.remoting.dto.RpcRequest
;
import
github.javaguide.config.CustomShutdownHook
;
import
github.javaguide.remoting.dto.RpcResponse
;
import
github.javaguide.provider.ServiceProvider
;
import
github.javaguide.provider.ServiceProvider
;
import
github.javaguide.provider.ServiceProviderImpl
;
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.remoting.dto.RpcRequest
;
import
github.javaguide.remoting.dto.RpcResponse
;
import
github.javaguide.remoting.transport.netty.codec.kyro.NettyKryoDecoder
;
import
github.javaguide.remoting.transport.netty.codec.kyro.NettyKryoDecoder
;
import
github.javaguide.remoting.transport.netty.codec.kyro.NettyKryoEncoder
;
import
github.javaguide.remoting.transport.netty.codec.kyro.NettyKryoEncoder
;
import
github.javaguide.serialize.kyro.KryoSerializer
;
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
;
...
@@ -76,6 +77,7 @@ public class NettyServer {
...
@@ -76,6 +77,7 @@ public class NettyServer {
// 绑定端口,同步等待绑定成功
// 绑定端口,同步等待绑定成功
ChannelFuture
f
=
b
.
bind
(
host
,
port
).
sync
();
ChannelFuture
f
=
b
.
bind
(
host
,
port
).
sync
();
CustomShutdownHook
.
getCustomShutdownHook
().
clearAll
();
// 等待服务端监听端口关闭
// 等待服务端监听端口关闭
f
.
channel
().
closeFuture
().
sync
();
f
.
channel
().
closeFuture
().
sync
();
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
...
...
rpc-framework-simple/src/main/java/github/javaguide/remoting/transport/socket/SocketRpcServer.java
浏览文件 @
7090a428
package
github.javaguide.remoting.transport.socket
;
package
github.javaguide.remoting.transport.socket
;
import
github.javaguide.config.CustomShutdownHook
;
import
github.javaguide.provider.ServiceProvider
;
import
github.javaguide.provider.ServiceProvider
;
import
github.javaguide.provider.ServiceProviderImpl
;
import
github.javaguide.provider.ServiceProviderImpl
;
import
github.javaguide.registry.ServiceRegistry
;
import
github.javaguide.registry.ServiceRegistry
;
...
@@ -44,10 +45,10 @@ public class SocketRpcServer {
...
@@ -44,10 +45,10 @@ public class SocketRpcServer {
private
void
start
()
{
private
void
start
()
{
try
(
ServerSocket
server
=
new
ServerSocket
())
{
try
(
ServerSocket
server
=
new
ServerSocket
())
{
server
.
bind
(
new
InetSocketAddress
(
host
,
port
));
server
.
bind
(
new
InetSocketAddress
(
host
,
port
));
log
.
info
(
"server starts..."
);
CustomShutdownHook
.
getCustomShutdownHook
().
clearAll
(
);
Socket
socket
;
Socket
socket
;
while
((
socket
=
server
.
accept
())
!=
null
)
{
while
((
socket
=
server
.
accept
())
!=
null
)
{
log
.
info
(
"client connected
"
);
log
.
info
(
"client connected
[{}]"
,
socket
.
getInetAddress
()
);
threadPool
.
execute
(
new
SocketRpcRequestHandlerRunnable
(
socket
));
threadPool
.
execute
(
new
SocketRpcRequestHandlerRunnable
(
socket
));
}
}
threadPool
.
shutdown
();
threadPool
.
shutdown
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录