目录
2.新建项目:cloud-consumer-feign-order80
⚪业务逻辑接口 + @FeignClient配置调用provider服务
⚪新建PaymentFeignService接口并新增注解@FeignClient
官网解释:Spring Cloud
feign | opqnfeign |
---|---|
|
|
微服务调用接口 + @FeignClient
feign在消费端使用
- "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>cloud2022artifactId>
- <groupId>com.atxupt.springcloudgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>cloud-consumer-feign-order80artifactId>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-openfeignartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
-
- <dependency>
- <groupId>com.atxupt.springcloudgroupId>
- <artifactId>cloud-api-commonsartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-actuatorartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- project>
- server:
- port: 80
-
- eureka:
- client:
- register-with-eureka: false
- service-url: # 配置服务中心,openFeign去里面找服务
- defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
- @SpringBootApplication
- @EnableFeignClients
- public class OrderFeignMain80 {
- public static void main(String[] args) {
- SpringApplication.run(OrderFeignMain80.class,args);
- }
- }
- @Component
- @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
- public interface PaymentFeignService {
- @GetMapping(value = "/payment/get/{id}")
- public CommonResult
getPaymentById(@PathVariable("id") Long id); - }
- @RestController
- @Slf4j
- public class OrderFeignController {
- @Resource
- private PaymentFeignService paymentFeignService;
-
- @GetMapping(value = "/consumer/payment/get/{id}")
- public CommonResult
getPaymentById(@PathVariable("id") Long id){ - return paymentFeignService.getPaymentById(id);
- }
- }
⭐成功查询(刷新后8001、8002交替出现)
- 服务提供方8001故意写暂停程序
- 服务消费方80添加超时方法PaymentFeignService
- 服务消费方80添加超时方法OrderFeignControIIer
- 测试:http://localhost/consumer/payment/feign/timeout
- 错误页面
- Openfeign默认超时等待为一秒,在消费者里面配置超时时间
- //8001服务提供方
- @GetMapping(value = "/payment/feign/timeout")
- public String paymentFeignTimeout(){
- // 业务逻辑处理正确,但是需要耗费3秒钟
- try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
- return serverPort;
- }
默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。
为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。
- eureka:
- client:
- register-with-eureka: false
- service-url:
- defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
- #设置feign客户端超时时间(OpenFeign默认支持ribbon)
- ribbon:
- #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
- ReadTimeout: 5000
- #指的是建立连接后从服务器读取到可用资源所用的时间
- ConnectTimeout: 5000
Feign 提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节。
即对Feign接口的调用情况进行监控和输出
- import feign.Logger;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class FeignConfig {
- @Bean
- Logger.Level feignLoggerLevel(){
- return Logger.Level.FULL;
- }
- }
- server:
- port: 80
-
- eureka:
- client:
- register-with-eureka: false
- service-url:
- defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
-
- #设置feign客户端超时时间
- #springCloud默认开启支持ribbon
- ribbon:
- #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
- ReadTimeout: 5000
- #指的是建立连接后从服务器读取到可用资源所用的时间
- ConnectTimeout: 5000
-
- logging:
- level:
- # feign日志以什么级别监控哪个接口
- com.atguigu.springcloud.service.PaymentFeignService: debug
-