问题描述
每次client调用server端,均有正常返回,但是server端还会多打出一些异常信息,如下。
org.apache.thrift.transport.TTransportException: null at org.apache.thrift.transport.TIOStreamTransport.read(TIOStreamTransport.java:132) at org.apache.thrift.transport.TTransport.readAll(TTransport.java:86) at org.apache.thrift.protocol.TBinaryProtocol.readAll(TBinaryProtocol.java:425) at org.apache.thrift.protocol.TBinaryProtocol.readI32(TBinaryProtocol.java:321) at org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinaryProtocol.java:225) at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:27) at org.apache.thrift.server.TThreadPoolServer$WorkerProcess.run(TThreadPoolServer.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
问题出现的环境背景及自己尝试过哪些方法
对thrift不熟悉,刚接触,根本不知道哪里去找原因。
相关代码
server端代码
import com.username.service.controllers.ServiceImpl; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.transport.TTransportException; public class StartService implements Runnable{ private static boolean breg = false; public void starWeather() throws TTransportException { TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory(); ServiceImpl handler = new ServiceImpl(); WeatherService.Processor processor= new WeatherService.Processor(handler); TServerTransport serverTransport = new TServerSocket(9000); TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport); serverArgs.processor(processor); serverArgs.protocolFactory(proFactory); TServer server = new TThreadPoolServer(serverArgs); breg = true; server.serve(); } @Override public void run() { try { if (!breg) starWeather(); } catch (TTransportException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) throws TTransportException { StartService server = new StartService(); server.starWeather(); } }
client端代码
package com.xiaohuan.client;
import com.username.rpc.weather_service.WeatherService; import com.username.rpc.weather_service.entity.Weather; import com.username.rpc.weather_service.request.GetCityWeatherRequest; import com.username.rpc.weather_service.response.GetCityWeatherResponse; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.RetryNTimes; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; import java.util.*; import static com.username.client.WeatherConstants.RPCNAME; import static com.username.client.ZkConstants.connectString; public class CallWeatherRPC { public String callWeather(String ip, int port, String city) { String retString = null; try { TTransport transport = new TSocket(ip, port); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); WeatherService.Client client = new WeatherService.Client(protocol); GetCityWeatherRequest request = new GetCityWeatherRequest(); request.setCity(city); GetCityWeatherResponse response = client.get_city_weather(request); if(response.isSuccess()){ List<Weather> weatherList = response.getWeather(); System.out.println(weatherList.get(0).getCity()); retString = weatherList.get(0).getCity() + weatherList.get(0).getTemperature(); }else{ System.out.println(response.getError_message()); } transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } System.out.println("retString:"+retString); return retString; } public static void main(String[] args) { CallWeatherRPC client = new CallWeatherRPC(); client.callWeather("127.0.0.1", 9000, "123"); } }
你期待的结果是什么?实际看到的错误信息又是什么?
虽然不影响功能,但是为什么server会多打印异常信息呢
阅读 5.3k

是thrift的异常捕获代码写错了,没有处理到。
0.12.0版本的TThreadPoolServer.java第315行
} catch (TException tx) { // A.这里的问题。 LOGGER.error("Thrift error occurred during processing of message.", tx); } catch (Exception x) { // We'll usually receive RuntimeException types here // Need to unwrap to ascertain real causing exception before we choose to ignore Throwable realCause = x.getCause(); // Ignore err-logging all transport-level/type exceptions if ((realCause != null && realCause instanceof TTransportException) // B.这里的代码应该放到A处,TTransportException是TException 的子类, // A处已经把异常捕获了,根本不会进入这里,导致打印错误。可以参看0.11.0 // 的代码,捕获到TTransportException异常是不做任何处理的。 || (x instanceof TTransportException)) { if (LOGGER.isDebugEnabled()) { // Write to debug, just in case the exception gets required LOGGER .debug("Received TTransportException during processing of message, ignoring: ", x); } } else { // Log the exception at error level and continue LOGGER.error("Error occurred during processing of message.", x); } }