SpringCloud05:Feign負(fù)載均衡

本系列對(duì)應(yīng)的是尚硅谷周陽(yáng)Spring Cloud的思維導(dǎo)圖整理的筆記,用來(lái)方便自己后面的知識(shí)點(diǎn)回顧。分別以每個(gè)知識(shí)點(diǎn)作為一篇文章詳細(xì)講述。

知識(shí)點(diǎn)傳送門(mén):
項(xiàng)目源碼

一、概述

官網(wǎng)解釋?zhuān)?br> http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

Feign是一個(gè)聲明式WebService客戶(hù)端。使用Feign能讓編寫(xiě)Web Service客戶(hù)端更加簡(jiǎn)單, 它的使用方法是定義一個(gè)接口,然后在上面添加注解,同時(shí)也支持JAX-RS標(biāo)準(zhǔn)的注解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對(duì)Feign進(jìn)行了封裝,使其支持了Spring MVC標(biāo)準(zhǔn)注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負(fù)載均衡。

Feign是一個(gè)聲明式的Web服務(wù)客戶(hù)端,使得編寫(xiě)Web服務(wù)客戶(hù)端變得非常容易,
只需要?jiǎng)?chuàng)建一個(gè)接口,然后在上面添加注解即可。
參考官網(wǎng):https://github.com/OpenFeign/feign

Feign能干什么
Feign旨在使編寫(xiě)Java Http客戶(hù)端變得更容易。
前面在使用Ribbon+RestTemplate時(shí),利用RestTemplate對(duì)http請(qǐng)求的封裝處理,形成了一套模版化的調(diào)用方法。但是在實(shí)際開(kāi)發(fā)中,由于對(duì)服務(wù)依賴(lài)的調(diào)用可能不止一處,往往一個(gè)接口會(huì)被多處調(diào)用,所以通常都會(huì)針對(duì)每個(gè)微服務(wù)自行封裝一些客戶(hù)端類(lèi)來(lái)包裝這些依賴(lài)服務(wù)的調(diào)用。所以,F(xiàn)eign在此基礎(chǔ)上做了進(jìn)一步封裝,由他來(lái)幫助我們定義和實(shí)現(xiàn)依賴(lài)服務(wù)接口的定義。在Feign的實(shí)現(xiàn)下,我們只需創(chuàng)建一個(gè)接口并使用注解的方式來(lái)配置它(以前是Dao接口上面標(biāo)注Mapper注解,現(xiàn)在是一個(gè)微服務(wù)接口上面標(biāo)注一個(gè)Feign注解即可),即可完成對(duì)服務(wù)提供方的接口綁定,簡(jiǎn)化了使用Spring cloud Ribbon時(shí),自動(dòng)封裝服務(wù)調(diào)用客戶(hù)端的開(kāi)發(fā)量。

Feign集成了Ribbon
利用Ribbon維護(hù)了MicroServiceCloud-Dept的服務(wù)列表信息,并且通過(guò)輪詢(xún)實(shí)現(xiàn)了客戶(hù)端的負(fù)載均衡。而與Ribbon不同的是,通過(guò)feign只需要定義服務(wù)綁定接口且以聲明式的方法,優(yōu)雅而簡(jiǎn)單的實(shí)現(xiàn)了服務(wù)調(diào)用

二、Feign使用步驟

1.參考microservicecloud-consumer-dept-80,新建新建microservicecloud-consumer-dept-feign

修改主啟動(dòng)名:DeptConsumer80_Feign_App

2.microservicecloud-consumer-dept-feign工程pom.xml修改,主要添加對(duì)feign的支持

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

3.修改microservicecloud-api工程

1)POM

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

2)新建DeptClientService接口并新增注解@FeignClient

package com.atguigu.springcloud.service;
import java.util.List;

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 com.atguigu.springcloud.entities.Dept;

@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

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

package com.atguigu.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptClientService;

@RestController
public class DeptController_Feign
{
  @Autowired
  private DeptClientService service = null;

  @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);
  }
}

5.microservicecloud-consumer-dept-feign工程修改主啟動(dòng)類(lèi)

package com.atguigu.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;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.springcloud"})
@ComponentScan("com.atguigu.springcloud")
public class DeptConsumer80_Feign_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptConsumer80_Feign_App.class, args);
  }
}

6.測(cè)試

  • 啟動(dòng)3個(gè)eureka集群
  • 啟動(dòng)3個(gè)部門(mén)微服務(wù)8001/8002/8003
  • 啟動(dòng)Feign啟動(dòng)
  • 訪問(wèn)http://localhost/consumer/dept/list
  • Feign自帶負(fù)載均衡配置項(xiàng)

總結(jié):Feign通過(guò)接口的方法調(diào)用Rest服務(wù)(之前是Ribbon+RestTemplate),
該請(qǐng)求發(fā)送給Eureka服務(wù)器(http://MICROSERVICECLOUD-DEPT/dept/list),通過(guò)Feign直接找到服務(wù)接口,由于在進(jìn)行服務(wù)調(diào)用的時(shí)候融合了Ribbon技術(shù),所以也支持負(fù)載均衡作用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評(píng)論 19 139
  • ?通過(guò)前面兩章對(duì)Spring Cloud Ribbon和Spring Cloud Hystrix的介紹,我們已經(jīng)掌...
    Chandler_玨瑜閱讀 214,478評(píng)論 15 140
  • 前言 上兩章節(jié),介紹了下關(guān)于注冊(cè)中心-Eureka的使用及高可用的配置示例,本章節(jié)開(kāi)始,來(lái)介紹下服務(wù)和服務(wù)之間如何...
    oKong閱讀 2,816評(píng)論 1 16
  • 微服務(wù)架構(gòu)模式的核心在于如何識(shí)別服務(wù)的邊界,設(shè)計(jì)出合理的微服務(wù)。但如果要將微服務(wù)架構(gòu)運(yùn)用到生產(chǎn)項(xiàng)目上,并且能夠發(fā)揮...
    java菜閱讀 3,038評(píng)論 0 6
  • 微服務(wù)架構(gòu)模式的核心在于如何識(shí)別服務(wù)的邊界,設(shè)計(jì)出合理的微服務(wù)。但如果要將微服務(wù)架構(gòu)運(yùn)用到生產(chǎn)項(xiàng)目上,并且能夠發(fā)揮...
    程序員技術(shù)圈閱讀 2,832評(píng)論 10 27

友情鏈接更多精彩內(nèi)容