1. 程式人生 > 其它 >07-SpringCloud 之 Feign

07-SpringCloud 之 Feign

什麼是 Feign

  • Feign 即:基於介面的服務呼叫,是一個宣告式的 Web Service 客戶端
  • Feign 集成了 Ribbon 可以使用 Ribbon 的負載均衡演算法

Feign 的使用

  1. 新建 Module:spring-cloud-consumer-dept-feign-80

  2. 編寫 pom 引入依賴

    pom.xml
    <?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">
        <parent>
            <artifactId>spring-cloud-netflix</artifactId>
            <groupId>com.kaishen</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>spring-cloud-consumer-dept-feign-80</artifactId>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <!-- 引入該模組所需依賴 -->
        <dependencies>
            <!-- api -->
            <dependency>
                <groupId>com.kaishen</groupId>
                <artifactId>spring-cloud-api</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <!-- lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
            <!--SpringBoot Web-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- SpringBoot 監控 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!-- Eureka Client -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
            <!-- Feign -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
        </dependencies>
    </project>
    
  3. 編寫 application.yml 配置註冊中心

    application.yml
    server:
      port: 80
    
    # Eureka 配置
    eureka:
      client:
        register-with-eureka: false # Consumer 無需註冊
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    
  4. 編寫啟動類,開啟 Feign 掃描:@EnableFeignClients(basePackages = {"com.kaishen"})

    DeptConsumerFeignApplication80
    package com.kaishen;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    /**
     * 部門服務消費者啟動類
     *  Feign 實現遠端呼叫
     * @author : toby
     * Create in 11:23 2022/5/9
     */
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients(basePackages = {"com.kaishen"})
    public class DeptConsumerFeignApplication80 {
    
        public static void main(String[] args) {
            SpringApplication.run(DeptConsumerFeignApplication80.class, args);
        }
    }
    
  5. 定義介面,開啟註解:@FeignClient(value = "SPRING-CLOUD-PROVIDER-DEPT", contextId = "1")

    DeptFeignService
    package com.kaishen.feign.service;
    
    import com.kaishen.pojo.Dept;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    /**
     * 部門客戶端服務
     * @author : toby
     * Create in 11:26 2022/5/9
     */
    @Component
    @FeignClient(value = "SPRING-CLOUD-PROVIDER-DEPT", contextId = "1")
    public interface DeptFeignService {
    
        /**
         * 根據部門編號查詢部門資訊
         * @param id 部門編號
         * @return 返回部門資訊
         */
        @GetMapping("/feign/dept/get/{id}")
        Dept feignQueryDeptById(@PathVariable("id") Long id);
    }
    

    注意:

    • 通過 @FeignClient 註解呼叫同一個微服務時,會出現 Bean 重複註冊的情況,可使用 contextId = "1" 來區分其唯一性
    • // 此處的 @PathVariable 註解,必須指定("id")
  6. 編寫 Controller 使用 Feign 實現遠端呼叫

    DeptConsumerFeignController
    package com.kaishen.controller;
    
    import com.kaishen.feign.service.DeptFeignService;
    import com.kaishen.feign.service.DeptHystrixDemotionService;
    import com.kaishen.feign.service.DeptHystrixFusingService;
    import com.kaishen.pojo.Dept;
    import lombok.extern.slf4j.Slf4j;
    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;
    
    /**
     * 部門服務消費者 Feign 控制層
     * @author : toby
     * Create in 11:43 2022/5/9
     */
    @RestController
    @Slf4j
    public class DeptConsumerFeignController {
    
        /**
         * Feign 實現遠端服務呼叫
         */
        @Resource
        private DeptFeignService deptFeignService;
    
        // region Feign 實現遠端服務呼叫
        /**
         * 根據部門編號查詢部門資訊
         *  -- Feign 實現遠端服務呼叫
         * @param id 部門編號
         * @return 返回部門資訊
         */
        @RequestMapping("/feign/dept/get/{id}")
        public Dept feignQueryDeptById(@PathVariable Long id) {
            // Feign 實現遠端服務呼叫
            Dept dept = deptFeignService.feignQueryDeptById(id);
    
            log.info("Feign 查詢結果:" + dept);
    
            return dept;
        }
        // endregion
    }