เมื่อใช้ microservices คุณมักจะมีปัญหาในการโทรระหว่างบริการ สปริงคลาวด์ให้การเรียกใช้อินเทอร์เฟซเฟซและการโทร RESTTEMPLATE
ที่นี่ฉันจะหารือเกี่ยวกับวิธีการโทร RestTemplate:
บริการ A: พารามิเตอร์วัตถุทั้งสามที่ได้รับพารามิเตอร์ทั้งสามนี้จะถูกสืบค้นผ่านฐานข้อมูล
บริการ B: การโทรบริการ A Service B ให้วิธีการสอบถามพารามิเตอร์สามพารามิเตอร์และต้องใช้พารามิเตอร์สามตัวในภายหลัง
สำหรับบริการ A มีสองวิธีในการจัดการกับมัน
1. บริการ B จัดเตรียมอินเทอร์เฟซแสร้งเพื่อเปิดเผยวิธีการสอบถามพารามิเตอร์สามพารามิเตอร์ บริการอ้างอิงโดยตรงไปยังพารามิเตอร์การสืบค้น บริการ B ต้องผ่านคำหลักแบบสอบถามสามคำในอดีต
ให้บริการ
@postmapping ("/import/{busicode}/{filepath}") แผนที่สาธารณะ <สตริงสตริง> importExcel (@PathVariable ("filePath") สตริง filePath,@pathVariable ("busicode")@requestbody map <string, string> พารามิเตอร์ Response.Setcharacterencoding ("UTF-8"); userInfo user = userutil.getUser (); return excelservice.importexcel (filepath, busicode, params, ผู้ใช้); - บริการบริการ
// แนะนำอินเทอร์เฟซ Feign ส่วนตัว excelfreign excelfreign; แผนที่สาธารณะ <สตริง, สตริง> importExcel (สตริง filepath, สตริง busicode, แผนที่ <สตริง, สตริง> พารามิเตอร์, ผู้ใช้ userInfo) {แผนที่ <สตริง, สตริง> result = new hashmap <สตริง, สตริง> (); excelfreign = springtool.getApplicationContext (). getBean (excelfreign.class); cmdimportConfigdto configdto = excelfreign.getcmdimportconfigbybusicode (busicode); cmdimportdto importdto = ใหม่ cmdimportdto (); importDto.SetImportConfigid (configdto.getId ()); importdto.setexcelpath (filepath); importdto.setparam (ใหม่ gsonBuilder (). สร้าง (). tojson (params)); importdto.setlog (""); impid ยาว = null; ลอง {impid = long.valueof (excelfreign.savecmdimportdto (importdto)); } catch (Exception e1) {e1.printstacktrace (); result.put ("ข้อผิดพลาด", "ข้อยกเว้นที่เกิดขึ้นในขณะที่บันทึก"); result.put ("ข้อความ", e1.getMessage ()); ผลการกลับมา; } ลอง {excelfreign.updateimportstatus (impid, importConstant.importstatus.submit, "ส่งประสบความสำเร็จ"); } catch (exception e) {e.printstacktrace (); } validateTask validateTask = new validateTask (); validateTask.init (impid, filepath, busicode, params, ผู้ใช้); ข้อความสตริง; ลอง {message = validateTask.call (); } catch (exception e) {e.printstacktrace (); result.put ("ข้อผิดพลาด", "ข้อยกเว้นที่เกิดขึ้นในการตรวจสอบ"); result.put ("ข้อความ", e.getMessage ()); ผลการกลับมา; } if (message! = null) {result.put ("ข้อผิดพลาด", "การตรวจสอบล้มเหลว"); result.put ("ข้อความ", ข้อความ); ผลการกลับมา; } StensedTask persisttask = new persentTask (); persisttask.init (impid, filepath, busicode, params, ผู้ใช้); result.putAll (importQueue.submit (StensentTask)); ผลการกลับมา; -B-FEGIN จัดทำโดย Service B
@feignClient (value = "frame-service", path = "/excelapi/v1") อินเตอร์เฟสสาธารณะ excelfreign ขยาย excelapi {}ให้บริการชั้น B API B-API
อินเทอร์เฟซสาธารณะ excelapi {/** * สถานะอัปเดต * @param impid * @param importType * @param result */@postmapping ("/updateimportstatus/{impid}/{importType}/{result}" @PathVariable ("ผลลัพธ์") ผลลัพธ์สตริง) โยนข้อยกเว้น; /** * รับรายการการกำหนดค่านำเข้า * @param busicode * @return */@getMapping ("/getimportconfig/{busicode}") cmdimportconfigdto getcmdimportconfigbybusicode (@pathVariable ("busicode") /** * บันทึกข้อมูล * @param importdto * @return */@postmapping ("/saveimport") สตริง savecmdimportdto (@requestbody cmdimportdto importdto); -Service B ใช้การกระทำของอินเตอร์เฟส API
@RestController @RequestMapping ("/excelapi/v1") คลาสสาธารณะ excelfeignaction ใช้ Excelapi {@autowired ส่วนตัว cmdexportService ExportService; /** * รับรายการการกำหนดค่านำเข้า * @param busicode * @return */@getMapping ("/getImportConfig/{busicode}") สาธารณะ cmdimportconfigdto getcmdimportconfigbybusicode (@pathvariable ("busicode") busicode) CMDIMPORTCONFIGSERVICE.GETCMDIMPORTCONFIGBYBUSICODE (BUSICODE); }/** * สถานะอัปเดต * @param impid * @param importStatus * @param result */@postmapping ("/updateimportstatus/{impid}/{importType}/{result}") public updateMportStatus (@PathVariable @PathVariable ("ผลลัพธ์") ผลลัพธ์สตริง) โยนข้อยกเว้น {CMDimportService.UpDateImportStatus (impid, importStatus, วันที่ใหม่ (), ผลลัพธ์); }/** * บันทึกข้อมูล * @param importdto * @return */@postmapping ("/saveimport") สตริงสาธารณะ savecmdimportdto (@requestbody cmdimportdto importdto) {ลอง {cmdimportservice.savecmdimportdo return importdto.getId (); } catch (exception e) {e.printstacktrace (); โยน BusinessRuntimeException ใหม่ ("ข้อยกเว้นระบบ"); -บริการ B เรียกเลเยอร์การกระทำ
/***** @param busicode การเข้ารหัสธุรกิจที่ส่งออกสามารถกำหนดได้ว่าโมดูลสามารถส่งออกได้* @param ค่าขอค่าพารามิเตอร์** ผ่านพารามิเตอร์ที่ซับซ้อนผ่าน RestTemplate* ส่งคืนสตรีมไฟล์และปล่อยให้เบราว์เซอร์ปรากฏขึ้นเพื่อดาวน์โหลด*/@postmapping expdownloadv3 (@PathVariable ("busicode") สตริง busicode, @requestbody แผนที่ <สตริง, วัตถุ> ค่า, คำขอ httpservletrequest) โยนข้อยกเว้น {ถ้า (stringutils.isblank (busicode)) {โยนธุรกิจใหม่ } // รับแผนที่กระบวนการดำเนินการ = RestTemplate.postForObject ("http: //" + serviceId + "/excelapi/v1/filename"/"+busicode,values,map.class) cmdexportconfigdto cmdexportconfig = exportService.getCMDEXPORTCONFIGBYBUSICODE (BUSICODE); ส่วนหัว = httpheaders ใหม่ (); FrameUrlConstants.transfromfilename (ผู้ใช้ชื่อไฟล์) + "."2. บริการ B โดยตรงผ่านพารามิเตอร์การสืบค้นไปยังบริการก
บริการ A:
/** * รับพารามิเตอร์ผ่าน * รับคู่คีย์สามคู่ต่อไปนี้ * CMDEXPORTCONFIG: CMDEXPORTCONFIGDTO * ExportFieldList: รายการ <cmdexportfieldConfigdto> * พารามิเตอร์: @param params * @param reponse * @param Response * @return */@postmapping ( MAP <string, Object> params, httpservletRequest คำขอ, การตอบสนอง httpservletResponse) {response.Setcharacterencoding ("UTF-8"); ลอง {// รับพา ธ ไฟล์ไปยัง ObjectMapper mapper = new ObjectMapper (); LinkedHashMap requestParmap = (linkedHashMap) params.get ("cmdexportConfig"); CMDEXPORTCONFIGDTO CMDEXPORTCONFIGDTO = NULL; รายการ <cmdexportfieldconfigdto> exportfieldList = null; if (requestparmap.size ()> 0) {cmdexportConfigdto = mapper.convertValue (requestparmap, cmdexportconfigdto.class); } arrayList arrayList = (arrayList) params.get ("ExportFieldList"); if (arraylist.size ()> 0) {exportfieldList = mapper.convertValue (arraylist, typereference ใหม่ <cmdexportfieldConfigdto> () {}); } ค่าแผนที่ = (แผนที่) params.get ("params"); String filePath = Excelservice.ExportExcel (CMDEXPORTCONFIGDTO, ExportFieldList, params, request.getServletContext (). getRealPath ("/")); แผนที่ <สตริงสตริง> แผนที่ = ใหม่ hashmap <สตริงสตริง> (); map.put ("filepath", filepath); ส่งคืน ResponseEntity ใหม่ (แผนที่ httpstatus.ok); } catch (ioexception e) {โยน runtimeException ใหม่ ("ข้อผิดพลาดไฟล์เอาต์พุต"); -บริการ B:
/**** @param busicode การเข้ารหัสธุรกิจที่ส่งออกสามารถกำหนดได้ว่าโมดูลบางอย่างจะดำเนินการส่งออก* @param ค่าพารามิเตอร์คำขอ** ผ่านพารามิเตอร์ที่ซับซ้อนผ่าน RestTemplate* ส่งคืนสตรีมไฟล์และปล่อยให้เบราว์เซอร์ปรากฏขึ้นเพื่อดาวน์โหลด ปัจจุบันจำเป็นต้องแก้ปัญหาการตอบสนองต่อสตรีมไบต์ไปยังคอนโซลของเบราว์เซอร์ วิธีการดาวน์โหลด URL จะถูกใช้หลังจากนั้น*/@postmapping (value = "/export/v3/{busicode}", ผลิต = mediaType.text_plain_value) @ResponseBody ResponseEntity <Byte []> expdownloadv3 คำขอ) โยนข้อยกเว้น {String busicode = value.get ("busicode"). toString (); ถ้า (stringutils.isblank (busicode)) {โยน BusinessRuntimeException ใหม่ ("พารามิเตอร์ไม่ถูกต้องโปรดตรวจสอบว่าพารามิเตอร์ถูกต้องหรือไม่ busicode?"); } // รับแผนที่กระบวนการดำเนินการ = ExcuTerestTemplate (busicode, ค่า); String path = (String) map.get ("filepath"); ไบต์ [] excel = fastdfsclient.downloadtobytes (พา ธ ); CMDEXPORTCONFIGDTO CMDEXPORTCONFIG = ExportService.getCMDexportConfigbyBusicode (busicode); // รับชื่อสตริงชื่อไฟล์ = cmdexportConfig.getReportName (); // รับสตริงชื่อต่อท้ายไฟล์ extfileName = path.substring (path.lastindexof ('.')+1); ส่วนหัว httpheaders = httpheaders ใหม่ (); eragent = request.getheader ("user-agent"); Headers.setContentType (MediaType.Application_octet_stream); Headers.SetContentDispositionFormData ("สิ่งที่แนบมา", FrameUrlConstants.transFromFileName (useragent, filename) + "." + extfilename); ส่งคืน ResponseEntity ใหม่ <byte []> (excel, ส่วนหัว, httpstatus.ok); } / ** * เรียกใช้การร้องขอ * @param busicode * @param ตัวแปร * @return * / แผนที่ส่วนตัว exhausttemplate (สตริง busicode, ตัวแปรแผนที่) {string serviceId = ""; // Query Export Configuration CMDEXPORTCONFIGDTO CMDEXPORTCONFIG = ExportService.GetCMDEXPORTCONFIGBYBUSICODE (BUSICODE); serviceId = cmdexportConfig.getSystemType (); if (cmdexportConfig == null) {โยน BusinessRuntimeException ใหม่ ("ไม่สามารถส่งออกได้โดยไม่ต้องส่งออกการกำหนดค่า"); } // รับข้อมูลฟิลด์ส่งออกตามรายการรหัสการกำหนดค่าการส่งออก <cmdexportfieldconfigdto> exportfieldList = exportService.getAllCmdExportFieldConfigdTobyconfigid (cmdexportConfig.getId ()); if (stringutils.isblank (serviceId)) {โยน BusinessRuntimeException ใหม่ ("บริการที่ส่งออกไม่ได้กำหนดค่า"); } แผนที่ <สตริงวัตถุ> urivariables = new hashmap <> (); urivariables.put ("cmdexportconfig", cmdexportconfig); urivariables.put ("ExportFieldList", ExportFieldList); urivariables.put ("params", ตัวแปร); ส่งคืน RestTemplate.postForObject ("http: //" + serviceId + "/excelservice/export/v2", httpentity ใหม่ (urivariables), map.class); -การตั้งค่าส่วนหัวของเบราว์เซอร์
/ ** * ตั้งค่าการเข้ารหัส URL ของไฟล์ดาวน์โหลดตามประเภทเบราว์เซอร์ที่แตกต่างกัน * @param useragent * @param filename * @return * @throws Exception */ สตริงคงที่สาธารณะ transfromfilename (สตริง useragent, string filename) if (stringUtils.contains (useragent, "msie")) {// เช่นเบราว์เซอร์ finalfileName = urlencoder.encode (ชื่อไฟล์, "UTF-8"); } อื่นถ้า (stringUtils.contains (useragent, "mozilla")) {// Google, firefox finalfilename = สตริงใหม่ (filename.getBytes ("gbk"), "iso8859-1"); } else {finalFileName = urlencoder.encode (ชื่อไฟล์, "UTF-8"); // เบราว์เซอร์อื่น} ส่งคืน finalfilename; -สรุป
ข้างต้นคือเมธอดสปริงคลาวด์ RestTemplate (หลายวัตถุ) แนะนำให้คุณรู้จักโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!