SpringCloud--服务接口调用--OpenFegin

  1. OpenFegin的理论介绍
  2. OpenFegin的使用:

    1.  

       

       

    2. <?xml version="1.0" encoding="UTF-8"?>
      
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
      
        <parent>
          <groupId>com.model</groupId>
          <version>1.0-SNAPSHOT</version>
          <artifactId>SpringCloud</artifactId>
        </parent>
      
        <artifactId>springcloud002-consumer-openfeign-order80</artifactId>
      
      
        <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <maven.compiler.source>1.8</maven.compiler.source>
          <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
      
        <dependencies>
      
      
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
          </dependency>
      
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
          </dependency>
          <!--引入实例类的依赖-->
          <dependency>
            <groupId>com.model</groupId>
            <artifactId>springcloud003-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
          </dependency>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
          </dependency>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
          </dependency>
          <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
          </dependency>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
          </dependency>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
          </dependency>
        </dependencies>
      
        <build>
          <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
              <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
              <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
              </plugin>
              <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
              <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
              </plugin>
              <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
              </plugin>
              <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
              </plugin>
              <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
              </plugin>
              <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
              </plugin>
              <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
              </plugin>
              <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
              <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
              </plugin>
              <plugin>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.0.0</version>
              </plugin>
            </plugins>
          </pluginManagement>
        </build>
      </project>
    3. package com.model;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
      import org.springframework.cloud.openfeign.EnableFeignClients;
      
      /**
       * Hello world!
       *
       */
      @SpringBootApplication
      @EnableDiscoveryClient
      @EnableFeignClients
      public class OrderOpenFeignMain80
      {
          public static void main( String[] args )
          {
              SpringApplication.run(OrderOpenFeignMain80.class, args);
          }
      }

       

    4. package com.model.feign;
      
      import com.model.pojo.CommonResult;
      import com.model.pojo.Payment;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.stereotype.Component;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/9/8 20:35
       */
      @Component
      @FeignClient(value = "springcloud001-provider-payment")
      public interface PaymentFeignService {
      
          @RequestMapping("/payment/getById/{id}")
          public CommonResult<Payment> getPayment(@PathVariable("id")Long id);
      
      
      
      }

       

    5. package com.model.controller;
      
      import com.model.feign.PaymentFeignService;
      import com.model.pojo.CommonResult;
      import com.model.pojo.Payment;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import javax.annotation.Resource;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/9/8 20:40
       */
      @RestController
      @RequestMapping("/consumer")
      public class OrderController {
      
          @Resource
          private PaymentFeignService paymentFeignService;
          @GetMapping("/payment/getById/{id}")
          public CommonResult<Payment> getPaymentById(@PathVariable("id")Long id){
              return paymentFeignService.getPayment(id);
          }
      }

       

    6.   

    7.  

    8.  

       

       

       

posted @ 2021-09-08 21:16  张紫韩  阅读(147)  评论(0编辑  收藏  举报