Netty第一个入门实例-TCP服务

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

目的:对Netty 线程模型 有一个初步认识, 便于理解Netty 模型理论

2.创建maven项目


通过eclipse或者IDEA创建一个普通的maven项目即可

3.导入依赖


引入相关的maven坐标

io.netty

netty-all

4.1.20.Final

4.创建服务端Handler


创建服务端的handler,主要是处理客户端提交的请求的。

package com.dpb.netty.tcp;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

import io.netty.util.CharsetUtil;

/**

  • @program: netty4demo

  • @description:

  • @author: 波波烤鸭

  • @create: 2019-12-23 11:24

*/

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

/**

  • 读取客户端发送来的数据

  • @param ctx ChannelHandler的上下文对象 有管道 pipeline 通道 channel 和 请求地址 等信息

  • @param msg 客户端发送的具体数据

  • @throws Exception

*/

@Override

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

System.out.println(“客户端请求到了…” + ctx.channel().remoteAddress());

ByteBuf buf = (ByteBuf) msg;

System.out.println(“客户端发送的数据是:” +buf.toString(CharsetUtil.UTF_8));

}

/**

  • 读取客户端发送数据完成后的方法

  • 在本方法中可以发送返回的数据

  • @param ctx

  • @throws Exception

*/

@Override

public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

// writeAndFlush 是组合方法

ctx.writeAndFlush(Unpooled.copiedBuffer(“你好啊,客户端…_”,CharsetUtil.UTF_8));

}

}

5.创建服务端


创建服务端

package com.dpb.netty.simple;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioServerSocketChannel;

/**

  • @program: netty4demo

  • @description:

  • @author: 波波烤鸭

  • @create: 2019-12-23 11:15

*/

public class NettyServerDemo {

public static void main(String[] args) {

// 创建对应的 线程池

// 创建Boss group

EventLoopGroup boosGroup = new NioEventLoopGroup(1);

// 创建 workgroup

EventLoopGroup workGroup = new NioEventLoopGroup();

// 创建对应的启动类

ServerBootstrap bootstrap = new ServerBootstrap();

try{

// 设置相关的配置信息

bootstrap.group(boosGroup,workGroup) // 设置对应的线程组

.channel(NioServerSocketChannel.class) // 设置对应的通道

.option(ChannelOption.SO_BACKLOG,1024) // 设置线程的连接个数

.childHandler(new ChannelInitializer() { // 设置

/**

  • 给pipeline 设置处理器

  • @param socketChannel

  • @throws Exception

*/

@Override

protected void initChannel(SocketChannel socketChannel) throws Exception {

socketChannel.pipeline().addLast(new NettyServerHandler());

}

});

System.out.println(“服务启动了…”);

// 绑定端口 启动服务

ChannelFuture channelFuture = bootstrap.bind(6668).sync();

// 对关闭通道进行监听

channelFuture.channel().closeFuture().sync();

}catch (Exception e){

}finally {

// 优雅停服

boosGroup.shutdownGracefully();

workGroup.shutdownGracefully();

}

}

}

6.创建客户端Handler


客户端handler主要是发送请求给客户端及处理服务端返回的信息。

package com.dpb.netty.simple;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

import io.netty.util.CharsetUtil;

/**

  • @program: netty4demo

  • @description:

  • @author: 波波烤鸭

  • @create: 2019-12-23 11:36

*/

public class NettyClientHandler extends ChannelInboundHandlerAdapter {

/**

  • 连接上服务的回调方法

  • @param ctx

  • @throws Exception

*/

@Override

public void channelActive(ChannelHandlerContext ctx) throws Exception {

// 发送数据

System.out.println(“连接上了 服务器…”);

ctx.writeAndFlush(Unpooled.copiedBuffer(“哈哈 你好呀!!!”, CharsetUtil.UTF_8));

}

/**

  • 读取服务端返回的信息

  • @param ctx

  • @param msg

  • @throws Exception

*/

@Override

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

ByteBuf buf = (ByteBuf) msg;

System.out.println(“服务端返回的信息:” + buf.toString(CharsetUtil.UTF_8));

}

}

最后

分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。

这些面试题相对应的技术点:

  • JVM
  • MySQL
  • Mybatis
  • MongoDB
  • Redis
  • Spring
  • Spring boot
  • Spring cloud
  • Kafka
  • RabbitMQ
  • Nginx

大类就是:

  • Java基础
  • 数据结构与算法
  • 并发编程
  • 数据库
  • 设计模式
  • 微服务
  • 消息中间件

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
1714641956564)]

[外链图片转存中…(img-Ospl2z7P-1714641956564)]

[外链图片转存中…(img-7UYQJ4mY-1714641956565)]

[外链图片转存中…(img-JFheYIHW-1714641956565)]

[外链图片转存中…(img-qV7hVIic-1714641956565)]

[外链图片转存中…(img-qWLbK5OU-1714641956565)]

[外链图片转存中…(img-Q1MHqHy6-1714641956566)]

[外链图片转存中…(img-bD29wJby-1714641956566)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 15
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值