鍍金池/ 問答/Java  網(wǎng)絡(luò)安全/ netty解析卡住,怎么處理?

netty解析卡住,怎么處理?

就是用java,netty寫的一個游戲服務(wù)器,目前經(jīng)常被爬蟲請求.

目前一夜過后,netty就卡住了,新的請求就會一直卡住,即不在控制臺打印出來,也不響應(yīng)客戶端請求.

有時候我在服務(wù)器的cmd上按個回車,就會又響應(yīng)客戶端請求.但這種方法顯然不能用...

我關(guān)閉用ChannelHandlerContext chc.close();是不是這樣不能斷開連接,導(dǎo)致線程被占滿?

 //分配用于處理業(yè)務(wù)的線程組數(shù)量  

  protected static final int BisGroupSize = 6;

  //每個線程組中線程的數(shù)量  

  protected static final int worGroupSize = 10;

紅框處是我按回車后輸出的,不按回車前客戶端請求卡住.

紅框處是我按回車后輸出的,不按回車前客戶端請求卡住.

    //SocketControl
    public static void  run(int port) throws Exception {  
        ServerBootstrap bootstrap = new ServerBootstrap();  
        bootstrap.group(bossGroup, workerGroup);
        bootstrap.channel(NioServerSocketChannel.class);  
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override  
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();  
                
                // 以("\n")為結(jié)尾分割的 解碼器  
                pipeline.addLast("framer", new DelimiterBasedFrameDecoder(10240, Delimiters.lineDelimiter()));  
                pipeline.addLast("decoder", new MyStringDecoder(Charset.forName("UTF-8")));  
                pipeline.addLast("encoder", new StringEncoder(Charset.forName("UTF-8")));  
                pipeline.addLast(new SocketServerHandler());  
            }  
        });  
        bootstrap.bind(IP,port).sync();  
        log.info("Socket服務(wù)器已啟動完成,于 "+port+" 端口");  
    }  
public class MyStringDecoder extends MessageToMessageDecoder<ByteBuf> {
 
    private final Charset charset;
    
    /**
     * Creates a new instance with the current system character set.
     */
    public MyStringDecoder() {
        this(Charset.defaultCharset());
    }

    /**
     * Creates a new instance with the specified character set.
     */
    public MyStringDecoder(Charset charset) {
        if (charset == null) {
            throw new NullPointerException("charset");
        }
        this.charset = charset;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        if(msg.getByte(0)!='{' || msg.getByte(msg.capacity()-1)!='}'){
            
        }else{
            out.add(msg.toString(charset));
        }
    }
}
public class SocketServerHandler extends SimpleChannelInboundHandler<String> {  
    private static final Logger log = Logger.getLogger("ruanxin");  
  
    @Override  
    public void exceptionCaught(ChannelHandlerContext chc, Throwable cause) throws Exception {  
        log.info(cause.getMessage());
        chc.close();
    }  
  
    @SuppressWarnings("unchecked")
    @Override  
    public void channelRead(ChannelHandlerContext chc, Object data) throws Exception {    
        if(data==null || !(data instanceof String)){
            log.info("不是字符串或數(shù)據(jù)為空");
            chc.close();
            return;
        }
        String dataStr=(String) data;
        log.info("|||" + dataStr+"|||");
        if(!dataStr.startsWith("{") || !dataStr.endsWith("}")){
            log.info("失敗數(shù)據(jù)內(nèi)容:data=" + dataStr);
            chc.close();
            return;
        }
        log.info("Read數(shù)據(jù)內(nèi)容:data=" + data);
        //...
    }
}
回答
編輯回答
終相守

看不出來什么問題啊,加點日志,看一下是不是哪個方法沒跑完。
之前通過redis處理map的更新時,高并發(fā)會有鎖住的情況。后續(xù)把map中更新頻繁的值取出來直接放redis就好了。

不一定要netty來背鍋

2017年1月22日 08:41
編輯回答
祈歡

這個問題解決了....

win10因為cmd開了快速編輯,有時候會卡住,導(dǎo)致java項目一起卡住.按回車后項目繼續(xù)運行,東西就輸出出來了....

cmd執(zhí)行程序時容易卡住

@ccfish 你的方向是對的,可沒有解決問題.

2018年6月5日 03:01