Springboot NettyClient NettyServer Demo
时间: 2023-06-20 08:09:52 浏览: 171
这里是一个简单的 Spring Boot + Netty Client + Netty Server 的示例代码:
首先,创建一个 Netty Server 类,用于接收客户端连接并处理消息:
```java
@Component
public class NettyServer {
private static final Logger logger = LoggerFactory.getLogger(NettyServer.class);
@Value("${netty.server.port}")
private Integer port;
private ChannelFuture serverChannelFuture;
@Autowired
private NettyServerInitializer nettyServerInitializer;
@PostConstruct
public void start() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(nettyServerInitializer)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
serverChannelFuture = bootstrap.bind(port).sync();
logger.info("Netty Server started on port {}.", port);
serverChannelFuture.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
@PreDestroy
public void stop() {
serverChannelFuture.channel().close();
}
}
```
其中,`NettyServerInitializer` 类是一个 `ChannelInitializer` 的实现,用于初始化服务器的 ChannelPipeline,这里就不贴出来了。
接下来,创建一个 Netty Client 类,用于向服务器发送消息,并接收服务器的响应:
```java
@Component
public class NettyClient {
private static final Logger logger = LoggerFactory.getLogger(NettyClient.class);
@Value("${netty.server.host}")
private String host;
@Value("${netty.server.port}")
private Integer port;
private ChannelFuture clientChannelFuture;
@Autowired
private NettyClientInitializer nettyClientInitializer;
public void start() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(nettyClientInitializer);
clientChannelFuture = bootstrap.connect(host, port).sync();
logger.info("Netty Client connected to {}:{}", host, port);
clientChannelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
public void sendMessage(String message) {
if (clientChannelFuture != null && clientChannelFuture.channel().isActive()) {
clientChannelFuture.channel().writeAndFlush(message);
logger.info("Sent message to server: {}", message);
} else {
logger.error("Netty Client is not connected to the server.");
}
}
}
```
同样地,`NettyClientInitializer` 类也是一个 `ChannelInitializer` 的实现,用于初始化客户端的 ChannelPipeline。
最后,我们可以在 Spring Boot 应用中使用这两个组件:
```java
@SpringBootApplication
public class NettyDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NettyDemoApplication.class, args);
}
@Autowired
private NettyClient nettyClient;
@Scheduled(fixedDelay = 1000)
public void sendMessageToServer() {
nettyClient.sendMessage("Hello, Netty Server!");
}
}
```
这里使用了 Spring Boot 的 `@Autowired` 和 `@Scheduled` 注解,每隔一秒钟向服务器发送一条消息。
以上就是一个简单的 Spring Boot + Netty Client + Netty Server 的示例代码,你可以根据自己的需要进行修改和扩展。
阅读全文