基于Netty实现一个类SpringMVC的轻量级Java服务端Web开发框架

之前学的Netty忘了,现在再捡起来,想着使用Netty来做一个项目,就做了这个简单框架。整个框架都是仿照Spring和SpringMVC来做的,一部分思路参考了Github上另一个项目。[^1]

简介

  • IOC容器:使用@Component注解,将类注册到IOC容器中,使用@Autowired自动注入
  • 自定义路由:使用@Controller,@RequestMapping注解自定义路由
  • 自动参数转换:支持对BeistHttpRequest,BeistHttpResponse(类似于HttpServletRequest)自动赋值,支持将RequestBody中的数据自动转换为JavaBean
  • Restful风格的API:可以使用@PathVariable获取URL中的值
  • 支持Cookie和Session
  • 拦截器:通过实现Interceptor来实现自定义拦截器,需要配置文件中配置Interceptor包的位置,支持使用Order来定义
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用Netty实现一个客户端对三个服务端Java代码示例: ```java import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; 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.NioSocketChannel; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; public class MultiServerClient { private List<Channel> channels = new ArrayList<>(); public void connect(String[] serverList) { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ClientHandler()); } }); for (String server : serverList) { String[] addr = server.split(":"); String host = addr[0]; int port = Integer.parseInt(addr[1]); ChannelFuture f = b.connect(new InetSocketAddress(host, port)).sync(); channels.add(f.channel()); } } catch (Exception e) { e.printStackTrace(); } } public void send(String message) { if (!channels.isEmpty()) { Channel channel = channels.get(0); // 这里选择第一个 Channel 实例发送数据 channel.writeAndFlush(message); } } public void close() { for (Channel channel : channels) { channel.close(); } channels.clear(); } private static class ClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 处理服务端响应的数据 } } public static void main(String[] args) { MultiServerClient client = new MultiServerClient(); String[] serverList = {"localhost:8080", "localhost:8081", "localhost:8082"}; client.connect(serverList); client.send("Hello, world!"); client.close(); } } ``` 在`connect()`方法中,循环遍历服务端列表,对于每个服务端,创建一个`InetSocketAddress`实例,然后调用`Bootstrap`的`connect()`方法连接服务端,并将返回的`ChannelFuture`实例中的`Channel`保存到`channels`列表中。 在`send()`方法中,从`channels`列表中选择一个`Channel`实例,通过`writeAndFlush()`方法发送数据。 在`main()`方法中,创建一个`MultiServerClient`实例,传入服务端列表,连接服务端,发送数据,然后关闭连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值