mPaas-RPC拦截器各种场景下的使用指南
1. 背景
金融級移動開發平臺 mPaaS[1](Mobile PaaS)為 App 開發、測試、運營及運維提供云到端的一站式解決方案,能有效降低技術門檻、減少研發成本、提升開發效率,協助企業快速搭建穩定高質量的移動應用。其中移動網關服務(Mobile Gateway Service,簡稱 MGS)作為mPaas最重要的組件之一,連接了移動客戶端與服務端,簡化了移動端與服務端的數據協議和通訊協議,從而能夠顯著提升開發效率和網絡通訊效率。在我們日常運維過程中發現,很多用戶在使用客戶端RPC組件的時候,有很多不同場景的訴求,比如攔截請求添加業務請求標記,免登,返回結果模擬,異常處理,限流等。本文旨在介紹通過利用RPC提供的攔截器機制,通過不同實際場景的描述,供業務參考使用。
2. RPC調用原理
當 App 在移動網關控制臺接入后臺服務后,調用RPC的示例代碼如下:
RpcDemoClient client = MPRpc.getRpcProxy(RpcDemoClient.class);
// 設置請求
GetIdGetReq req = new GetIdGetReq();
req.id = "123";
req.age = 14;
req.isMale = true;
// 發起 rpc 請求
String response = client.getIdGet(req);
值得好奇的是,整個調用過程中其實我們并沒有去實現 RpcDemoClient 這個接口,而是通過 MPRpc.getRpcProxy 獲取了一個代理,通過代理對象完成了調用。在這里其實主要使用了 Java 動態代理的技術。當調用RPC接口的時候,會通過動態代理的RpcInvocationHandler,回調其實現的invoke方法,最終在invoke內實現數據的序列化處理最后通過網絡庫發到服務端。
public <T> T getRpcProxy(Class<T> clazz) {
LogCatUtil.info("RpcFactory", "clazz=[" + clazz.getName() + "]");
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new RpcInvocationHandler(this.mConfig, clazz, this.mRpcInvoker));
}
在業務開發中,如果在某些情況下需要控制客戶端的網絡請求(攔截網絡請求,禁止訪問某些接口,或者限流),可以通過 RPC 攔截器實現。
RpcService rpcService =
getMicroApplicationContext().findServiceByInterface(RpcService.class.getName());
rpcService.addRpcInterceptor(OperationType.class, new CommonInterceptor());
3. 攔截器
RPC目前采用了攔截器機制實現RPC的自定義處理,如下圖所示,業務可以通過設置自定義RpcIntercept實現在請求前,請求異常,請求返回三個階段對RPC的定制處理。
圖1:rpc攔截器調用示意圖
4. preHandle場景
典型使用場景:業務添加自定義的業務全局標識或者其他統計字段
@Overridepublic boolean preHandle(Object proxy,
ThreadLocal<Object> retValue,byte[] retRawValue,
Class<?> aClass,
Method method,
Object[] args,
Annotation annotation,
ThreadLocal<Map<String, Object>> threadLocal)throws RpcException {//Do something...
RpcInvocationHandler handler = (RpcInvocationHandler) Proxy.getInvocationHandler(proxy);
handler.getRpcInvokeContext().addRequestHeader("header", "headerCustom");return true;}
典型使用場景:比如如果當前未登錄,對需要登錄的rpc先阻斷,統一提示登錄
@Overridepublic boolean preHandle(Object proxy,
ThreadLocal<Object> retValue,byte[] retRawValue,
Class<?> aClass,
Method method,
Object[] args,
Annotation annotation,
ThreadLocal<Map<String, Object>> threadLocal)throws RpcException {//Do something...
String operationType = getOperationType(aClass, method, args);if ("operationType1".equals(operationType)) {boolean isLogin = false;if (!isLogin) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {@Overridepublic void run() {
Toast.makeText(LauncherApplicationAgent.getInstance().getApplicationContext()," 當前未登錄,請登錄", Toast.LENGTH_SHORT).show();}});// 返回給上層調用登錄失敗的異常,上層做業務處理throw new RpcException(RpcException.ErrorCode.CLIENT_LOGIN_FAIL_ERROR, "login fail.");}}return true;}private String getOperationType(Class<?> aClass, Method method, Object[] args) {if (aClass == null || null == method) return "";
OperationType operationType = method.getAnnotation(OperationType.class);return operationType == null ? "" : operationType.value();}
5. postHandle場景
1. 攔截接口返回
典型使用場景:全局修改服務端的返回結果,比如mock服務端的數據
@Overridepublic boolean postHandle(Object proxy,
ThreadLocal<Object> threadLocal,byte[] retRawValue,
Class<?> aClass,
Method method,
Object[] args,
Annotation annotation) throws RpcException {//Do something...// 場景:修改服務端返回的數據,比如mock數據,或者修改服務端數據
String operationType = getOperationType(aClass, method, args);
LoggerFactory.getTraceLogger().debug(TAG, "postHandle:" + operationType);if ("operationType1".equals(operationType)) {
String value = JSON.parse(retRawValue).toString();
LoggerFactory.getTraceLogger().debug(TAG, "postHandle 原始返回" + value);
String mockData = "{\"img\":\"imgPath\",\"User\":{\"name\":\"我是mock的數據\",\"age\":18}}";
Object mockObj = JSON.parseObject(mockData, method.getReturnType());
threadLocal.set(mockObj);return true;}return true;}private String getOperationType(Class<?> aClass, Method method, Object[] args) {if (aClass == null || null == method) return "";
OperationType operationType = method.getAnnotation(OperationType.class);return operationType == null ? "" : operationType.value();}
6. exceptionHandle場景
1. 異常統一處理
比如登錄態失效,服務端會統一返回2000的錯誤碼,客戶端可以在exceptionHandle里統一攔截進行登錄態免登操作
@Overridepublic boolean exceptionHandle(Object proxy, ThreadLocal<Object> retValue, byte[] bytes, Class<?> aClass, Method method, Object[] objects,
RpcException rpcException, Annotation annotation) throws RpcException {
String operationType = getOperationType(aClass, method, objects);if (RpcException.ErrorCode.CLIENT_LOGIN_FAIL_ERROR == rpcException.getCode()&&"operationType1".equals(operationType)) {// 1. 去免登
hasLogin = true;// 2. 免登后在幫上層重發請求,免登操作對上層業務無感知try {
LoggerFactory.getTraceLogger().debug(TAG, "exceptionHandle. Start resend rpc begin " + operationType);// 重發請求
Object object = method.invoke(proxy, objects);
retValue.set(object);
LoggerFactory.getTraceLogger().debug(TAG, "exceptionHandle. Start resend rpc success");return false;} catch (Throwable e) {
LoggerFactory.getTraceLogger().error(TAG, "resend rpc occurs illegal argument exception", e);throw new RpcException(RpcException.ErrorCode.CLIENT_HANDLE_ERROR, e + "");}}return true;}
7. H5場景
由于H5場景中使用的jsapi的rpc,需要支持在js環境里傳遞到native環境,所以在設計上,是統一通過operationType: alipay.client.executerpc 接口進行的轉發,所以針對H5發送的RPC請求,需要做特殊判斷,通過入參拿到真實的operationType接口,示例代碼如下。
1. 獲取H5請求的接口名稱和入參
var params = [{"_requestBody":{"userName":"", "userId":0}}]var operationType = 'alipay.mobile.ic.dispatch'
AlipayJSBridge.call('rpc', {
operationType: operationType,
requestData: params,
headers:{}}, function (result) {
console.log(result);});
業務通過jsapi去請求rpc,如何獲取jsapi請求的rpc名稱,可以參考代碼如下
@Overridepublic boolean preHandle(Object o, ThreadLocal<Object> threadLocal, byte[] bytes, Class<?> aClass, Method method, Object[] objects, Annotation annotation, ThreadLocal<Map<String, Object>> threadLocal1) throws RpcException {
String operationType = getOperationType(aClass, method, objects);if ("alipay.client.executerpc".equals(operationType)) {// H5的rpc名稱
String rpcName = (String) objects[0];// 入參
String req = (String) objects[1];
LoggerFactory.getTraceLogger().debug(TAG, "operationType:" + rpcName + " " + req);} else {// Native的rpc}
LoggerFactory.getTraceLogger().debug(TAG, "operationType:" + operationType);return true;}
參考文檔
[1] mPaaS平臺:https://www.aliyun.com/product/mobilepaas/mpaas
我們是阿里云智能全球技術服務-SRE團隊,我們致力成為一個以技術為基礎、面向服務、保障業務系統高可用的工程師團隊;提供專業、體系化的SRE服務,幫助廣大客戶更好地使用云、基于云構建更加穩定可靠的業務系統,提升業務穩定性。我們期望能夠分享更多幫助企業客戶上云、用好云,讓客戶云上業務運行更加穩定可靠的技術。
原文鏈接:https://developer.aliyun.com/article/781269?
版權聲明:本文內容由阿里云實名注冊用戶自發貢獻,版權歸原作者所有,阿里云開發者社區不擁有其著作權,亦不承擔相應法律責任。具體規則請查看《阿里云開發者社區用戶服務協議》和《阿里云開發者社區知識產權保護指引》。如果您發現本社區中有涉嫌抄襲的內容,填寫侵權投訴表單進行舉報,一經查實,本社區將立刻刪除涉嫌侵權內容。總結
以上是生活随笔為你收集整理的mPaas-RPC拦截器各种场景下的使用指南的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何跑通第一个 SQL 作业
- 下一篇: Android Native crash