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实现消息转发服务的更多相关文章

  1. netty实现消息中心(二)基于netty搭建一个聊天室

    前言 上篇博文(netty实现消息中心(一)思路整理 )大概说了下netty websocket消息中心的设计思路,这篇文章主要说说简化版的netty聊天室代码实现,支持群聊和点对点聊天. 此demo ...

  2. netty实现消息中心(一)思路整理

    一.需求 需要实现直播间的以下功能: 群发消息(文本.图片.推荐商品) 点对点私发消息(文本.图片.推荐商品) 单个用户禁言 全体用户禁言 撤回消息 聊天记录持久化 二.技术实现 服务端消息中心采用n ...

  3. Android 基于Netty的消息推送方案之对象的传递(四)

    在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...

  4. Android 基于Netty的消息推送方案之字符串的接收和发送(三)

    在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...

  5. SmartRoute之大规模消息转发集群实现

    消息转发的应用场景在现实中的应用非常普遍,我们常用的IM工具也是其中之一:现有很多云平台也提供了这种基础服务,可以让APP更容易集成相关功能而不必投入相应的开发成本.对于实现这样一个简单功能并不复杂, ...

  6. 腾讯云分布式高可靠消息队列服务CMQ架构

    在分布式大行其道的今天,我们在系统内部.平台之间广泛运用消息中间件进行数据交换及解耦.CMQ是腾讯云内部自研基于的高可靠.强一致.可扩展分布式消息队列,在腾讯内部包括微信手机QQ业务红包.腾讯话费充值 ...

  7. Java语言快速实现简单MQ消息队列服务

    目录 MQ基础回顾 主要角色 自定义协议 流程顺序 项目构建流程 具体使用流程 代码演示 消息处理中心 Broker 消息处理中心服务 BrokerServer 客户端 MqClient 测试MQ 小 ...

  8. 客户端(springmvc)调用netty构建的nio服务端,获得响应后返回页面(同步响应)

    后面考虑通过netty做一个真正意义的简约版RPC框架,今天先尝试通过正常调用逻辑调用netty构建的nio服务端并同步获得返回信息.为后面做铺垫 服务端实现 我们先完成服务端的逻辑,逻辑很简单,把客 ...

  9. 使用AWS亚马逊云搭建Gmail转发服务(三)

    title: 使用AWS亚马逊云搭建Gmail转发服务(三) author:青南 date: 2015-01-02 15:42:22 categories: [Python] tags: [log,G ...

随机推荐

  1. Django1.7官方文档中的tutorial——翻译

    写下你的第一个Django应用,第一部分 让我们通过例子来学习. 通过这篇指南,我们将会带你浏览一遍一个基本投票应用的创建. 它由两部分组成: 1一个让人们查看投票和进行投票的公共站点 2一个让你添加 ...

  2. OS-MAC: An Efficient MAC Protocol for Spectrum-Agile Wireless Networks

    IEEE TRANSACTIONS ON MOBILE COMPUTING, VOL. 7, NO. 8, AUGUST 2008 正如之前所说的,这是一个out-of-band local cove ...

  3. Android的debug.keystore拒绝访问导致的生成异常及解决方案

    构建Android应用程序的时候输出异常:[apkbuilder] keytool 错误: java.io.FileNotFoundException: C:\Users\my\.android\de ...

  4. httrack,webdup,WinHTTrack,WebZip

    怎么下载摄像头游戏jabbo,并使其能离线运行?修改 1.摄像头游戏jabbo:JABBO Ultimatum by LiveMurals Interactive电脑为:windows 7 32位.试 ...

  5. H.264 RTP 封包格式

    H.264 视频 RTP 负载格式 1. 网络抽象层单元类型 (NALU) NALU 头由一个字节组成, 它的语法如下: +---------------+      |0|1|2|3|4|5|6|7 ...

  6. ###Android 断点调试和高级调试###

    转自:http://www.2cto.com/kf/201506/408358.html 有人说Android 的调试是最坑的,那我只能说是你不会用而已,我可以说Android Studio的调试是我 ...

  7. Java开发者工具

    From:http://www.csdn.net/article/2015-03-26/2824317 1. Notepad++ Notepad++是用于编辑xml.脚本以及记笔记的最佳工具.这个工具 ...

  8. noip2014总结

    noip总结 经过七周的停课,我们终于迎来了期盼已久的noip考试.在这一次的noip考试中,我们经历了很多,也收获了很多.当然这一次考试中也有很多值得总结的地方,特写此总结. 这一次考试考得还不错, ...

  9. [Oracle] Data Guard 系列(5) - 创建逻辑备库

    在创建逻辑备库之前,必须得先创建物理备库,关于如何创建物理备库,请参考<Data Guard 系列(4) - 在不停主库的情况下创建物理备库>. 1. 在物理备库上停止日志应用服务 SYS ...

  10. iOS SDK原生JSON解析

    - (IBAction)touchReadButton:(id)sender { NSData *jsonData = [[NSData alloc] initWithContentsOfFile:J ...