Feign的介紹和使用


一、Feign的簡介

  Feign是一個聲明式 WebService 客戶端,使用Feign能夠讓編寫Web Service 客戶端更加簡單,它的使用方法是定義一個接口,然后在上面添加注解,同時也支持JAX-RS標准的注解。Feign也支持可插拔式的編碼器和解碼器。

  Spring Cloud 對 Fiegn 進行了封裝,使其支持了Spring MVC 標准注解和HttpMessageConverts。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

  前面使用Ribbon+RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模版化的調用方法,但是在實際的開發中,由於對服務依賴的調用可能不止一處。往往一個接口會被多處調用,所以通常會對每個微服務自行封裝一些客戶端類來包裝依賴服務的調用,所以Feign在此基礎上做了進一步封裝,有他來幫助我們自定義和實現依賴服務接口的定義,在Feign的實現下,我們只需要創建一個接口並使用注解的方式來配置他(以前是Dao接口上面標注Mapper注解,現在是一個微服務接口上面標注一個Feign注解即可),即可完成服務提供放的接口綁定,簡化了使用Spring Cloud Ribbon時,自動封裝服務調用客戶端的開發量。

 

 二、工程配置

1、在 microservicecloud-api 工程增加pom依賴和代碼:

(1)pom.xml文件中添加feign的jar包:spring-cloud-starter-feign

<!-- feign相關 -->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

(2)新建DeptClientService接口並且添加@FeignClient注解

@FeignClient(value = "microservicecloud-dept") public interface DeptClientService { @RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept); @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") Long id); @RequestMapping(value = "/dept/get/list", method = RequestMethod.GET) public List<Dept> list(); }

說明:

@FeignClient的value值指明對哪個微服務進行負載均衡;

@RequestMapping 是Spring提供的注解,這里可以直接使用以前使用SpringMVC時用過的各種注解,唯一不同的是,這里只是把注解用在了接口上。

 

2、新建一個消費者項目(Feign):microservicecloud-consumer-dept-feign

(1)添加pom.xml中的依賴

 <dependencies>
        <dependency><!-- 自己定義的api -->
            <groupId>com.yufeng.springcloud</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- Ribbon相關 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 修改后立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

(2)在主啟動類中增加 @EnableFeignClients 注解

@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class DeptConsumer80_Feign_App { public static void main(String[] args) { SpringApplication.run(DeptConsumer80_Feign_App.class, args); } }

在主啟動類中增加 @EnableFeignClients 注解之后,在Controller中使用 @Autowired 注解導入 DeptClientService 時候才不會報錯;

(3)在controller包下增加 DeptController ,使用@Autowired直接注入上面定義的 FeignConsumerService接口對應的實例

@RestController public class DeptController { @Autowired private DeptClientService deptClientService; @RequestMapping(value = "/consumer/dept/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") Long id) { return deptClientService.get(id); } @RequestMapping(value = "/consumer/dept/get/list", method = RequestMethod.GET) public List<Dept> list() { return deptClientService.list(); } }

 

(4)結果驗證:啟動eureka集群,啟動3個生產者服務,啟動本服務;

在瀏覽器中打開:http://localhost/consumer/dept/get/1,可以看到輪詢調用了3個生產者服務;

 

importcom.yufeng.springcloud.entities.Dept;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
* Created by Administrator on 2019/6/11.
*/
@FeignClient(value = "microservicecloud-dept")
public interface FeignConsumerService
{
@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(@RequestBody Dept dept);

@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") Long id);

@RequestMapping(value = "/dept/get/list", method = RequestMethod.GET)
public List<Dept> list();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM