05.SpringCloud OpenFeign (服務介面呼叫)
阿新 • • 發佈:2021-01-01
1.概述
OpenFeign是什麼
Feign是一個宣告式的web服務客戶端,讓編寫web服務客戶端變得容易,只需建立一個介面並在介面上添加註解即可 https://github.com/spring-cloud/spring-cloud-openfeign能幹嘛
Feign和OpenFeign兩者區別
2.OpenFeign使用步驟
核心:介面+註解 微服務呼叫介面+@FeignClient 新建cloud-consumer-feign-order80 Feign在消費端使用 pomyml<?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>springcloud2020</artifactId> <groupId>com.chl.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-feign-order80</artifactId> <!--openfeign--> <dependencies> <!--openfeign的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.chl.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
主啟動類
業務類 業務邏輯介面+@FeignClient配置呼叫provider服務 新建PaymentFeignService介面並新增註解@FeignClient@SpringBootApplication @EnableFeignClients //開啟openfeign的功能 public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class,args); } }
控制層Controller@Component @FeignClient("CLOUD-PAYMENT-SERVICE") //對應的提供方的服務名 public interface PaymentFeignService { //要呼叫提供方的對應介面url 用呼叫本地的service方式呼叫 遠端的服務 @GetMapping("/payment/get/{id}") public CommonResult get(@PathVariable("id")Long id); }
@RestController
@Slf4j
public class OrderFeignController {
@Autowired
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult get(@PathVariable("id")Long id){
return paymentFeignService.get(id);
}
}
測試
先啟動2個eureka叢集7001/7002
再啟動2個微服務8001/8002
啟動OpenFeign order80啟動
http://localhost/consumer/payment/get/1
Feign自帶負載均衡配置項
小總結
3.OpenFeign超時控制
超時設定,故意設定超時演示出錯情況
1.服務提供方8001故意寫暫停程式 PaymentController新加測試方法 /**
* 故意寫的服務提供方吃力業務要3秒,用於測試feign的超時問題
* @return
*/
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout(){
try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
return serverPort;
}
2.服務消費方80新增超時方法PaymentFeignService
//測試feigin的超時問題
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout();
3.服務消費方80新增超時方法OrderFeignController
/**
* 測試feigin的超時問題
* @return
*/
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout(){
return paymentFeignService.paymentFeignTimeout();
}
4.測試
http://localhost/consumer/payment/feign/timeout
原因說明
OpenFeign預設等待一秒鐘,超過後報錯、OpenFeign預設支援Ribbon
超時設定
ribbon:
ReadTimeout: 5000 #請求連線的超時時間 5秒
ConnectTimeout: 5000 #請求處理的超時時間 5秒
4.OpenFeign日誌列印功能
日誌列印功能是什麼
日誌級別
配置日誌bean
在消費端新增配置bean@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
YML檔案裡需要開啟日誌的Feign客戶端
logging:
level:
com.chl.springcloud.service.PaymentFeignService: debug
測試
先啟動2個eureka叢集7001/7002
再啟動2個微服務8001/8002
啟動OpenFeign order80啟動
http://localhost/consumer/payment/get/1
測試結果