package com.almond;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class MyAlmondServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup work = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss, work).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO)).childHandler(new MyAlmondInitlizer());
Channel channel = serverBootstrap.bind(8899).sync().channel();
channel.closeFuture().sync();
} finally {
boss.shutdownGracefully();
work.shutdownGracefully();
}
}
}
以上代码,EventLoopGroup是循环组,boss负责接受任务,work负责处理任务,handler是针对boss的循环组,childHandler是针对work循环组,closeFuture是长连接
服务端有initlizer,也有handler
package com.demo5;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
public class MyServerInitlizer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// 因为websocket基于http协议
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new ChunkedWriteHandler());
// 对http消息进行聚合,在fullHttprequest或者fullHttpresponse 能把分段的请求聚合到一起
pipeline.addLast(new HttpObjectAggregator(8192));
pipeline.addLast(new WebSocketServerProtocolHandler("/wsws"));
pipeline.addLast(new MyTextWebSocketFrameHandler());
}
}
还有handler
package com.demo5;
import java.time.LocalDateTime;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
public class MyTextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
System.out.println("收到消息" + msg.text());
ctx.channel().writeAndFlush(new TextWebSocketFrame("服务器时间" + LocalDateTime.now()));
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println("handlerAdded:" + ctx.channel().id().asLongText());
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
System.out.println("handlerRemoved:" + ctx.channel().id().asLongText());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("异常:"+cause.getMessage());
ctx.close();
}
}
各种handler处理的组件是在handler里添加,就是addlast,而关于处理逻辑,接受参数和返回值的信息,在handler里设置,包括其中有建立连接的各个步骤
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
以上容器是管道组,服务器端的ChannelHandlerContext得到的channel,就是每个访问者各自的channel,传递给服务器的channel管道中
代码地址 https://gitee.com/chijingsanxun/nettying.git
连接的时候,是先通过http取得连接,然后通过handler将起升级为websocket连接,就变成了长连接,这其中有很多好处,其中之一是不需要频繁连接,无需使用循环组频繁互相请求,还有就是头文件等只要连接一次就能一直使用
接口调用的时候,用rmi连接,可以实现项目之间的方法调用,但是因为rmi只有在服务端和客户端都是Java语言写的基础上才能encode(编码) 和decode(解码),也就是序列化,所以功能受限
而有更强大的rpc协议,可以跨语言实现方法调用