代码资料链接:https://download.csdn.net/download/ly1h1/90945836
1.背景
若依的微服务框架,少不了各微服务之间的接口调用,以下是采用feign来进行微服务之间的方法调用。
2.案例说明
在system模块下的某个接口,调用factory(定制化微服务)的接口方法。
(注意:多入参用(自定义模型那种)POST,普通单个变量采用Get,本人使用的感受,不然会报错)
3.factory被调用方说明
3.1.接口层创建接口controller
(直接在自动生成的接口文件里面写2个接口方法)
3.1.1 接口代码编写,注意RequiresPermissions和GetMapping路径
package com.ruoyi.factory.controller;import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;import com.ruoyi.common.core.domain.R;
import com.ruoyi.factory.api.domain.FaFactoryconfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.factory.domain.FaAbnormalinformation;
import com.ruoyi.factory.service.IFaAbnormalinformationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;/*** 异常信息Controller* * @author ruoyi* @date 2025-06-01*/
@RestController
@RequestMapping("/abnormalinformation")
public class FaAbnormalinformationController extends BaseController
{@Autowiredprivate IFaAbnormalinformationService faAbnormalinformationService;/*** 查询异常信息列表*/@RequiresPermissions("factory:abnormalinformation:list")@GetMapping("/list")public TableDataInfo list(FaAbnormalinformation faAbnormalinformation){startPage();List<FaAbnormalinformation> list = faAbnormalinformationService.selectFaAbnormalinformationList(faAbnormalinformation);return getDataTable(list);}/*** 导出异常信息列表*/@RequiresPermissions("factory:abnormalinformation:export")@Log(title = "异常信息", businessType = BusinessType.EXPORT)@PostMapping("/export")public void export(HttpServletResponse response, FaAbnormalinformation faAbnormalinformation){List<FaAbnormalinformation> list = faAbnormalinformationService.selectFaAbnormalinformationList(faAbnormalinformation);ExcelUtil<FaAbnormalinformation> util = new ExcelUtil<FaAbnormalinformation>(FaAbnormalinformation.class);util.exportExcel(response, list, "异常信息数据");}/*** 获取异常信息详细信息*/@RequiresPermissions("factory:abnormalinformation:query")@GetMapping(value = "/{abnormalinformationId}")public AjaxResult getInfo(@PathVariable("abnormalinformationId") Long abnormalinformationId){return success(faAbnormalinformationService.selectFaAbnormalinformationByAbnormalinformationId(abnormalinformationId));}/*** 新增异常信息*/@RequiresPermissions("factory:abnormalinformation:add")@Log(title = "异常信息", businessType = BusinessType.INSERT)@PostMappingpublic AjaxResult add(@RequestBody FaAbnormalinformation faAbnormalinformation){return toAjax(faAbnormalinformationService.insertFaAbnormalinformation(faAbnormalinformation));}/*** 修改异常信息*/@RequiresPermissions("factory:abnormalinformation:edit")@Log(title = "异常信息", businessType = BusinessType.UPDATE)@PutMappingpublic AjaxResult edit(@RequestBody FaAbnormalinformation faAbnormalinformation){return toAjax(faAbnormalinformationService.updateFaAbnormalinformation(faAbnormalinformation));}/*** 删除异常信息*/@RequiresPermissions("factory:abnormalinformation:remove")@Log(title = "异常信息", businessType = BusinessType.DELETE)@DeleteMapping("/{abnormalinformationIds}")public AjaxResult remove(@PathVariable Long[] abnormalinformationIds){return toAjax(faAbnormalinformationService.deleteFaAbnormalinformationByAbnormalinformationIds(abnormalinformationIds));}/*** 查询工厂配置列表*/@RequiresPermissions("factory:abnormalinformation:remoteabnormallist")@PostMapping("/remoteabnormallist")public R<List<FaFactoryconfigVO>> remoteabnormallist(@RequestBody FaFactoryconfigVO faFactoryconfigVO){faFactoryconfigVO.setFactoryCode("123");faFactoryconfigVO.setFactoryCode("2345");List<FaFactoryconfigVO> LIS= new ArrayList<>();LIS.add(faFactoryconfigVO);return R.ok(LIS);}/*** 查询工厂配置列表 @GetMapping(value = "/{factoryconfigId}")*/@RequiresPermissions("factory:abnormalinformation:getabnormalremotelist")@GetMapping(value = "/getabnormalremotelist/{abnormalid}")public R<List<FaFactoryconfigVO>> getabnormalremotelist(@PathVariable("abnormalid") Long abnormalid){FaFactoryconfigVO faFactoryconfigVO = new FaFactoryconfigVO();faFactoryconfigVO.setFactoryCode("123");faFactoryconfigVO.setFactoryCode("2345");List<FaFactoryconfigVO> LIS= new ArrayList<>();LIS.add(faFactoryconfigVO);return R.ok(LIS);}}
3.1.2 数据模型VO(统一被调用方ruoyi-api下编写,调用方直接引用)
package com.ruoyi.factory.api.domain;import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;public class FaAbnormalInformationVO extends BaseEntity
{private static final long serialVersionUID = 1L;/** 工厂主键 */private Long factoryconfigId;/** 工厂编号 */@Excel(name = "工厂编号")private String factoryCode;/** 工厂名称 */@Excel(name = "工厂名称")private String factoryName;public void setFactoryconfigId(Long factoryconfigId){this.factoryconfigId = factoryconfigId;}public Long getFactoryconfigId(){return factoryconfigId;}public void setFactoryCode(String factoryCode){this.factoryCode = factoryCode;}public String getFactoryCode(){return factoryCode;}public void setFactoryName(String factoryName){this.factoryName = factoryName;}public String getFactoryName(){return factoryName;}@Overridepublic String toString() {return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE).append("factoryconfigId", getFactoryconfigId()).append("factoryCode", getFactoryCode()).append("factoryName", getFactoryName()).append("remark", getRemark()).toString();}
}
3.2 接口层ruoyi-api模块下,编写对外暴露的接口代码
3.2.1 调用接口Interface
package com.ruoyi.factory.api;import com.ruoyi.common.core.constant.ServiceNameConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.factory.api.domain.FaFactoryconfigVO;
import com.ruoyi.factory.api.factory.RemoteAbnormalInformationFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;import java.util.ArrayList;
import java.util.List;/*** 工厂服务* * @author ruoyi*/
@FeignClient(contextId = "remoteAbnormalInformationService", value = ServiceNameConstants.FACRORY_SERVICE, fallbackFactory = RemoteAbnormalInformationFallbackFactory.class)
public interface RemoteAbnormalInformationService
{@PostMapping("/abnormalinformation/remoteabnormallist")public R<List<FaFactoryconfigVO>> remoteabnormallist(@RequestBody FaFactoryconfigVO faFactoryconfigVO);@GetMapping("/abnormalinformation/getabnormalremotelist/{factoryconfigId2}")public R<List<FaFactoryconfigVO>> getabnormalremotelist(@PathVariable("factoryconfigId2") Long factoryconfigId2);
}
3.2.2 降级机制代码Factory
package com.ruoyi.factory.api.factory;import com.ruoyi.common.core.domain.R;
import com.ruoyi.factory.api.RemoteAbnormalInformationService;
import com.ruoyi.factory.api.RemoteFactoryConfigService;
import com.ruoyi.factory.api.domain.FaFactoryconfigVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;import java.util.ArrayList;
import java.util.List;/*** 用户服务降级处理* * @author ruoyi*/
@Component
public class RemoteAbnormalInformationFallbackFactory implements FallbackFactory<RemoteAbnormalInformationService>
{private static final Logger log = LoggerFactory.getLogger(RemoteFactoryConfigFallbackFactory.class);@Overridepublic RemoteAbnormalInformationService create(Throwable throwable){log.error("异常服务调用失败:{}", throwable.getMessage());return new RemoteAbnormalInformationService(){@Overridepublic R<List<FaFactoryconfigVO>> remoteabnormallist(FaFactoryconfigVO faFactoryconfigVO){return R.fail("异常信息失败:" + throwable.getMessage());}@Overridepublic R<List<FaFactoryconfigVO>> getabnormalremotelist(Long factoryconfigId2){return R.fail("异常信息失败:" + throwable.getMessage());}};}
}//@RequiresPermissions("factory:abnormalinformation:remoteabnormallist")
//@PostMapping("/remoteabnormallist")
//public R<List<FaFactoryconfigVO>> remoteabnormallist(@RequestBody FaFactoryconfigVO faFactoryconfigVO)
//{
// faFactoryconfigVO.setFactoryCode("123");
// faFactoryconfigVO.setFactoryCode("2345");
// List<FaFactoryconfigVO> LIS= new ArrayList<>();
// LIS.add(faFactoryconfigVO);
// return R.ok(LIS);
//}
//
//
///**
// * 查询工厂配置列表 @GetMapping(value = "/{factoryconfigId}")
// */
//@RequiresPermissions("factory:abnormalinformation:getabnormalremotelist")
//@GetMapping(value = "/getabnormalremotelist/{abnormalid}")
//public R<List<FaFactoryconfigVO>> getabnormalremotelist(@PathVariable("abnormalid") Long abnormalid)
//{
// FaFactoryconfigVO faFactoryconfigVO = new FaFactoryconfigVO();
// faFactoryconfigVO.setFactoryCode("123");
// faFactoryconfigVO.setFactoryCode("2345");
// List<FaFactoryconfigVO> LIS= new ArrayList<>();
// LIS.add(faFactoryconfigVO);
// return R.ok(LIS);
//}
4.system调用方说明
4.1 启动类代码Application,注意需要将Bean的扫表目录加上调用方路径,如果不加的话,会出现空指针的情况。
3.2.3 system模块下找一个调用的接口方法里面,编写调用的代码
package com.ruoyi.system.controller;import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.factory.api.RemoteAbnormalInformationService;
import com.ruoyi.factory.api.RemoteFactoryConfigService;
import com.ruoyi.factory.api.domain.FaFactoryconfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.RpCompanyconfig;
import com.ruoyi.system.service.IRpCompanyconfigService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;/*** 公司配置Controller* * @author ruoyi* @date 2025-05-25*/
@RestController
@RequestMapping("/companyconfig")
public class RpCompanyconfigController extends BaseController
{@Autowiredprivate IRpCompanyconfigService rpCompanyconfigService;@Autowiredprivate RemoteFactoryConfigService remoteFactoryConfigService;@Autowiredprivate RemoteAbnormalInformationService remoteAbnormalInformationService;/*** 查询公司配置列表*/@RequiresPermissions("system:companyconfig:list")@GetMapping("/list")public TableDataInfo list(RpCompanyconfig rpCompanyconfig){startPage();
// FaFactoryconfigVO test= new FaFactoryconfigVO();
// test.setFactoryCode("1");
// test.setFactoryName("2");
// R<List<FaFactoryconfigVO>> ASD= remoteFactoryConfigService.remotelist(test);
// Long longget=2L;
// R<List<FaFactoryconfigVO>> ASD2=remoteFactoryConfigService.getremotelist(longget);FaFactoryconfigVO test= new FaFactoryconfigVO();test.setFactoryCode("1");test.setFactoryName("2");R<List<FaFactoryconfigVO>> ASD= remoteAbnormalInformationService.remoteabnormallist(test);Long longget=2L;R<List<FaFactoryconfigVO>> ASD2=remoteAbnormalInformationService.getabnormalremotelist(longget);List<RpCompanyconfig> list = rpCompanyconfigService.selectRpCompanyconfigList(rpCompanyconfig);return getDataTable(list);}/*** 公司名称下拉列表*/@RequiresPermissions("system:companyconfig:list")@GetMapping("/selectCompanyData")public TableDataInfo selectCompanyData(){RpCompanyconfig rpCompanyconfig=new RpCompanyconfig();rpCompanyconfig.setRemark("1");List<RpCompanyconfig> list = rpCompanyconfigService.selectRpCompanyconfigList(rpCompanyconfig);return getDataTable(list);}/*** 导出公司配置列表*/@RequiresPermissions("system:companyconfig:export")@Log(title = "公司配置", businessType = BusinessType.EXPORT)@PostMapping("/export")public void export(HttpServletResponse response, RpCompanyconfig rpCompanyconfig){List<RpCompanyconfig> list = rpCompanyconfigService.selectRpCompanyconfigList(rpCompanyconfig);ExcelUtil<RpCompanyconfig> util = new ExcelUtil<RpCompanyconfig>(RpCompanyconfig.class);util.exportExcel(response, list, "公司配置数据");}/*** 获取公司配置详细信息*/@RequiresPermissions("system:companyconfig:query")@GetMapping(value = "/{companyId}")public AjaxResult getInfo(@PathVariable("companyId") Long companyId){return success(rpCompanyconfigService.selectRpCompanyconfigByCompanyId(companyId));}/*** 新增公司配置*/@RequiresPermissions("system:companyconfig:add")@Log(title = "公司配置", businessType = BusinessType.INSERT)@PostMappingpublic AjaxResult add(@RequestBody RpCompanyconfig rpCompanyconfig){return toAjax(rpCompanyconfigService.insertRpCompanyconfig(rpCompanyconfig));}/*** 修改公司配置*/@RequiresPermissions("system:companyconfig:edit")@Log(title = "公司配置", businessType = BusinessType.UPDATE)@PutMappingpublic AjaxResult edit(@RequestBody RpCompanyconfig rpCompanyconfig){return toAjax(rpCompanyconfigService.updateRpCompanyconfig(rpCompanyconfig));}/*** 删除公司配置*/@RequiresPermissions("system:companyconfig:remove")@Log(title = "公司配置", businessType = BusinessType.DELETE)@DeleteMapping("/{companyIds}")public AjaxResult remove(@PathVariable Long[] companyIds){return toAjax(rpCompanyconfigService.deleteRpCompanyconfigByCompanyIds(companyIds));}
}