Result dubbo接口返回类型的设计

 dubbo实现了分布式远程调用的框架,集成了spring配置功能,dubbo要求远程接口的参数和返回值都是可序列化的,Result泛型返回类型可作为接口的统一返回类型:
  • int code,接口调用成功=0,错误码=其他值
  • T object,具体返回值
  • String error,字符串错误码,可选
  • String message,错误消息,可选,error=message可以配置成属性文件
  • Exception exception,异常消息,可选
public class Result implements Serializable {
	/**
	 * 接口调用成功,不需要返回对象
	 */
	public static  Result newSuccess(){
		Result result = new Result<>();
		return result;
	}
	
	/**
	 * 接口调用成功,有返回对象
	 */
	public static  Result newSuccess(T object) {
		Result result = new Result<>();
		result.setObject(object);
		return result;
	}
	
	/**
	 * 接口调用失败,有错误码和描述,没有返回对象
	 */
	public static  Result newFailure(int code, String message){
		Result result = new Result<>();
		result.setCode(code!=0 ? code : -1);
		result.setMessage(message);
		return result;
	}
	
	/**
	 * 接口调用失败,有错误字符串码和描述,没有返回对象
	 */
	public static  Result newFailure(String error, String message){
		Result result = new Result<>();
		result.setCode(-1);
		result.setError(error);
		result.setMessage(message);
		return result;
	}
	
	/**
	 * 转换或复制错误结果
	 */
	public static  Result newFailure(Result failure){
		Result result = new Result<>();
		result.setCode(failure.getCode()!=0 ? failure.getCode() : -1);
		result.setError(failure.getError());
		result.setMessage(failure.getMessage());
		result.setException(failure.getException());
		return result;
	}
	
	/**
	 * 接口调用失败,返回异常信息
	 */
	public static  Result newException(Exception e){
		Result result = new Result<>();
		result.setCode(-1);
		result.setException(e);
		result.setMessage(e.getMessage());
		return result;
	}
	
	private int code;
	private T object;
	private String error;
	private String message;
	private Exception exception;
	
	/** 判断返回结果是否成功 */
	public boolean success() {
		return code == 0;
	}
	/** 判断返回结果是否有结果对象 */
	public boolean hasObject() {
		return code==0 && object!=null;
	}
	/** 判断返回结果是否有异常 */
	public boolean hasException() {
		return exception != null;
	}
	
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public T getObject() {
		return object;
	}
	public void setObject(T object) {
		this.object = object;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Exception getException() {
		return exception;
	}
	public void setException(Exception exception) {
		this.exception = exception;
	}

	public String toString() {
		StringBuilder result = new StringBuilder("Result");
		if(object!=null) result.append("<"+object.getClass().getSimpleName()+">");
		result.append(": {code="+code);
		if(object!=null) result.append(", object="+object);
		if(error!=null) result.append(", error="+error);
		if(message!=null) result.append(", message="+message);
		if(exception!=null) {
			StringWriter stringWriter = new StringWriter();
			exception.printStackTrace(new PrintWriter(stringWriter));
			result.append(", exception="+stringWriter.toString());
		}
		result.append(" }");
		return result.toString();
	}
}




[ 非英文:245, 总字符:3032 ]
评论列表
  • # 0 ccc 2015-09-19 13:16:02

    哪有要求返回值都要序列化,你可以看dubbo test里面的。。。。。。package com.alibaba.dubbo.rpc.proxy;

    /*** TestService*/

    public interface DemoService{void sayHello(String name);

    String echo(String text);
    
    long timestamp();
    
    String getThreadName();
    
    int getSize(String[] strs);
    
    int getSize(Object[] os);
    
    Object invoke(String service, String method) throws Exception;
    
    int stringLength(String str);
    
    Type enumlength(Type... types);
    

    }

    回复
  • # 1 hongwei 2015-10-30 13:42:25

    返回值和接口参数,需要在client和server之间传递,总会有序列化和编码解码。int、String、Enum、Object、void等或许还可以,你试试自定义User对象并且不实现序列化接口(最好再加个不可序列化的字段)。另外这里是封装的通用返回类型,要考虑到:返回值有多种类型,错误码和错误消息,异常返回等。还有一个AopLogger可以对所有的dubbo远程调用做切面,详细地记录下调用和返回结果的日志。

    回复
  • # 2 hongwei 2015-10-30 14:22:46

    dubbo返回结果不序列化会是怎样,https://www.xlongwei.com/detail/15103014

    回复
  

  

你可能感兴趣的:(Result dubbo接口返回类型的设计)