6、Sping Cloud Feign

1.Spring Cloud Feign簡介

(1).Fegin簡介

  官方文檔:http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

 

  Feign是一個聲明式WebService客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個接口,然後在上面添加註解,同時也支持JAX-RS標準的註解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標準註解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

  Feign是一個聲明式的Web服務客戶端,使得編寫Web服務客戶端變得非常容易,

只需要創建一個接口,然後在上面添加註解即可。

  官方文檔:https://github.com/OpenFeign/feign

(2).Feign作用

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

(3).Feign集成Ribbon

  利用Ribbon維護了MicroServiceCloud-Dept的服務列表信息,並且通過輪詢實現了客戶端的負載均衡。而與Ribbon不同的是,通過feign只需要定義服務綁定接口且以聲明式的方法,優雅而簡單的實現了服務調用。

2.Feign實現步驟

 

(1).創建工程

[1].新建工程feign

  新建microservicecloud-consumer-dept-feign

 

 

  將consumer裏的com包和application.yml配置文件複製到新創建的feign裏

 

  修改主啓動類名

 

[2].pom文件

  配置microservicecloud-consumer-dept-feign的pom文件

<?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>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

    <artifactId>microservicecloud-consumer-dept-feign</artifactId>

 

    <dependencies>

        <dependency><!-- 自己定義的api -->

            <!--注意:groupId我們需要由com.hosystem.springcloud 變成com.hosystem就不會出現'Dependency 'com.hosystem.springcloud:microservicecloud-api:1.0-SNAPSHOT' not found'錯誤-->

            <groupId>com.hosystem</groupId>

            <artifactId>microservicecloud-api</artifactId>

            <version>${project.version}</version>

        </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>

 

        <!-- 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>

 

        <!-- feign相關 -->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

    </dependencies>

 

</project>

(2).修改microservicecloud-api工程

 

[1].修改pom文件

  新增部分:

        <!--feign相關-->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

    </dependencies>

  完整部分:

<?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>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

    <!--當前Module叫什麼名字 -->

    <artifactId>microservicecloud-api</artifactId>

 

    <!-- 當前Module需要用到的jar包,按自己需求添加,如果父類已經包含了,可以不用寫版本號 -->

    <dependencies>

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

        </dependency>

 

        <!--feign相關-->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

    </dependencies>

 

</project>

[2].創建接口

  新建DeptClientService接口並新增註解@FeignClient。

package com.hosystem.springcloud.service;

 

import com.hosystem.springcloud.entities.Dept;

import org.springframework.cloud.netflix.feign.FeignClient;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

 

import java.util.List;

 

@FeignClient(value = "MICROSERVICECLOUD-DEPT")

public interface DeptClientService

{

    @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)

    public Dept get(@PathVariable("id") long id);

 

    @RequestMapping(value = "/dept/list",method = RequestMethod.GET)

    public List<Dept> list();

 

    @RequestMapping(value = "/dept/add",method = RequestMethod.POST)

    public boolean add(Dept dept);

}

[3].mvn clean和mvn install

 

(3).修改microservicecloud-consumer-dept-feign

  microservicecloud-consumer-dept-feign工程修改Controller,添加上一步新建的DeptClientService接口

package com.hosystem.springcloud.controller;

 

import com.hosystem.springcloud.entities.Dept;

import com.hosystem.springcloud.service.DeptClientService;

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;

import java.util.List;

 

 

@RestController //注:這裏一定不能忘記註解@RestController 否則出現404;

public class DeptController_Consumer {

 

    //注:如果出現could not autowired no beans of 'DeptClientService' type found. 我們只需要將@autowired改成@Resource即可

    @Resource

    private DeptClientService service;

 

    @RequestMapping(value = "/consumer/dept/get/{id}")

    public Dept get(@PathVariable("id") Long id)

    {

        return this.service.get(id);

    }

 

    @RequestMapping(value = "/consumer/dept/list")

    public List<Dept> list()

    {

        return this.service.list();

    }

 

    @RequestMapping(value = "/consumer/dept/add")

    public Object add(Dept dept)

    {

        return this.service.add(dept);

    }

}

(4).修改主啓動類

  修改microservicecloud-consumer-dept-feign主啓動類

package com.hosystem.springcloud;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.netflix.feign.EnableFeignClients;

import org.springframework.context.annotation.ComponentScan;

 

 

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients(basePackages= {"com.hosystem.springcloud"})

@ComponentScan("com.hosystem.springcloud")

public class DeptConsumer80_Feign_App

{

    public static void main(String[] args)

    {

        SpringApplication.run(DeptConsumer80_Feign_App.class, args);

    }

}

(5).測試

[1].啓動eureka集羣

 

[2].啓動provider8001、provider8002、provider8003

 

[3].啓動Feign

 

[4].訪問頁面

  Feign自帶負載均衡配置項

http://localhost/consumer/dept/list

 

(6).總結

  Feign通過接口的方法調用Rest服務(之前是Ribbon+RestTemplate),該請求發送給Eureka服務器(http://MICROSERVICECLOUD-DEPT/dept/list),通過Feign直接找到服務接口,由於在進行服務調用的時候融合了Ribbon技術,所以也支持負載均衡作用。

 

參考文檔:

http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

https://github.com/OpenFeign/feign

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章