SpringCloud05:Feign負載均衡

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

知識點傳送門:
項目源碼

一、概述

官網(wǎng)解釋:
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服務(wù)客戶端,使得編寫Web服務(wù)客戶端變得非常容易,
只需要創(chuàng)建一個接口,然后在上面添加注解即可。
參考官網(wǎng):https://github.com/OpenFeign/feign

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

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

二、Feign使用步驟

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

修改主啟動名:DeptConsumer80_Feign_App

2.microservicecloud-consumer-dept-feign工程pom.xml修改,主要添加對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工程修改主啟動類

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.測試

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

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

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

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

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