赞
踩
第一步:在porm中引入openfeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
第二步:创建外部服务接口类,添加注解和添加外部接口方法
@Component
@FeignClient(value = "identity")
//value中的值为对应微服务的服务名
public interface DockingClient {
@PostMapping("/docking/modelTree/{modelId}")
Result<?> getModelTree(@PathVariable(value = "modelId") String modelId);
@PostMapping("/identityManage/allIdentities")
//括号内是外部服务的接口地址
Result<?> getAllIdentities();
}
第三步:调用该接口获取外部接口数据
@Resource private DockingClient dockingClient; @Override public Result<?> getNumber(Integer stuNum,Integer lessonsNum) { Result<?> allIdentities=dockingClient.getAllIdentities(); log.info("allIdentities:[{}]", allIdentities); if (!allIdentities.isSuccess()) { log.error(allIdentities.getMessage()); throw new ErpException(allIdentities.getMessage()); } int identityNum=Integer.parseInt(String.valueOf(((Map<String, Object>) allIdentities.getResult()).get("total"))); Map<String,Integer> map = new HashMap<String, Integer>(); map.put("identityNum",identityNum); map.put("lessonsNum",lessonsNum); map.put("studentNum",stuNum); return Result.OK(map); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。