Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lijianghuflute
canal
提交
780b4f71
canal
项目概览
lijianghuflute
/
canal
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
canal
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
780b4f71
编写于
7月 05, 2017
作者:
A
agapple
提交者:
GitHub
7月 05, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #333 from jasonhx140/master
fix bug:异常网络连接导致canal server 无法关闭
上级
8e88dc87
8600e982
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
40 addition
and
2 deletion
+40
-2
server/src/main/java/com/alibaba/otter/canal/server/netty/CanalServerWithNetty.java
...libaba/otter/canal/server/netty/CanalServerWithNetty.java
+24
-1
server/src/main/java/com/alibaba/otter/canal/server/netty/handler/ClientAuthenticationHandler.java
...nal/server/netty/handler/ClientAuthenticationHandler.java
+4
-1
server/src/main/java/com/alibaba/otter/canal/server/netty/handler/HandshakeInitializationHandler.java
.../server/netty/handler/HandshakeInitializationHandler.java
+12
-0
未找到文件。
server/src/main/java/com/alibaba/otter/canal/server/netty/CanalServerWithNetty.java
浏览文件 @
780b4f71
...
@@ -9,6 +9,8 @@ import org.jboss.netty.channel.Channel;
...
@@ -9,6 +9,8 @@ import org.jboss.netty.channel.Channel;
import
org.jboss.netty.channel.ChannelPipeline
;
import
org.jboss.netty.channel.ChannelPipeline
;
import
org.jboss.netty.channel.ChannelPipelineFactory
;
import
org.jboss.netty.channel.ChannelPipelineFactory
;
import
org.jboss.netty.channel.Channels
;
import
org.jboss.netty.channel.Channels
;
import
org.jboss.netty.channel.group.ChannelGroup
;
import
org.jboss.netty.channel.group.DefaultChannelGroup
;
import
org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory
;
import
org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory
;
import
com.alibaba.otter.canal.common.AbstractCanalLifeCycle
;
import
com.alibaba.otter.canal.common.AbstractCanalLifeCycle
;
...
@@ -32,6 +34,7 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
...
@@ -32,6 +34,7 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
private
int
port
;
private
int
port
;
private
Channel
serverChannel
=
null
;
private
Channel
serverChannel
=
null
;
private
ServerBootstrap
bootstrap
=
null
;
private
ServerBootstrap
bootstrap
=
null
;
private
ChannelGroup
childGroups
=
null
;
//socket channel container, used to close sockets explicitly.
private
static
class
SingletonHolder
{
private
static
class
SingletonHolder
{
...
@@ -40,6 +43,7 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
...
@@ -40,6 +43,7 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
private
CanalServerWithNetty
(){
private
CanalServerWithNetty
(){
this
.
embeddedServer
=
CanalServerWithEmbedded
.
instance
();
this
.
embeddedServer
=
CanalServerWithEmbedded
.
instance
();
this
.
childGroups
=
new
DefaultChannelGroup
();
}
}
public
static
CanalServerWithNetty
instance
()
{
public
static
CanalServerWithNetty
instance
()
{
...
@@ -55,6 +59,19 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
...
@@ -55,6 +59,19 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
this
.
bootstrap
=
new
ServerBootstrap
(
new
NioServerSocketChannelFactory
(
Executors
.
newCachedThreadPool
(),
this
.
bootstrap
=
new
ServerBootstrap
(
new
NioServerSocketChannelFactory
(
Executors
.
newCachedThreadPool
(),
Executors
.
newCachedThreadPool
()));
Executors
.
newCachedThreadPool
()));
/*
* enable keep-alive mechanism, handle abnormal network connection scenarios on OS level.
* the threshold parameters are depended on OS.
* e.g. On Linux:
* net.ipv4.tcp_keepalive_time = 300
* net.ipv4.tcp_keepalive_probes = 2
* net.ipv4.tcp_keepalive_intvl = 30
*/
bootstrap
.
setOption
(
"child.keepAlive"
,
true
);
/*
* optional parameter.
*/
bootstrap
.
setOption
(
"child.tcpNoDelay"
,
true
);
// 构造对应的pipeline
// 构造对应的pipeline
bootstrap
.
setPipelineFactory
(
new
ChannelPipelineFactory
()
{
bootstrap
.
setPipelineFactory
(
new
ChannelPipelineFactory
()
{
...
@@ -62,7 +79,8 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
...
@@ -62,7 +79,8 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
public
ChannelPipeline
getPipeline
()
throws
Exception
{
public
ChannelPipeline
getPipeline
()
throws
Exception
{
ChannelPipeline
pipelines
=
Channels
.
pipeline
();
ChannelPipeline
pipelines
=
Channels
.
pipeline
();
pipelines
.
addLast
(
FixedHeaderFrameDecoder
.
class
.
getName
(),
new
FixedHeaderFrameDecoder
());
pipelines
.
addLast
(
FixedHeaderFrameDecoder
.
class
.
getName
(),
new
FixedHeaderFrameDecoder
());
pipelines
.
addLast
(
HandshakeInitializationHandler
.
class
.
getName
(),
new
HandshakeInitializationHandler
());
//support to maintain child socket channel.
pipelines
.
addLast
(
HandshakeInitializationHandler
.
class
.
getName
(),
new
HandshakeInitializationHandler
(
childGroups
));
pipelines
.
addLast
(
ClientAuthenticationHandler
.
class
.
getName
(),
pipelines
.
addLast
(
ClientAuthenticationHandler
.
class
.
getName
(),
new
ClientAuthenticationHandler
(
embeddedServer
));
new
ClientAuthenticationHandler
(
embeddedServer
));
...
@@ -87,6 +105,11 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
...
@@ -87,6 +105,11 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
this
.
serverChannel
.
close
().
awaitUninterruptibly
(
1000
);
this
.
serverChannel
.
close
().
awaitUninterruptibly
(
1000
);
}
}
//close sockets explicitly to reduce socket channel hung in complicated network environment.
if
(
this
.
childGroups
!=
null
)
{
this
.
childGroups
.
close
().
awaitUninterruptibly
(
5000
);
}
if
(
this
.
bootstrap
!=
null
)
{
if
(
this
.
bootstrap
!=
null
)
{
this
.
bootstrap
.
releaseExternalResources
();
this
.
bootstrap
.
releaseExternalResources
();
}
}
...
...
server/src/main/java/com/alibaba/otter/canal/server/netty/handler/ClientAuthenticationHandler.java
浏览文件 @
780b4f71
package
com.alibaba.otter.canal.server.netty.handler
;
package
com.alibaba.otter.canal.server.netty.handler
;
import
java.util.concurrent.TimeUnit
;
import
org.apache.commons.lang.StringUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.jboss.netty.buffer.ChannelBuffer
;
import
org.jboss.netty.buffer.ChannelBuffer
;
import
org.jboss.netty.channel.ChannelFuture
;
import
org.jboss.netty.channel.ChannelFuture
;
...
@@ -87,10 +89,11 @@ public class ClientAuthenticationHandler extends SimpleChannelHandler {
...
@@ -87,10 +89,11 @@ public class ClientAuthenticationHandler extends SimpleChannelHandler {
if
(
clientAuth
.
getNetWriteTimeout
()
>
0
)
{
if
(
clientAuth
.
getNetWriteTimeout
()
>
0
)
{
writeTimeout
=
clientAuth
.
getNetWriteTimeout
();
writeTimeout
=
clientAuth
.
getNetWriteTimeout
();
}
}
//fix bug: soTimeout parameter's unit from connector is millseconds.
IdleStateHandler
idleStateHandler
=
new
IdleStateHandler
(
NettyUtils
.
hashedWheelTimer
,
IdleStateHandler
idleStateHandler
=
new
IdleStateHandler
(
NettyUtils
.
hashedWheelTimer
,
readTimeout
,
readTimeout
,
writeTimeout
,
writeTimeout
,
0
);
0
,
TimeUnit
.
MILLISECONDS
);
ctx
.
getPipeline
().
addBefore
(
SessionHandler
.
class
.
getName
(),
ctx
.
getPipeline
().
addBefore
(
SessionHandler
.
class
.
getName
(),
IdleStateHandler
.
class
.
getName
(),
IdleStateHandler
.
class
.
getName
(),
idleStateHandler
);
idleStateHandler
);
...
...
server/src/main/java/com/alibaba/otter/canal/server/netty/handler/HandshakeInitializationHandler.java
浏览文件 @
780b4f71
...
@@ -3,6 +3,7 @@ package com.alibaba.otter.canal.server.netty.handler;
...
@@ -3,6 +3,7 @@ package com.alibaba.otter.canal.server.netty.handler;
import
org.jboss.netty.channel.ChannelHandlerContext
;
import
org.jboss.netty.channel.ChannelHandlerContext
;
import
org.jboss.netty.channel.ChannelStateEvent
;
import
org.jboss.netty.channel.ChannelStateEvent
;
import
org.jboss.netty.channel.SimpleChannelHandler
;
import
org.jboss.netty.channel.SimpleChannelHandler
;
import
org.jboss.netty.channel.group.ChannelGroup
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -18,10 +19,21 @@ import com.alibaba.otter.canal.server.netty.NettyUtils;
...
@@ -18,10 +19,21 @@ import com.alibaba.otter.canal.server.netty.NettyUtils;
* @version 1.0.0
* @version 1.0.0
*/
*/
public
class
HandshakeInitializationHandler
extends
SimpleChannelHandler
{
public
class
HandshakeInitializationHandler
extends
SimpleChannelHandler
{
//support to maintain socket channel.
private
ChannelGroup
childGroups
;
public
HandshakeInitializationHandler
(
ChannelGroup
childGroups
)
{
this
.
childGroups
=
childGroups
;
}
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
HandshakeInitializationHandler
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
HandshakeInitializationHandler
.
class
);
public
void
channelOpen
(
ChannelHandlerContext
ctx
,
ChannelStateEvent
e
)
throws
Exception
{
public
void
channelOpen
(
ChannelHandlerContext
ctx
,
ChannelStateEvent
e
)
throws
Exception
{
//add new socket channel in channel container, used to manage sockets.
if
(
childGroups
!=
null
)
{
childGroups
.
add
(
ctx
.
getChannel
());
}
byte
[]
body
=
Packet
.
newBuilder
()
byte
[]
body
=
Packet
.
newBuilder
()
.
setType
(
CanalPacket
.
PacketType
.
HANDSHAKE
)
.
setType
(
CanalPacket
.
PacketType
.
HANDSHAKE
)
.
setBody
(
Handshake
.
newBuilder
().
build
().
toByteString
())
.
setBody
(
Handshake
.
newBuilder
().
build
().
toByteString
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录