Feign服务调用
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
1.引入依赖
<!--服务调用--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2.在服务调用端主启动类上加注解 @EnableFeignClients
3.在服务调用端定义接口写上被调用控制层方法完全路径
例
被调用控制层被调用方法为加粗方法
@RestController @RequestMapping("/eduvod/video") @CrossOrigin public class VodController { @Autowired private VodService vodService; //根据视频id删除视频 @DeleteMapping("/removeAliyunVideo/{videoId}") public R removeAliyunVideo(@PathVariable("videoId") String videoId){ vodService.removeAliyunVideo(videoId); return R.ok(); } }
调用层定义接口后续称之为自定义接口完全路径为黄色字体组合绿色字体为被调用服务的微服务名
@Component @FeignClient("service-vod") public interface VodClient { //调用方法的路径,路径要书写完全 @DeleteMapping("/eduvod/video/removeAliyunVideo/{videoId}") //红色部分名称必须书写 public R removeAliyunVideo(@PathVariable("videoId") String videoId); }
4.在调用端注入自定义接口调用接口中方法即可