<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 前言 * 在以往的單工程項目,所有代碼都融合在一起,業務相互調用只需要引入共有的工具類或者對應模塊的service。 * 到了微服務時代,已然不能使用這種方式,我們需要尋找新的解決方案。 * 若每個模塊都把需要調用模塊的service拷貝一份,那會令代碼非常冗余,影響整個工程的健壯性。 * 稍大一些的系統,會分成多個庫,比如用戶庫、訂單庫分開,訂單服務想要取到用戶的相關信息,由于不能 * 連接到用戶的庫,所以無法直接新建數據庫查詢以達到目的。 * 這個時候,遠程調用方案出現,訂單服務只需調用用戶服務的API,就可以獲取所需信息,非常方便。 * 我們下面來看下如何使用SpringCloud的Feign來進行微服務遠程調用。 ## Feign簡介 * Feign是一種聲明式、模板化的HTTP客戶端。在Spring Cloud中使用Feign, 我們可以做到使用HTTP請求遠程服 * 務時能與調用本地方法一樣的編碼體驗,開發者完全感知不到這是遠程方法,更感知不到這是個HTTP請求。 ## 代碼示例 1. 我們以 jpower-system 服務為例,讓我們的 jpower-user 可以調用到 jpower-system 的API 2. 在jpower-system下新建了system-api工程,在該工程中新建package:com.wlcb.jpower.feign ![](https://img.kancloud.cn/f6/78/f6784b158f7888f6ec36bebe86b005de_698x496.png) 3. 在 com.wlcb.jpower.feign 下創建一個接口類,命名為 SystemClient 4. 增加如下代碼,**下面代碼是目前工程中已經實現的API接口** ~~~ /** * @author mr.gmac */ @FeignClient(value = AppConstant.JPOWER_SYSTEM, path = "/core/") public interface SystemClient { /** * @author null * @Description //TODO 查詢部門所有下級部門id * @date 0:42 2020/9/3 0003 * @param id * @return com.wlcb.jpower.module.base.vo.ResponseData<java.lang.String> */ @GetMapping("/org/queryChildById") ResponseData<List<String>> queryChildOrgById(@RequestParam String id); @GetMapping("/org/queryOrgById") ResponseData<TbCoreOrg> queryOrgById(@RequestParam String orgId); @GetMapping("/client/getClientByClientCode") ResponseData<TbCoreClient> getClientByClientCode(@RequestParam String clientCode); @GetMapping("/function/getUrlsByRoleIds") ResponseData<List<Object>> getUrlsByRoleIds(@RequestParam List<String> roleIds); @GetMapping("/tenant/getTenantByCode") ResponseData<TbCoreTenant> getTenantByCode(@RequestParam String tenantCode); @GetMapping("/function/getMenuListByRole") ResponseData<List<TbCoreFunction>> getMenuListByRole(@RequestParam List<String> roleIds); @GetMapping("/dataScope/getAllRoleDataScope") ResponseData<List<TbCoreDataScope>> getAllRoleDataScope(); @GetMapping("/dataScope/getDataScopeByRole") ResponseData<List<TbCoreDataScope>> getDataScopeByRole(@RequestParam List<String> roleIds); } ~~~ 5. 繼續新建`system-api-biz`工程maven繼承 `system-api`,用來實現feign定義的接口,在工程中新建package:com.wlcb.jpower.controller ![](https://img.kancloud.cn/bc/5f/bc5fa449b68106e8d2f3a9faf7945f60_894x390.png) 6. 新建`SystemClientController`類,需要繼承`SystemClient`接口類 ![](https://img.kancloud.cn/f3/82/f3825e02f2f04efb34084289623280cb_778x416.png) 7. 增加如下,因為Feign本質上是HTTP客戶端,所以我們創建的 BlogClientImpl 其實就是 SpringMVC 的 Controller ,所以需要加上 @RestController 注解。 **以下代碼是工程目前已經實現的代碼** ~~~ @ApiIgnore @RestController @RequestMapping("/core") @AllArgsConstructor public class SystemClientController implements SystemClient { private CoreOrgService coreOrgService; private CoreClientService coreClientService; private CoreFunctionService coreFunctionService; private TenantService tenantService; private CoreDataScopeService coreDataScopeService; @Override @GetMapping("/org/queryChildById") public ResponseData<List<String>> queryChildOrgById(@RequestParam String id){ return ReturnJsonUtil.ok("查詢成功",coreOrgService.queryChildById(id)); } @Override @GetMapping("/function/getUrlsByRoleIds") public ResponseData<List<Object>> getUrlsByRoleIds(@RequestParam List<String> roleIds) { return ReturnJsonUtil.ok("查詢成功",coreFunctionService.getUrlsByRoleIds(roleIds)); } @Override @GetMapping("/function/getMenuListByRole") public ResponseData<List<TbCoreFunction>> getMenuListByRole(@RequestParam List<String> roleIds) { return ReturnJsonUtil.ok("查詢成功",coreFunctionService.getMenuListByRole(roleIds)); } @Override @GetMapping("/dataScope/getAllRoleDataScope") public ResponseData<List<TbCoreDataScope>> getAllRoleDataScope() { return ReturnJsonUtil.ok("查詢成功",coreDataScopeService.getAllRoleDataScope()); } @Override @GetMapping("/dataScope/getDataScopeByRole") public ResponseData<List<TbCoreDataScope>> getDataScopeByRole(@RequestParam List<String> roleIds) { return ReturnJsonUtil.ok("查詢成功",coreDataScopeService.getDataScopeByRole(roleIds)); } @Override @GetMapping("/tenant/getTenantByCode") public ResponseData<TbCoreTenant> getTenantByCode(@RequestParam String tenantCode){ return ReturnJsonUtil.ok("查詢成功",tenantService.getOne(Condition.<TbCoreTenant>getQueryWrapper().lambda().eq(TbCoreTenant::getTenantCode,tenantCode))); } @Override @GetMapping("/org/queryOrgById") public ResponseData<TbCoreOrg> queryOrgById(@RequestParam String orgId) { return ReturnJsonUtil.ok("查詢成功",coreOrgService.getById(orgId)); } @Override @GetMapping("/client/getClientByClientCode") public ResponseData<TbCoreClient> getClientByClientCode(@RequestParam String clientCode) { return ReturnJsonUtil.ok("查詢成功",coreClientService.loadClientByClientCode(clientCode)); } } ~~~ 8. 在`jpower-user`下的`user-biz`工程的 pom.xml 文件引入`jpower-system`的API包 ![](https://img.kancloud.cn/a9/20/a9207b5bdca96f42f0693448737cd04d_1412x1532.png) 9. 找到`UserController`,新增如下代碼 ~~~ private SystemClient systemClient; @GetMapping(value = "/orgDeatil",produces="application/json") public ResponseData<TbCoreOrg> orgDeatil(String id){ ResponseData<TbCoreOrg> r = systemClient.queryOrgById(id); return r; } ~~~ 10. 啟動工程,我們去調用查看API調用返回結果 ![](https://img.kancloud.cn/8d/4f/8d4f4cb1af05244830ef2e43130edadb_2912x1476.png) 11. 我們可以看到我們請求jpower-user服務的接口成功通過feign調用jpower-system查出了數據。 ## 后續 * 下面我們來看下 Sentinel 熔斷機制
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看