問題描述
每次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); } }