Netty4整合到Spring boot

项目搭建

添加依赖包

1
2
3
4
5
6
7
<!--Netty-->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.36.Final</version>
</dependency>
<!--Netty END-->

修改配置文件

  1. application.properties重命名为application.yml
  2. 添加以下配置:

    1
    2
    3
    n:
    port: 7000
    url: 127.0.0.1

编写服务端

NettyServer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package club.maoyupeng.demo.sslnetty.web.socket.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
* <b>标题: Netty 服务 </b><br />
* <pre>
* </pre>
*
* @author 毛宇鹏
* @date 创建于 上午9:02 2018/4/25
*/
@Component
public class NettyServer {

private static final Logger log = Logger.getLogger(NettyServer.class);

private final EventLoopGroup bossGroup = new NioEventLoopGroup();
private final EventLoopGroup workerGroup = new NioEventLoopGroup();

private Channel channel;

/**
* 启动服务
*/
public ChannelFuture run (InetSocketAddress address) {

ChannelFuture f = null;
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerChannelInitializer())
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);

f = b.bind(address).syncUninterruptibly();
channel = f.channel();
} catch (Exception e) {
log.error("Netty start error:", e);
} finally {
if (f != null && f.isSuccess()) {
log.info("Netty server listening " + address.getHostName() + " on port " + address.getPort() + " and ready for connections...");
} else {
log.error("Netty server start up Error!");
}
}

return f;
}

public void destroy() {
log.info("Shutdown Netty Server...");
if(channel != null) { channel.close();}
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
log.info("Shutdown Netty Server Success!");
}
}

ServerChannelInitializer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package club.maoyupeng.demo.sslnetty.web.socket.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

/**
* <b>标题: </b><br />
* <pre>
* </pre>
*
* @author 毛宇鹏
* @date 创建于 上午9:13 2018/4/25
*/
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) {
// 解码编码
socketChannel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
socketChannel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));

socketChannel.pipeline().addLast(new ServerHandler());
}
}

ServerHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package club.maoyupeng.demo.sslnetty.web.socket.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
* <b>标题: </b><br />
* <pre>
* </pre>
*
* @author 毛宇鹏
* @date 创建于 上午9:14 2018/4/25
*/
public class ServerHandler extends SimpleChannelInboundHandler<Object> {

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) {
System.out.println("server receive message :"+ msg);
ctx.channel().writeAndFlush("yes server already accept your message" + msg);
ctx.close();
}

@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("channelActive>>>>>>>>");
}
}

编写客户端

NettyClient.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package club.maoyupeng.demo.sslnetty.web.socket.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;
import org.apache.log4j.Logger;

import java.net.InetSocketAddress;

/**
* <b>标题: </b><br />
* <pre>
* </pre>
*
* @author 毛宇鹏
* @date 创建于 上午10:56 2018/4/25
*/
public class NettyClient {

private static final Logger log = Logger.getLogger(NettyClient.class);

private static Bootstrap b;
private static ChannelFuture f;
private static final EventLoopGroup workerGroup = new NioEventLoopGroup();

private static void init () {
try {
log.info("init...");
b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
// 解码编码
socketChannel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
socketChannel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));

socketChannel.pipeline().addLast(new ClientHandler());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}

public static Object startAndWrite(InetSocketAddress address, Object send) throws InterruptedException {
init();
f = b.connect(address).sync();
// 传数据给服务端
f.channel().writeAndFlush(send);
f.channel().closeFuture().sync();
return f.channel().attr(AttributeKey.valueOf("Attribute_key")).get();
}

public static void main(String[] args) {
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 7000);
String message = "hello";
try {
Object result = NettyClient.startAndWrite(address, message);
log.info("....result:" + result);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
f.channel().close();
workerGroup.shutdownGracefully();
log.info("Closed client!");
}
}
}

ClientHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package club.maoyupeng.demo.sslnetty.web.socket.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;

/**
* <b>标题: </b><br />
* <pre>
* </pre>
*
* @author 毛宇鹏
* @date 创建于 上午11:25 2018/4/25
*/
public class ClientHandler extends SimpleChannelInboundHandler<Object> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object msg) {
channelHandlerContext.channel().attr(AttributeKey.valueOf("Attribute_key")).set(msg);
channelHandlerContext.close();
}
}

配置 Springboot 启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package club.maoyupeng.demo.sslnetty;

import club.maoyupeng.demo.sslnetty.web.socket.server.NettyServer;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.InetSocketAddress;

/**
* @author 毛宇鹏 http://www.maoyupeng.club
*/
@SpringBootApplication
public class SslNettyApplication implements CommandLineRunner {

@Value("${n.port}")
private int port;

@Value("${n.url}")
private String url;

@Autowired
private NettyServer socketServer;

public static void main(String[] args) {
SpringApplication.run(SslNettyApplication.class, args);
}

@Override
public void run(String... strings) {
InetSocketAddress address = new InetSocketAddress(url, port);
ChannelFuture future = socketServer.run(address);
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
socketServer.destroy();
}
});
future.channel().closeFuture().syncUninterruptibly();
}
}

注意事项

由于spring boot启动后, 接着启动netty, 所以netty会造成io阻塞, 当打包时, 必须在maven设置 打包跳过测试

1
2
3
4
5
6
7
8
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
坚持原创技术分享,您的支持将鼓励我继续创作!对, 你想得对, 我真的缺你的这几块钱!
0%