1. 程式人生 > 其它 >2020-12-15 Feign負載均衡

2020-12-15 Feign負載均衡

技術標籤:Java# springcloud

十、Feign負載均衡

10.1簡介

feign是宣告式的web service客戶端,它讓微服務之間的呼叫變得更簡單了,類似controller呼叫service。SpringCloud集成了Ribbon和Eureka,可在使用Feign時提供負載均衡的http客戶端。
只需要建立一個介面,然後添加註解 即可!

feign,主要是社群,大家都習慣面向介面程式設計。這個是很多開發人員的規範。呼叫微服務訪問兩種方法

  1. 微服務名字【ribbon】
  2. 介面和註解【feign】
Feign能幹什麼
  • Feign旨在使編寫Java Http客戶端變得更容易
  • 前面在使用Ribbon + RestTemplate時,利用RestTemplate對Http請求的封裝處理,形成了一套模板化的呼叫方法。但是在實際開發中,由於對服務依賴的呼叫可能不止一處,往往一個介面會被多處呼叫,所以通常都會針對每個微服務自行封裝一些客戶端類來包裝這些依賴服務的呼叫。所以,Feign在此基礎上做了進一步封裝,由他來幫助我們定義和實現依賴服務介面的定義,在Feign的實現下,我們只需要建立一個介面並使用註解的方式來配置它(類似於以前Dao介面上標註Mapper註解,現在是一個微服務介面上面標註一個Feign註解即可。)即可完成對服務提供方的介面繫結,簡化了使用Spring Cloud Ribbon時,自動封裝服務呼叫客戶端的開發量。
  • Feign集成了Ribbon
  • 利用Ribbon維護了MicroServiceCloud-Dept的服務列表資訊,並且通過輪詢實現了客戶端的負載均衡,而與Ribbon不同的是,通過Feign只需要定義服務繫結介面且以宣告式的方法,優雅而且簡單的實現了服務呼叫。

10.2.Feign使用步驟

1. 在springcloud-api專案下新建service層,和pojo同級
2. 複製springcloud-consumer-dept-80專案且命名為springcloud-consumer-dept-feign
  • 刪除MyRule資料夾
  • 修改啟動類名稱為FeignDeptConsumer_80
3.在API專案和feign的專案的pom檔案中新增Feign依賴
 		<dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-feign</artifactId>
             <version>1.4.6.RELEASE</version>
         </dependency>
3. 編寫剛才新建的DeptClientService 介面
package com.buba.
springcloud.service; import com.buba.springcloud.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @Component @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT") public interface DeptClientService { @GetMapping("/dept/get/{id}") public Dept queryById(@PathVariable("id") Long id); @GetMapping("/dept/list") public Dept queryAll(); @PostMapping("/dept/add") public Dept addDept(Dept dept); }
4.修改springcloud-consumer-dept-feign中controller層中的DeptConsumerController
package com.buba.springcloud.controller;

import com.buba.springcloud.pojo.Dept;
import com.buba.springcloud.service.DeptClientService;
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 java.util.List;

@RestController
public class DeptConsumerController {

   @Autowired
   private DeptClientService deptClientService=null;

   @RequestMapping("/consumer/dept/get/{id}")
   public Dept get(@PathVariable("id") Long id){
       return this.deptClientService.queryById(id);
   }
   @RequestMapping("/consumer/dept/add")
   public Boolean add(Dept dept){
       return this.deptClientService.addDept(dept);
   }
   @RequestMapping("/consumer/dept/list")
   public List<Dept> list(){
       return this.deptClientService.queryAll();
   }
}

5.修改springcloud-consumer-dept-feign啟動類
package com.buba.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

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

注意:這裡我使用這個註解的時候報錯
在這裡插入圖片描述
不用管,專案可以正常執行,上百度查,說這是因為idea的拼寫檢查,可以忽視的.

6.啟動測試
  • 可以看到使用Feign也可以實現負載均衡
  • 測試成功!