SpringCloud與微服務Ⅶ --- Feign負載均衡


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

一.Feign是什么

Feign是一個聲明式的Web客戶端。它使編寫Web服務客戶端變得更容易,它的使用方法是定義一個接口,然后在上面添加注解,同時也支持JAX-RS標准的注解。Feign也支持可拔插式的編碼器和解碼器。SpringCloud對Feign進行了封裝,使其支持了SpringMVC標准注解和HttpMessageConverts。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

 

二.Feign能做什么

Feign旨在編寫Java Http客戶端更加容易。

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

 

Feign集成了Ribbon

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

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

 

三.Feign工程構建

修改microservice-api項目

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>microservice</artifactId>
        <groupId>com.wang.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>microservice-api</artifactId>
    <packaging>jar</packaging>

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

新增API接口類DeptClientService:

package com.wang.springcloud.service;

import com.wang.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;

/**
 *
 * @Description: 修改microservicecloud-api工程,根據已經有的DeptClientService接口

新建

一個實現了FallbackFactory接口的類DeptClientServiceFallbackFactory
 * @author 
 * @date 
 */
@FeignClient(value = "MICROSERVICE-DEPT")
//@FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class)
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);
}

上述工作完成后,使用clean,package重新打包成jar,方便其他項目調用。

新建microservice-consumer-dept-feign項目。

microservice-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>microservice</artifactId>
        <groupId>com.wang.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <dependencies>
        <dependency><!-- 自己定義的api -->
            <groupId>com.wang.springcloud</groupId>
            <artifactId>microservice-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency><!-- Feign相關 -->
            <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>

</project>

修改DeptController

@RestController
public class DeptController {
    @Autowired
    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);
    }

}

啟動所有項目,等待一段時間待注冊完畢后,訪問http://localhost/consumer/dept/list,不斷刷新頁面觀察是否有了負載均衡效果。

 

四.Nginx、Ribbon、Feign的區別

服務器端負載均衡 Nginx

Nginx 基於C語言,快速,性能高5w/s。

Redis 5w/s,RibbatMQ 1.2w/s ApacheActiveMQ 0.6w/s 業務系統,kafka 20w~50w/s大數據,Zuul2.0 200w/s

負載均衡、反向代理,代理后端服務器。隱藏真實地址,防火牆,不能外網直接訪問,安全性較高。屬於服務器端負載均衡。既請求由 nginx 服務器端進行轉發。

客戶端負載均衡 Ribbon

Ribbon 是從 eureka 注冊中心服務器端上獲取服務注冊信息列表,緩存到本地,然后在本地實現輪詢負載均衡策略。

既在客戶端實現負載均衡。

應用場景的區別:

Nginx 適合於服務器端實現負載均衡 比如 Tomcat ,Ribbon 適合與在微服務中 RPC 遠程調用實現本地服務負載均衡,比如Dubbo、SpringCloud 中都是采用本地負載均衡。

聲明式web服務客戶端Feign

Feign 是一個聲明web服務客戶端, 這便得編寫web服務客戶端更容易Spring Cloud Netflix 的微服務都是以 HTTP 接口的形式暴露的,所以可以用 Apache 的 HttpClient 或 Spring 的 RestTemplate 去調用,而 Feign 是一個使用起來更加方便的HTTP 客戶端,使用起來就像是調用自身工程的方法,而感覺不到是調用遠程方法。

Feign包含了Ribben,有時候有的項目會2個技術一起用在該項目中是因為Feign是遠程調用的,Ribbon是做負載均衡的。


免責聲明!

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



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