当前位置: 首页 > news >正文

网站排名做不上去吗企业建站系统费用

网站排名做不上去吗,企业建站系统费用,wordpress二维码活码,如何增加网站的流量Netty Bootstrap和ServerBootstrapFuture和ChannelFutureChannelSelectorNioEventLoop和NioEventLoopGroupByteBuf示例代码 Channel相关组件入站详情出站详情对象编解码ProtoBuf和ProtoStuffnetty实现群聊系统粘包和拆包TCP协议特点举个例子 Bootstrap和ServerBootstrap Boots… Netty Bootstrap和ServerBootstrapFuture和ChannelFutureChannelSelectorNioEventLoop和NioEventLoopGroupByteBuf示例代码 Channel相关组件入站详情出站详情对象编解码ProtoBuf和ProtoStuffnetty实现群聊系统粘包和拆包TCP协议特点举个例子 Bootstrap和ServerBootstrap Bootstrap是Netty的启动程序一个Netty应用通常由一个Bootstrap开始。Bootstrap的主要作用是配置Netty程序串联Netty的各个组件。 Future和ChannelFuture 这个方法是异步的交给别的线程去执行该任务当执行到这之后netty不一定启动了 ChannelFuture channelFuture bootstrap.bind(9090); channelFuture.addListener(new ChannelFutureListener() {Overridepublic void operationComplete(ChannelFuture funture) throws Exception {if(funture.isSuccess()){System.out.println(监听9090成功);}else{System.out.println(监听9090失败);}} });该方法可以知晓有没有启动成功或者改为同步的方式 ChannelFuture channelFuture bootstrap.bind(9090).sync(); ChannelFuture和Future的子类提供了针对于Channel的异步监听操作 Channel NioSocketChannel:异步的客户端才CP Socket连接通道 NioServerSocketChannel:异步的服务端TCP Socket连接通道 NioDatagramChannel:异步的UDP连接通道 NioSctpChannel:异步的客户端Sctp连接通道 NioSctpServerChannel:异步的服务端Sctp连接通道 Selector 通过Selector多路复用器实现IO的多路复用。Selector可以监听多个连接的Channel事件同时可以不断的查询已注册Channel是否处于就绪状态实现一个线程可以高效的管理多个Channel。 NioEventLoop和NioEventLoopGroup NioEventLoop本质就是一个线程一个NioEventLoop就对应一个线程但可以达到异步的处理事务因为NioEventLoop内部维护了一个异步任务队列用于存储需要在事件循环中执行的任务。通过将任务添加到队列中NioEventLoop可以在空闲时间执行这些任务从而实现了异步提交事务的能力。 NioEventLoopGroup管理NioEventLoop的生命周期可以理解为是线程池内部维护了一组线程。每个线程即NioEventLoop)负责处理多个Channel上的事件。注意一个Channel只对应一个线程NioEventLoop和Channel它们是一对多的关系。 一个线程可以管理多个channel但一个channel只能被一个线程执行 ByteBuf 初始情况 写入数据 读取数据 已读的区域[0,readerIndex] 可读的区域[readIndex,writeIndex) 可写的区域[writeIndex,capacity) 示例代码 public class ByteBufDemo {public static void main(String[] args) {//创建一个有10个容量数据的ByteBuf对象ByteBuf buf Unpooled.buffer(10);System.out.println(init buf:buf);//添加数据for(int i 0;i5;i){buf.writeByte(i);}System.out.println(after write:buf);//按照索引读取数据for(int i 0;i3;i){System.out.println(buf.getByte(i));}System.out.println(after get:buf);//读取数据for(int i 0;i3;i){System.out.println(buf.readByte());}System.out.println(after read:buf);} }Channel相关组件 ChannelHandler ChannelHandler用于处理拦截IO事件往往在ChannelHandler中可以加入业务处理逻辑。ChannelHandler执行完后会将io事件转发到ChannelPipeline中的下一个处理程序。 ChannelHandlerContext 保存Channel相关的上下文信息并关联一个ChannelHandler对象。 ChannelPipeline ChannelPipeline是一个双向链表其中保存着多个ChannelHandler。ChannelPipeline实现了一种高级形式的过滤器模式在IO操作时发生的入站和出站事件会导致ChannelPipeline中的多个ChannelHandler被依次调用。 入站详情 现在我们的客户端和服务端之间就有三个拦截器 我们在NettyServerHandler里面收到信息就不用解码了为什么因为解码器的拦截器已经帮我们做好了 当我们服务端读数据的时候会从客户端读数据入站因为解码的handler和业务处理的handler是入站拦截器所以会对数据产生作用但编码的handler不会因为它是一个出站handler 出站详情 站在服务端的立场 在Netty中客户端和服务端的addLast方法有一些不同之处。具体来说它们的区别如下 1. 顺序当调用addLast方法添加处理器时它们的顺序略有不同。对于客户端来说添加的处理器是按照添加的顺序进行顺序执行的即先添加的处理器先执行。而对于服务端来说添加的处理器是按照逆序执行的即先添加的处理器后执行。 2. 作用对象客户端的addLast方法主要作用于Outbound事件用于处理从客户端发送到服务端的请求。而服务端的addLast方法主要作用于Inbound事件用于处理从服务端接收到的请求。 3. 处理逻辑客户端和服务端的addLast方法所添加的处理器通常具有不同的处理逻辑。客户端的处理器通常用于编码请求、发送请求等操作。服务端的处理器通常用于解码请求、处理请求、编码响应等操作。 对象编解码 对象编码器 对象解码器 ProtoBuf和ProtoStuff 为了编解码提升性能可以使用Protobuf域者Protpstuff对数据进行序列话和反序列化效率更高。 第一步导入依赖 dependencygroupIdcom.dyuproject.protostuff/groupIdartifactIdprotostuff-api/artifactIdversion1.0.10/version /dependencydependencygroupIdcom.dyuproject.protostuff/groupIdartifactIdprotostuff-core/artifactIdversion1.0.10/version /dependencydependencygroupIdcom.dyuproject.protostuff/groupIdartifactIdprotostuff-runtime/artifactIdversion1.0.11/version /dependency第二步引入工具类 public class ProtostuffUtils {/*** 避免每次序列化都重新申请Buffer空间*/private static LinkedBuffer buffer LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);/*** 缓存Schema*/private static MapClass?, Schema? schemaCache new ConcurrentHashMap();/*** 序列化方法把指定对象序列化成字节数组** param obj* param T* return*/SuppressWarnings(unchecked)public static T byte[] serialize(T obj) {ClassT clazz (ClassT) obj.getClass();SchemaT schema getSchema(clazz);byte[] data;try {data ProtostuffIOUtil.toByteArray(obj, schema, buffer);} finally {buffer.clear();}return data;}/*** 反序列化方法将字节数组反序列化成指定Class类型** param data* param clazz* param T* return*/public static T T deserialize(byte[] data, ClassT clazz) {SchemaT schema getSchema(clazz);T obj schema.newMessage();ProtostuffIOUtil.mergeFrom(data, obj, schema);return obj;}SuppressWarnings(unchecked)private static T SchemaT getSchema(ClassT clazz) {SchemaT schema (SchemaT) schemaCache.get(clazz);if (Objects.isNull(schema)) {//这个schema通过RuntimeSchema进行懒创建并缓存//所以可以一直调用RuntimeSchema.getSchema(),这个方法是线程安全的schema RuntimeSchema.getSchema(clazz);if (Objects.nonNull(schema)) {schemaCache.put(clazz, schema);}}return schema;} }netty实现群聊系统 服务端基本代码 //群聊系统的服务器 public class ChatServer {public static void main(String[] args) throws Exception {EventLoopGroup boosGroup new NioEventLoopGroup(1);EventLoopGroup workerGroup new NioEventLoopGroup();ServerBootstrap serverBootstrap new ServerBootstrap();//配置参数serverBootstrap.group(boosGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,1024).childHandler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel ch) throws Exception {//获得piplelineChannelPipeline pipeline ch.pipeline();//添加handlerpipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());//添加业务处理handlerpipeline.addLast(new ChatServerHandler());}});System.out.println(聊天室启动了...);ChannelFuture channelFuture serverBootstrap.bind(9090).sync();channelFuture.channel().closeFuture().sync();boosGroup.shutdownGracefully();workerGroup.shutdownGracefully();} }服务端业务代码 public class ChatServerHandler extends SimpleChannelInboundHandlerString{private SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//存放Channel的容器而且还可以执行对每个channel执行的任务private static ChannelGroup channelGroup new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);//有客户端上线了//有新的客户端连接了将该客户端的上线信息广播给其它所有客户端Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {//得到客户端的channelChannel channel ctx.channel();String message 客户端-channel.remoteAddress()于sdf.format(new Date())上线了\n;//得到其它客户端的channel向其它客户端发送该客户端的channelchannelGroup.writeAndFlush(message);//加入到channelGroup中channelGroup.add(channel);}/** 客户端下线则广播给其它客户端*/Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {Channel channel ctx.channel();//生成一个下线的信息String message 客户端-channel.remoteAddress()于sdf.format(new Date())下线了\n;//广播给其它客户端channelGroup.writeAndFlush(message);}/**具体读数据的业务 */Overrideprotected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {//获得当前发消息的客户端channelChannel channel ctx.channel();//遍历所有的channelchannelGroup.forEach(ch-{if(channel!ch){ch.writeAndFlush(客户端-channel.remoteAddress()于sdf.format(new Date())说:msg\n);}else{ch.writeAndFlush(我于sdf.format(new Date())说:msg\n);}});}Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {super.exceptionCaught(ctx, cause);} }客户端基本代码 public class ChatClient {public static void main(String[] args) throws Exception {EventLoopGroup eventLoopGroup new NioEventLoopGroup();Bootstrap bootstrap new Bootstrap();bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline ch.pipeline();pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());pipeline.addLast(new ChatClientHandler());}});//发送消息ChannelFuture channelFuture bootstrap.connect(127.0.0.1, 9090).sync();Channel channel channelFuture.channel();System.out.println(欢迎进入Yc聊天室);Scanner scanner new Scanner(System.in);while (scanner.hasNextLine()){String message scanner.nextLine();channel.writeAndFlush(message);}eventLoopGroup.shutdownGracefully();} }客户端业务代码 public class ChatClientHandler extends SimpleChannelInboundHandlerString {//打印在控制台Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {System.out.println(s);} }粘包和拆包 TCP协议特点 作为一个流式传输协议数据在TCP中传输是没有边界的。也就是说客户端发送的多条数据有可能会被认为是一条数据。或者客户端发送的一条数据有可能会被分成多条数据。这是由于TCP协议并不了解上层业务数据的具体含义在使用TCP协议传输数据时是根据TCP缓冲区的实际情况进行数据包的划分。 举个例子 我们要发两句话 我是杨 他是李 可能别人收到的信息就是我是杨他是李一条数据也可能收到我是 杨他是李这两句话 假设我们这有个客户端 发送两百次消息 就可能得到这样的结果 粘包缓冲区还可以放的下 拆包缓冲区不可以放的下乱码发生的原因是因为一个字的字节放在不同缓冲区内发送
http://www.zqtcl.cn/news/186077/

相关文章:

  • 网站域名费用怎么做分录销售crm客户管理系统
  • 海南住房与城乡建设网站大连做网站团队
  • 邯郸最穷的三个县长春纯手工seo
  • 昌黎网站建设贵德县建设局网站
  • 山西网站制作公司兼职做网站安全么
  • 阿里做网站怎么做青岛网站维护
  • 怎么建网站手机版郑州网站建设哪家好
  • 做企业网站有哪些好处安龙网站建设
  • 怎做连接网站wordpress iis设置方法
  • ugc网站开发网站设计常见流程
  • dz论坛可以做招聘网站国内空间没备案可以打开网站吗
  • 建设用地规划证查询网站公司起名字大全免费好听
  • 杭州网站建设公司有哪些瑞诺国际的数字营销模式
  • 宣城网站建设 有限公司高州做网站
  • 做外贸最适合的网站系统有可以做国外支付系统的网站吗
  • 建设执业资格注册中心网站办事大厅ui设计素材库
  • 个人网站免费建站4399电脑版网页链接
  • 重庆开县网站建设公司推荐网站建设与维护高职
  • 关于网站开发的技术博客海口网站设计建设
  • xx市院门户网站建设方案做视频特技的网站
  • 肇庆seo公司咨询23火星seo 网站
  • 天元建设集团有限公司破产新手seo网站做什么类型好
  • spa.net网站开发二次开发需要什么
  • 如何做网站静态页面商丘网签查询
  • 网站建设好学么模版型网站是怎样的
  • 网站维护建设费应计入科目高端营销型网站制作
  • 推荐几个好的网站wordpress 加载数据库表格也卖弄
  • 承德网站开发找人做网站安全吗
  • 百度网站推广电话眼镜网站怎么做竞价
  • 邢台建设银行官方网站为什么建设网站很多公司没有