netty实现消息转发服务
1、结构图

2、消息服务器
消息服务器(SNS)由Http Netty Server(HNS)和WebSocket Netty Server(WNS)组成。HNS采用Netty Http+XML协议栈开发实现,WNS采用Netty WebSocket+JSON实现。
HNS只接收预定义的HttpXmlRequest类型的数据,这由编解码器控制,编解码器是继承了MessageToMessageDecoder<T>和MessageToMessageEncoder<T>这两个编解码基础类、并用于解析处理预定义HttpXmlRequest数据的类。HNS根据接收结果向客户端发送预定义的HttpXmlResponse类型数据。
HNS可以通过HttpXmlClient创建与业务服务器的链接,并通过HttpXmlClientHandler转发业务请求。HttpXmlClientHandler继承自SimpleChannelInboundHandler,通过它可以实现HNS与业务服务器的异步通信。
目前,WNS主要用于与Web客户端端进行websocket通信,WNS通过全局变量Global.WSCG维护通道信息,通过Global.appUsers维护客户端连接。WNS定义了一个消息基类BaseMsg,该类描述了客户端发起请求所需要的数据信息。同样,WNS也定义了一个AppUser类用于存储客户端信息,必须说明的一点是,同一个AppUser可能存在多个通道,因此,在AppUser中定义了一个ChannelId数组,该数组维护了当前用户的所有通道ID。客户端发起连接请求时,必要的数据包括appid、userId、cmd,appid是一个业务服务器的唯一标识。
3、业务服务器
业务服务器是各个应用端建立的与SNS交互的Http Netty Server,换句话说,每一个应用都需要启动一个HNS用于与SNS交互。同样的,业务服务器也是通过HttpXmlClient向SNS的HNS发起连接请求,不再赘述。
4、HttpXmlServer
package com.sns.protocol.http.xml.server; import java.net.InetSocketAddress; import com.zehin.sns.protocol.http.xml.codec.HttpXmlRequest;
import com.zehin.sns.protocol.http.xml.codec.HttpXmlRequestDecoder;
import com.zehin.sns.protocol.http.xml.codec.HttpXmlResponseEncoder;
import com.zehin.sns.protocol.http.xml.pojo.HttpRequestMessage; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder; public class HttpXmlServer implements Runnable { private EventLoopGroup bossGroup = null;
private EventLoopGroup workerGroup = null;
private SimpleChannelInboundHandler<HttpXmlRequest> handler = null; private int port = 9999; @SuppressWarnings("unused")
private HttpXmlServer() {
} public HttpXmlServer(int _port, SimpleChannelInboundHandler<HttpXmlRequest> _handler) {
this.port = _port;
this.handler = _handler;
} @Override
public void run() {
// 处理网络连接---接受请求
bossGroup = new NioEventLoopGroup();
// 进行socketChannel的网络读写
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
// boss线程接收参数设置,BACKLOG用于构造服务端套接字ServerSocket对象,
// 标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度。
// 如果未设置或所设置的值小于1,Java将使用默认值50。
.option(ChannelOption.SO_BACKLOG, 1024)
// 发送缓冲器
.option(ChannelOption.SO_SNDBUF, 1024)
// 接收缓冲器
.option(ChannelOption.SO_RCVBUF, 1024)
// 接收缓冲分配器
.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(256, 2048, 65536))
// work线程参数设置
.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(256, 2048, 65536))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
ch.pipeline().addLast("xml-decoder",
new HttpXmlRequestDecoder(HttpRequestMessage.class, true));
ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
ch.pipeline().addLast("xml-encoder", new HttpXmlResponseEncoder());
ch.pipeline().addLast("xmlServerHandler", handler);
}
});
ChannelFuture future = b.bind(new InetSocketAddress(port)).sync();
System.out.println("HTTP netty server started. the port is " + port);
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
} public void shutdown() {
if (bossGroup != null)
bossGroup.shutdownGracefully();
if (workerGroup != null)
workerGroup.shutdownGracefully();
}
}
5、HttpXmlServerHandler
package com.sns.protocol.http.xml.server; import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; import com.zehin.sns.protocol.http.xml.codec.HttpXmlRequest;
import com.zehin.sns.protocol.http.xml.codec.HttpXmlResponse;
import com.zehin.sns.protocol.http.xml.pojo.HttpRequestMessage;
import com.zehin.sns.protocol.http.xml.pojo.HttpResponseMessage; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener; @Sharable
public final class HttpXmlServerHandler extends SimpleChannelInboundHandler<HttpXmlRequest> { @Override
public void messageReceived(final ChannelHandlerContext ctx, HttpXmlRequest xmlRequest) throws Exception {
HttpRequest request = xmlRequest.getRequest();
HttpRequestMessage reqMessage = (HttpRequestMessage) xmlRequest.getBody();
System.out.println("Http server receive request : " + reqMessage);
HttpResponseMessage resMessage = dobusiness(reqMessage);
ChannelFuture future = ctx.writeAndFlush(new HttpXmlResponse(null, resMessage));
if (!isKeepAlive(request)) {
future.addListener(new GenericFutureListener<Future<? super Void>>() {
public void operationComplete(Future future) throws Exception {
ctx.close();
}
});
}
} private HttpResponseMessage dobusiness(HttpRequestMessage req) {
HttpResponseMessage resMessage = new HttpResponseMessage();
if (req.getCmd() == 0) {
resMessage.setResult(true);
} else {
// other verify code here...
}
return resMessage;
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
if (ctx.channel().isActive()) {
sendError(ctx, INTERNAL_SERVER_ERROR);
}
} private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status,
Unpooled.copiedBuffer("失败: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}
6、备注
主要参考《Netty权威指南》而写了个简单的消息转发。
netty实现消息转发服务的更多相关文章
- netty实现消息中心(二)基于netty搭建一个聊天室
前言 上篇博文(netty实现消息中心(一)思路整理 )大概说了下netty websocket消息中心的设计思路,这篇文章主要说说简化版的netty聊天室代码实现,支持群聊和点对点聊天. 此demo ...
- netty实现消息中心(一)思路整理
一.需求 需要实现直播间的以下功能: 群发消息(文本.图片.推荐商品) 点对点私发消息(文本.图片.推荐商品) 单个用户禁言 全体用户禁言 撤回消息 聊天记录持久化 二.技术实现 服务端消息中心采用n ...
- Android 基于Netty的消息推送方案之对象的传递(四)
在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...
- Android 基于Netty的消息推送方案之字符串的接收和发送(三)
在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...
- SmartRoute之大规模消息转发集群实现
消息转发的应用场景在现实中的应用非常普遍,我们常用的IM工具也是其中之一:现有很多云平台也提供了这种基础服务,可以让APP更容易集成相关功能而不必投入相应的开发成本.对于实现这样一个简单功能并不复杂, ...
- 腾讯云分布式高可靠消息队列服务CMQ架构
在分布式大行其道的今天,我们在系统内部.平台之间广泛运用消息中间件进行数据交换及解耦.CMQ是腾讯云内部自研基于的高可靠.强一致.可扩展分布式消息队列,在腾讯内部包括微信手机QQ业务红包.腾讯话费充值 ...
- Java语言快速实现简单MQ消息队列服务
目录 MQ基础回顾 主要角色 自定义协议 流程顺序 项目构建流程 具体使用流程 代码演示 消息处理中心 Broker 消息处理中心服务 BrokerServer 客户端 MqClient 测试MQ 小 ...
- 客户端(springmvc)调用netty构建的nio服务端,获得响应后返回页面(同步响应)
后面考虑通过netty做一个真正意义的简约版RPC框架,今天先尝试通过正常调用逻辑调用netty构建的nio服务端并同步获得返回信息.为后面做铺垫 服务端实现 我们先完成服务端的逻辑,逻辑很简单,把客 ...
- 使用AWS亚马逊云搭建Gmail转发服务(三)
title: 使用AWS亚马逊云搭建Gmail转发服务(三) author:青南 date: 2015-01-02 15:42:22 categories: [Python] tags: [log,G ...
随机推荐
- mysql 关键字bug
今天运行语句 select * from order; 出现bug: ERROR 1064 (42000): You have an error in your SQL syntax; check t ...
- export-data.js
var timeBtnClick = (function() { function _todayClick() { $('.select-time .today').on('click', funct ...
- Bone Collector II(HDU 2639 DP)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- GTW likes math(BC 1001)
GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1 ...
- 三维地图(BFS)
亡命逃窜 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 从前有个叫hck的骑士,为了救我们美丽的公主,潜入魔王的老巢,够英雄吧.不过英雄不是这么好当的.这个可怜的娃 ...
- 理解 backbone.js 中的 bind 和 bindAll 方法,关于如何在方法中指定其中的 this,包含apply方法的说明[转载]
转载自:http://gxxsite.com/content/view/id/132.html 在backbone.js的学习过程中,被bind和bindAll弄得有点晕,这里包括underscore ...
- linux vi 中s 替换方法
vi/vim 中可以使用 :s 命令来替换字符:s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n, ...
- jQuery UI 之 Bootstrap 快速入门
转载自(http://www.shouce.ren/example/show/s/6444) 1. 下载 这个页面是用来展示 jQuery UI Bootstrap 项目的 -- 我们将 Bootst ...
- TCP/IP协议详解概述
TCP/IP协议详解卷1--第一章概述--读书笔记 作者:vpoet 日期:2015/06/25 注:本系列的文章只是作者对TCP/IP协议的理解,难免会出现纰漏或者不完整,当然也有可能很肤浅,希望大 ...
- asp.net 错误处理
一.从客户端(...)中检测到有潜在危险的 Request.Form 值.(如图) 解决办法: 1.为 c:/windows/temp 文件夹 设置 IIS_Iusers 可读写权限 (可解决部分问题 ...