SpringBoot使用Netty替换tomcat框架

本文介绍如何在SpringBoot项目中使用Netty替代传统的Tomcat服务器。通过创建SpringBoot主启动类,定义Netty自定义注解,设置Netty服务端,构建业务处理类,实现了一个Netty服务器来处理HTTP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 1、创建SpringBoot项目-主启动类如下(NettyHandler是自定义注解,稍后会放出)
@SpringBootApplication
@ComponentScan(includeFilters = @ComponentScan.Filter(NettyHandler.class))
public class DemoMainApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(DemoMainApplication.class).web(WebApplicationType.NONE).run(args);
    }
}
  • 2、创建NettyHandler自定义注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface  NettyHandler {

    @AliasFor("path")
    String[] value() default {};

    String name() default "";

    RequestMethod[] method() default {};

    @AliasFor("value")
    String[] path() default {};

}
  • 3、创建Netty服务端
@Configuration
public class NettyServer implements ApplicationListener<ApplicationStartedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(NettyServer.class);


    private int port = 8080;


    @Override
    public void onApplicationEvent(@NonNull ApplicationStartedEvent event) {
        ServerBootstrap bootstrap = new ServerBootstrap();
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ChannelFuture channelFuture = null;
        try {
            bootstrap.group(bossGroup, workerGroup);
            channelFuture =  bootstrap.channel(NioServerSocketChannel.class)
                    .childHandler(new InitServerHandler())
                    .bind(port).syncUninterruptibly().addListener(future -> {
                        String loginfo = "Netty 启动成功!端口=" + port;
                        logger.info(loginfo);
                    });
        }catch (Exception e) {

        } finally {
            channelFuture.channel().closeFuture().addListener(future -> {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            });
        }

    }
}
  • 4、创建 InitServerHandler(HttpServerHandler 为业务处理类)
public class InitServerHandler extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        ch.pipeline().addLast("http-decoder",new HttpRequestDecoder());
        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(Integer.MAX_VALUE));
        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
        ch.pipeline().addLast("http-server", new HttpServerHandler());
    }
}
  • 5、创建业务处理类(setApplicationContext根据业务需求进行改造)
@ChannelHandler.Sharable
@Component
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> implements ApplicationContextAware {


    private static ConcurrentHashMap<String ,HashMap<String, String>> concurrentHashMap = new ConcurrentHashMap<String, HashMap<String, String>>();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
		//TODO 处理业务
  		System.out.println("业务处理");
        ctx.writeAndFlush(request).addListener(ChannelFutureListener.CLOSE);

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        Map<String, Object> handlers = applicationContext.getBeansWithAnnotation(NettyHandler.class);
        for (Map.Entry<String, Object> entry : handlers.entrySet()) {
            Object handler = entry.getValue();
            for (Method method : handler.getClass().getMethods())
            {
                Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
                for (Annotation ann:declaredAnnotations)
                {
                    if(ann.annotationType().getName().equals(NettyHandler.class.getName()))
                    {
                        String[] value = method.getAnnotation(NettyHandler.class).value();
                        if(value != null)
                        {
                            for (String uri:value)
                            {
                                uri = uri.replace("/","");
                                HashMap<String, String> submap = new HashMap<String, String>();
                                submap.put(method.getName(),handler.getClass().getName());
                                concurrentHashMap.put(uri, submap);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.fireExceptionCaught(cause);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值