All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.houbb.config.socket.client.handler.ConfigClientHandler Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
 * Copyright (c)  2019. houbinbin Inc.
 * rpc All rights reserved.
 */

package com.github.houbb.config.socket.client.handler;

import com.alibaba.fastjson.JSON;
import com.github.houbb.config.socket.client.support.invoke.InvokeService;
import com.github.houbb.config.socket.client.support.listener.ConfigNotifyListener;
import com.github.houbb.config.socket.common.constant.ServiceNameConst;
import com.github.houbb.config.socket.common.req.ConfigNotifyReq;
import com.github.houbb.config.socket.common.rpc.RpcMessageDto;
import com.github.houbb.heaven.util.lang.StringUtil;
import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 

客户端处理类

* * TODO: ChannelInboundHandlerAdapter 学习一下对应的方法,然后处理各种场景。 * *
 Created: 2019/10/16 11:30 下午  
*
 Project: rpc  
* * @author houbinbin * @since 0.0.2 */ public class ConfigClientHandler extends SimpleChannelInboundHandler { private static final Log log = LogFactory.getLog(ConfigClientHandler.class); private final InvokeService invokeService; private final ConfigNotifyListener listener; public ConfigClientHandler(InvokeService invokeService, ConfigNotifyListener listener) { this.invokeService = invokeService; this.listener = listener; } @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf)msg; byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); RpcMessageDto rpcMessageDto = JSON.parseObject(bytes, RpcMessageDto.class); final String methodType = rpcMessageDto.getMethodType(); if(rpcMessageDto.isRequest()) { // 请求类接口,根据 serviceName 做路由 if(ServiceNameConst.NOTIFY.equals(methodType)) { ConfigNotifyReq notifyReq = JSON.parseObject(rpcMessageDto.getJson(), ConfigNotifyReq.class); listener.configNotify(notifyReq); } else { log.warn("不支持的方法类型 {}", methodType); } } else { // 丢弃掉 traceId 为空的信息 if(StringUtil.isBlank(rpcMessageDto.getTraceId())) { log.info("[Server] response traceId 为空,直接丢弃", JSON.toJSON(rpcMessageDto)); return; } invokeService.addResponse(rpcMessageDto.getTraceId(), rpcMessageDto); log.info("[Client] response is :{}", JSON.toJSON(rpcMessageDto)); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 每一次如果都关闭,会导致长链接丢失。 // 最简单的方法是保持不关闭。 // 同时把服务端,当做注册中心。当然,也可以单独写一个注册中心。 log.error("[Rpc Server] meet ex", cause); ctx.close(); } // @Override // public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { // // 每次用完要关闭,不然拿不到response,我也不知道为啥(目测得了解netty才行) // // 个人理解:如果不关闭,则永远会被阻塞。 // ctx.flush(); // ctx.close(); // // final String id = ctx.channel().id().asLongText(); // log.info("{} channelReadComplete", id); // } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy