OpenFeign 作用是可远程调用其他服务

    1. 在父 pom 中引入 spring cloud 依赖

      1. <dependencyManagement>
      2. <dependencies>
      3. <!--spring cloud 依赖-->
      4. <dependency>
      5. <groupId>org.springframework.cloud</groupId>
      6. <artifactId>spring-cloud-dependencies</artifactId>
      7. <version>${spring-cloud.version}</version>
      8. <type>pom</type>
      9. <scope>import</scope>
      10. </dependency>
      11. </dependencies>
      12. </dependencyManagement>
    2. 在公共模块的 pom.xml中引入 spring cloudOpenFeign 依赖

      1. <!--使模块可通过HTTP进行远程调用其他服务-->
      2. <dependency>
      3. <groupId>org.springframework.cloud</groupId>
      4. <artifactId>spring-cloud-starter-openfeign</artifactId>
      5. </dependency>

    3.添加 @EnableFeignClients 注释

    1. @SpringBootApplication
    2. @EnableFeignClients
    3. public class Application {
    4. public static void main(String[] args) {
    5. SpringApplication.run(Application.class, args);
    6. }
    7. }

    4.创建 client 目录,里面放需要调用的服务的接口

    1. // VodClient.java
    2. package com.catmmao.edu.client;
    3. import com.catmmao.utils.data.response.CommonResponse;
    4. import org.springframework.cloud.openfeign.FeignClient;
    5. import org.springframework.http.ResponseEntity;
    6. import org.springframework.web.bind.annotation.DeleteMapping;
    7. import org.springframework.web.bind.annotation.PathVariable;
    8. /**
    9. * 阿里视频点播服务
    10. *
    11. * @author catmmao
    12. * @since 2021/9/4 下午5:42
    13. */
    14. @FeignClient("edu-vod")
    15. public interface VodClient {
    16. /**
    17. * 删除视频
    18. *
    19. * @param id 阿里云生成的视频ID
    20. */
    21. @DeleteMapping(value = "/vod/{id}")
    22. ResponseEntity<CommonResponse<?>> deleteVideo(@PathVariable String id);
    23. }