Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
caopu16
whatsmars
提交
588d1f1e
W
whatsmars
项目概览
caopu16
/
whatsmars
与 Fork 源项目一致
Fork自
武汉红喜 / whatsmars
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
588d1f1e
编写于
10月 05, 2018
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
whatsmars-remoting
上级
c28ab4c6
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
201 addition
and
0 deletion
+201
-0
pom.xml
pom.xml
+1
-0
whatsmars-remoting/README.md
whatsmars-remoting/README.md
+4
-0
whatsmars-remoting/pom.xml
whatsmars-remoting/pom.xml
+26
-0
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeClient.java
...java/org/hongxi/whatsmars/remoting/netty4/TimeClient.java
+45
-0
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeClientHandler.java
...g/hongxi/whatsmars/remoting/netty4/TimeClientHandler.java
+39
-0
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeServer.java
...java/org/hongxi/whatsmars/remoting/netty4/TimeServer.java
+51
-0
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeServerHandler.java
...g/hongxi/whatsmars/remoting/netty4/TimeServerHandler.java
+35
-0
未找到文件。
pom.xml
浏览文件 @
588d1f1e
...
...
@@ -24,6 +24,7 @@
<module>
whatsmars-sharding-jdbc
</module>
<module>
whatsmars-earth
</module>
<module>
whatsmars-spring-boot-samples
</module>
<module>
whatsmars-remoting
</module>
</modules>
<properties>
...
...
whatsmars-remoting/README.md
0 → 100644
浏览文件 @
588d1f1e
Netty is an asynchronous event-driven network application framework
for rapid development of maintainable high performance protocol servers & clients.
[
《Netty权威指南》
](
http://e.jd.com/30186249.html
)
`e.jd.com`
\ No newline at end of file
whatsmars-remoting/pom.xml
0 → 100644
浏览文件 @
588d1f1e
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
whatsmars-parent
</artifactId>
<groupId>
org.hongxi
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
whatsmars-remoting
</artifactId>
<packaging>
jar
</packaging>
<name>
${project.artifactId}
</name>
<url>
http://maven.apache.org
</url>
<dependencies>
<!-- netty4 -->
<dependency>
<groupId>
io.netty
</groupId>
<artifactId>
netty-all
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeClient.java
0 → 100644
浏览文件 @
588d1f1e
package
org.hongxi.whatsmars.remoting.netty4
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
/**
* Created by shenhongxi on 2018/10/5.
*/
public
class
TimeClient
{
public
void
connect
(
int
port
,
String
host
)
throws
Exception
{
// 配置客户端NIO线程组
EventLoopGroup
group
=
new
NioEventLoopGroup
();
try
{
Bootstrap
bootstrap
=
new
Bootstrap
();
bootstrap
.
group
(
group
).
channel
(
NioSocketChannel
.
class
)
.
option
(
ChannelOption
.
TCP_NODELAY
,
true
)
.
handler
(
new
ChannelInitializer
<
SocketChannel
>()
{
@Override
protected
void
initChannel
(
SocketChannel
socketChannel
)
throws
Exception
{
socketChannel
.
pipeline
().
addLast
(
new
TimeClientHandler
());
}
});
// 发起异步连接操作
ChannelFuture
future
=
bootstrap
.
connect
(
host
,
port
).
sync
();
// 等待客户端链路关闭
future
.
channel
().
closeFuture
().
sync
();
}
finally
{
// 优雅退出,释放NIO线程组
group
.
shutdownGracefully
();
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
TimeClient
().
connect
(
8080
,
"127.0.0.1"
);
}
}
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeClientHandler.java
0 → 100644
浏览文件 @
588d1f1e
package
org.hongxi.whatsmars.remoting.netty4
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.Unpooled
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
/**
* Created by shenhongxi on 2018/10/5.
*/
public
class
TimeClientHandler
extends
ChannelInboundHandlerAdapter
{
private
final
ByteBuf
firstMessage
;
public
TimeClientHandler
()
{
byte
[]
req
=
"QUERY TIME COMMAND"
.
getBytes
();
firstMessage
=
Unpooled
.
buffer
(
req
.
length
);
firstMessage
.
writeBytes
(
req
);
}
@Override
public
void
channelActive
(
ChannelHandlerContext
ctx
)
throws
Exception
{
ctx
.
writeAndFlush
(
firstMessage
);
}
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
ByteBuf
buf
=
(
ByteBuf
)
msg
;
byte
[]
req
=
new
byte
[
buf
.
readableBytes
()];
buf
.
readBytes
(
req
);
String
body
=
new
String
(
req
,
"UTF-8"
);
System
.
out
.
println
(
"Now is : "
+
body
);
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
ctx
.
close
();
}
}
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeServer.java
0 → 100644
浏览文件 @
588d1f1e
package
org.hongxi.whatsmars.remoting.netty4
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.channel.socket.nio.NioServerSocketChannel
;
/**
* Created by shenhongxi on 2018/10/5.
*/
public
class
TimeServer
{
public
void
bind
(
int
port
)
throws
Exception
{
// 配置服务端的NIO线程组
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
();
EventLoopGroup
workerGroup
=
new
NioEventLoopGroup
();
try
{
ServerBootstrap
bootstrap
=
new
ServerBootstrap
();
bootstrap
.
group
(
bossGroup
,
workerGroup
)
.
channel
(
NioServerSocketChannel
.
class
)
.
option
(
ChannelOption
.
SO_BACKLOG
,
1024
)
.
childHandler
(
new
ChildChannelHandler
());
// 绑定端口,同步等待成功
ChannelFuture
future
=
bootstrap
.
bind
(
port
).
sync
();
System
.
out
.
println
(
"Server started!"
);
// 等待服务端监听端口关闭
future
.
channel
().
closeFuture
().
sync
();
}
finally
{
// 优雅退出,释放线程池资源
bossGroup
.
shutdownGracefully
();
workerGroup
.
shutdownGracefully
();
}
}
private
class
ChildChannelHandler
extends
ChannelInitializer
<
SocketChannel
>
{
@Override
protected
void
initChannel
(
SocketChannel
socketChannel
)
throws
Exception
{
socketChannel
.
pipeline
().
addLast
(
new
TimeServerHandler
());
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
TimeServer
().
bind
(
8080
);
}
}
whatsmars-remoting/src/main/java/org/hongxi/whatsmars/remoting/netty4/TimeServerHandler.java
0 → 100644
浏览文件 @
588d1f1e
package
org.hongxi.whatsmars.remoting.netty4
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.Unpooled
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
java.util.Date
;
/**
* Created by shenhongxi on 2018/10/5.
*/
public
class
TimeServerHandler
extends
ChannelInboundHandlerAdapter
{
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
ByteBuf
buf
=
(
ByteBuf
)
msg
;
byte
[]
req
=
new
byte
[
buf
.
readableBytes
()];
buf
.
readBytes
(
req
);
String
body
=
new
String
(
req
,
"UTF-8"
);
System
.
out
.
println
(
"The time server receive command : "
+
body
);
String
currentTimes
=
"QUERY TIME COMMAND"
.
equalsIgnoreCase
(
body
)
?
new
Date
().
toString
()
:
"BAD COMMAND"
;
ByteBuf
resp
=
Unpooled
.
copiedBuffer
(
currentTimes
.
getBytes
());
ctx
.
write
(
resp
);
}
@Override
public
void
channelReadComplete
(
ChannelHandlerContext
ctx
)
throws
Exception
{
ctx
.
flush
();
}
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
ctx
.
close
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录