網站首頁 編程語言 正文
- 項目使用feign進行模塊間rpc調用解耦
- RPC(Remote Procedure Call)遠程過程調用,簡單的理解是一個節點請求另一個節點提供的服務,調用本地接口一樣調用遠程接口的方式,就是RPC
- 而Feign是Spring Cloud全家桶中推薦使用的RPC框架,使用了HTTP作為傳輸層協議,底層封裝的是Spring RestTemplate。
- 是Netflix開發的聲明式、模板化的HTTP客戶端, Feign可以幫助我們更快捷、優雅地調用HTTP API。
調用方通過創建一個Feign提供者接口(不需要實現),url+requestMapping指向http接口
V2通用接口主要增加了 對象傳參的支持,
調用鏈:
CommonFeignService -->ConsumerController–>SpringBeanInvoker–>SpringBeanInvokerImpl
如用戶新增接口
interface UserService{
/**
* 新增數據
* 2020-09-01
* @param user
* @return
* @author yuhui
*/
int addUser(User user);
}
新版本feign接口調用
class SsoLoginServiceImpl{
/**
*部門單點登錄
*部分實現
*/
public String deptSsoLogin(String authorization, String uid, String userAccount, String userName, String deptName, String deptCode) throws UnknownHostException {
//創建用戶對象。
JSONObject user = new JSONObject();
user.put("userAccount", userAccount);
String password = Md5Crypt.md5Crypt("Qwer123$".getBytes());
user.put("password", password);
user.put("userName", userName);
user.put("enabled", "1");
user.put("userType", "0");
user.put("userDesc", "通過單點登錄介入");
//支持對象json傳參。
request.setArgs(new Object[]{JSONObject.toJSONString(user)});
String res = iCommonFeignService.autoJson2BeanInvoke(request);
}
}
SpringBeanInvokerImpl核心代碼
class SpringBeanInvokerImpl{
/**
* 1 根據入參參數類型與方法名直接查找對應類的方法. 匹配成功,則返回結果<br>
* 2 若1失敗, 則遍歷目標類方法,找到與入參方法名匹配的目標方法名. 若參數數量與接口參數要求數量一致. 則3 <br>
* 3 進行參數類型判斷匹配. 判斷接口入參類型, 嘗試json轉換, 若匹配成功,則返回結果,跳出循環.<br>
* 本方法適應重載方法.即同名不同參的方法.<br>
* 2019年9月5日上午10:05:13
*
* @param springBean
* @param methodName
* @param args
* @return
* @author whx
*/
private Method getBeanMethodByJsonParams(Object springBean, String methodName, Object... args) {
Method method = null;
//根據參數類型精確匹配.
Class<?>[] parameterTypes = null;
if (args != null && args.length > 0) {
parameterTypes = new Class<?>[args.length];
for (int i = 0; i < args.length; i++) {
parameterTypes[i] = args[i].getClass();
}
}
try {
method = springBean.getClass().getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
log.info("精確參數類型查找方法失敗,使用遍歷方法進行查找,并判斷入參類型是否與原始方法參數類型一致或入參類型是原始方法參數的子類/實現.");
Method[] methods = springBean.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
Method subMethod = methods[i];
if (subMethod.getName().equals(methodName)) {
Class<?>[] methodParamTypes = subMethod.getParameterTypes();
if (methodParamTypes == null || methodParamTypes.length <= 0) {
method = subMethod;
} else {
//如果參數類型長度不匹配.則直接返回
if (parameterTypes == null || (methodParamTypes.length != parameterTypes.length)) {
method = null;
} else {
//自動參數轉換
boolean paramMatch = true;
try {
Object[] params = parameterTypesCheck(methodParamTypes, args, false);
if (params.length == args.length) {
paramMatch = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
if (paramMatch) {
method = subMethod;
break;
}
}
}
}
}
} catch (SecurityException e) {
e.printStackTrace();
}
return method;
}
}
原文鏈接:https://blog.csdn.net/YSOLA4/article/details/115105512
相關推薦
- 2022-07-07 C語言數組快速入門詳細講解_C 語言
- 2022-08-13 Redis 性能影響 - 內存碎片和緩沖區
- 2022-09-15 c語言實現數組循環左移m位_C 語言
- 2022-06-10 C語言?模擬實現memcpy與memmove函數詳解_C 語言
- 2022-11-15 自定義?Github?Action?庫實戰詳解_相關技巧
- 2024-01-07 SpringData Jpa 之 修改、刪除數據
- 2022-11-10 Ant?Design中使用css切換的問題及解決_React
- 2023-02-09 c++報錯問題解決方案lvalue?required?as?left?operand?of?assi
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支