講解Feign 實作負載均衡,熔斷,且它的使用方法是定義一個接口,然後在上面添加注解。不再需要拼接URL,參數等操作。該文章最後還有已經搭建好的項目,免費領取!!
Feign簡介:
Feign是一個聲明式WebService用戶端.使用Feign能讓編寫WebService用戶端更加簡單,它的使用方法是定義一個接口,然後在上面添加注解。不再需要拼接URL,參數等操作,且支援負載均衡,服務熔斷,壓縮請求,和日志配置 ,而且我們再也不需要去拼接我們的請求位址啦,是以說Feign真的是非常的好用啊。
實作:
1、在我們的消費者引入依賴
2、編寫feign用戶端接口
3、消費者啟動類添加支援Feign注解
4、通路測試接口
實作過程:
在消費者pom.xml檔案中引入依賴
<!--配置feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
建立feign用戶端
在消費者中建立UserClient接口:

具體代碼實作:
package com.shenwang.feign.client;
import com.shenwang.domain.User;
import com.shenwang.feign.fallback.UserClientFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author: shenwang
* Date: 2021/7/10
*/
@Service("userClient")
@FeignClient(value = "user-provider")
public interface UserClient {
/**
* 根據id擷取使用者資訊
* @param id
* @return
*/
@RequestMapping(value = "/user/findById/{id}")
User findById(@PathVariable(value = "id") Integer id);
}
注意: FeignClient注解參數value的值對應的是我們提供者application.yml檔案中給的服務的名字,RequestMapping參數路徑要與提供者對該方法的實作的路徑保持一緻
Feign會通過動态代理,幫我們生成實作類。
注解@FeignClient聲明Feign的用戶端,注解value指明服務名稱
接口定義的方法,采用SpringMVC的注解。Feign會根據注解幫我們生成URL位址
注解@RequestMapping中的/user,不要忘記。因為Feign需要拼接可通路位址
然後我們控制層直接引用UserClient 調用接口方法
package com.shenwang.controller;
import com.shenwang.feign.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: shenwang
* Date: 2021/7/8
*/
@RestController
@RequestMapping("/consumer")
public class UserController {
@Autowired
private UserClient userClient;
/**
* 根據id擷取使用者資訊
* @HystrixCommand 如果發生問題,則調用降級處理方法
* @param id
* @return
*/
@GetMapping("findById/{id}")
public Object findById(@PathVariable Integer id){
return userClient.findById(id);
}
}
開啟Feign
在啟動類加上注解@EnableFeignClients
package com.shenwang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* user-consumer 啟動類
* @author shenwang
* @EnbelDiscoveryClient 開啟Eureka用戶端發現功能
* @EnbelCircuitBreaker 開啟熔斷
* @EnbelFeignClients 開啟Feign
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableFeignClients
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
/**
* 将RestTemplate執行個體放到spring容器中
* @LoadBalanced 開啟負載均衡
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
測試:
Feign負載均衡
Feign自身已經內建了Ribbon,是以使用Feign的時候,不需要額外引入依賴。
在application.yml檔案中配置負載均衡
Feign内置的ribbon預設設定了請求逾時時長,預設是1000,可以修改
ribbon内部有重試機制,一旦逾時,會自動重新發起請求。如果不希望重試可以關閉配置:
# 修改服務位址輪詢政策,預設是輪詢,配置之後變随機
user-provider:
ribbon:
#輪詢
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
ConnectTimeout: 10000 # 連接配接逾時時間
ReadTimeout: 2000 # 資料讀取逾時時間
MaxAutoRetries: 1 # 最大重試次數(第一個服務)
MaxAutoRetriesNextServer: 0 # 最大重試下一個服務次數(叢集的情況才會用到)
OkToRetryOnAllOperations: false # 無論是請求逾時或者socket read timeout都進行重試
這樣我們的負載均衡就可以使用啦~
Feign熔斷支援
feign整合Hystrix熔斷器
Feign預設也有對Hystrix的內建!
實作步驟
1. 在配置檔案application.yml中開啟feign熔斷器支援
2. 編寫FallBack處理類,實作FeignClient用戶端
3. 在@FeignClient注解中,指定FallBack處理類。
在配置檔案application.yml中開啟feign熔斷器支援
feign:
hystrix:
enabled: true # 開啟Feign的熔斷功能
熔斷降級類建立
在子工程 消費者(user-consumer)下建立一類 com.shenwang.feign.fallback.UserClientFallBack
再用該類去實作我們剛剛建立的UserClient 具體代碼如下:
package com.shenwang.feign.fallback;
import com.shenwang.domain.User;
import com.shenwang.feign.client.UserClient;
import org.springframework.stereotype.Component;
/**
* 熔斷降級類
* @author: shenwang
* Date: 2021/7/12
*/
@Component
public class UserClientFallBack implements UserClient {
/**
* 服務降級處理方法
* @param id
* @return
*/
@Override
public User findById(Integer id) {
User user=new User();
user.setUsername("fallback 服務降級了.....");
return user;
}
}
指定Fallback處理類
在UserClient @FeignClient注解中 指定FallBack處理類
測試
關閉服務的提供者 再去請求服務 再測試結果:
成功熔斷!!
今天部落格就分享到這裡啦
下面是該項目的連結:
連結:https://pan.baidu.com/s/1NeiI5KOnxhxy3sqE_iyY_g
提取碼:ahua