008 netty实践_多客户端连接服务器通信

        实际生产中多客户端连接服务器通信是常用应用,多客户端连接一个服务器端,多客户端连接多个服务器;

package com.cc.netty.best.multi.connection;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
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.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class Server {

	public static void main(String[] args) {
		//服务类
		ServerBootstrap bootstrap = new ServerBootstrap();
		
		//boss和worker
		EventLoopGroup boss = new NioEventLoopGroup();
		EventLoopGroup worker = new NioEventLoopGroup();
		try {
			//设置线程池
			bootstrap.group(boss, worker);
			//设置socket工厂
			bootstrap.channel(NioServerSocketChannel.class);
			
			bootstrap.childHandler(new ChannelInitializer<Channel>() {	//设置管道工厂
				@Override
				protected void initChannel(Channel ch) throws Exception {
					ch.pipeline().addLast(new StringDecoder());
					ch.pipeline().addLast(new StringEncoder());
					ch.pipeline().addLast(new ServerHandler());
				}
			});
			
			//设置参数,TCP参数
			bootstrap.option(ChannelOption.SO_BACKLOG, 2048);//serverSocketchannel的设置,链接缓冲池的大小
			bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);//socketchannel的设置,维持链接的活跃,清除死链接
			bootstrap.childOption(ChannelOption.TCP_NODELAY, true);//socketchannel的设置,关闭延迟发送
			//绑定端口
			ChannelFuture future = bootstrap.bind(8765);
			System.out.println("server-8765:start");
			
			//等待服务端关闭[阻塞]
			future.channel().closeFuture().sync();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			//释放资源
			boss.shutdownGracefully();
			worker.shutdownGracefully();
		}
	}
}
package com.cc.netty.best.multi.connection;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ServerHandler extends SimpleChannelInboundHandler<String> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		System.out.println(ctx.channel()+":"+msg);
		//ctx.channel().writeAndFlush("hi");
		ctx.writeAndFlush(ctx.channel()+"server-8765:hi");
		
	}

	/**
	 * 新客户端接入
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("channelActive");
	}

	/**
	 * 客户端断开
	 */
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("channelInactive");
	}

	/**
	 * 异常
	 */
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
	}


	
	
}
package com.cc.netty.best.multi.connection;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;


public class MultiClent {

	/* 服务类  */
	private Bootstrap bootstrap = new Bootstrap();
	
	private final AtomicInteger index = new AtomicInteger(0); 
	
	/* 会话 将Channel封装到 ArrayList中实现池化 */
	private List<Channel> channels = new ArrayList<Channel>();
	
	/**
	 * <B>方法名称:</B>初始化方法<BR>
	 * <B>概要说明:</B>初始化链接<BR>
	 * @param count 链接数
	 */
	public void init(int count){
	
		//worker:
		EventLoopGroup worker = new NioEventLoopGroup();
		//设置线程池
		this.bootstrap.group(worker);
		//设置socket工厂
		this.bootstrap.channel(NioSocketChannel.class);
		//设置管道
		this.bootstrap.handler(new ChannelInitializer<Channel>() {
			@Override
			protected void initChannel(Channel channel) throws Exception {
				channel.pipeline().addLast(new StringDecoder());
				channel.pipeline().addLast(new StringEncoder());
				channel.pipeline().addLast(new ClientHandler());
			}
		});
		/* 根据传入count值创建服务器连接,把连接封装进channels实现池化*/
		for (int i = 1; i <= count; i++) {
			ChannelFuture future = this.bootstrap.connect("127.0.0.1", 8765);
			channels.add(future.channel());
		}
	}
	/** -获取指定Channel
	 *  -应用AtomicInteger(原子类)的getAndIncrement获取并自增,
	 *  -同channels的大小进行取余,进行Math.abs取绝对值
	 * @param count
	 * @return
	 */
	public Channel getNextChannel(int count){
		Channel channel = channels.get(Math.abs(index.getAndIncrement() % channels.size()));
		
		if(!channel.isActive()){
			//如果非活跃,重新连接
			reconnect(channel);
			
			if(count >= channels.size()){
				throw new RuntimeException("no use channel !");
			}
			return getNextChannel(count + 1);
		}
		return channel;
	}
	
	public void reconnect(Channel channel){
		ReentrantLock reentrantLock = new ReentrantLock();
		try {
			/** -加锁
			 *  -新旧替换过程中必须保证操作唯一性(必须考虑并发)
			 */
			reentrantLock.lock();
			if(channels.indexOf(channel) == -1){
				return ;
			}
			Channel newChannel = this.bootstrap.connect("127.0.0.1", 8765).channel();
			//用新创建的channal替换旧的channal
			channels.set(channels.indexOf(channel), newChannel);
		} finally {
			reentrantLock.unlock();
		}
	}
	
	
}
package com.cc.netty.best.multi.connection;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
 * 	客户端消息处理
 *
 */
public class ClientHandler extends SimpleChannelInboundHandler<String> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		System.out.println("客户端收到消息:" + msg);
		
	}

}
package com.cc.netty.best.multi.connection;

import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
 * 启动类
 *
 */
public class Start {

	public static void main(String[] args) {

		MultiClent client = new MultiClent();
		client.init(5);//创建5个客户端,连接5个channal,向服务器发送请求
		
		//输入数据发起通信
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		while(true){
			try {
				System.out.println("请输入:");
				String msg = bufferedReader.readLine();
				//给定初始值为0,实际生产动态传入
				client.getNextChannel(0).writeAndFlush(msg);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开源的Java web开发框架,而Netty是一个异步事件驱动的网络应用框架。一般情况下,Netty通常用于创建高性能的网络服务器客户端。 如果要在Spring Boot中使用Netty服务器进行定时通知客户端,可以通过以下步骤实现: 1. 创建一个Netty服务器:首先,在Spring Boot项目中添加Netty的依赖项,然后编写一个类来创建Netty服务器。这个类需要实现ChannelHandler接口,并重写channelRead方法来处理接收到的消息。 2. 设置定时通知:在Netty服务器初始化完毕后,可以使用Java的定时任务来实现定时通知功能。可以使用ScheduledExecutorService类来定时执行指定的代码块。在代码块中,调用Netty服务器的write方法发送通知消息给客户端。 3. 客户端接收通知:客户端需要定义一个Netty客户端,并通过连接Netty服务器来接收通知消息。客户端需要实现ChannelHandler接口,并重写channelRead方法来处理接收到的通知消息。 4. 启动Spring Boot应用:在完成以上步骤后,可以启动Spring Boot应用,并同时启动Netty服务器客户端。当定时任务触发时,服务器会发送通知消息给客户端。 需要注意的是,以上步骤只是大致的实现过程,具体的代码实现需要根据项目的需求进行适当的调整。同时,要确保服务器客户端之间的通信方式和数据格式是一致的,以确保通信的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值