SpingCloud(H版&alibaba)框架开发教程-25 OpenFeign服务调用、超时处理、日志增强

2020最新版SpringCloud(H版&alibaba)框架开发教程-周阳

cloud-consumer-feign-order80模块

微服务模块

1. 建module

New --> Module --> Maven[Module SDK:1.8.0_191] --> name[cloud-consumer-feign-order80] --> Finish

2. 改pom


<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>cloud2020artifactId>
    <groupId>com.antherd.springcloudgroupId>
    <version>1.0-SNAPSHOTversion>
  parent>
  <modelVersion>4.0.0modelVersion>

  <artifactId>cloud-consumer-feign-order80artifactId>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-openfeignartifactId>
      <version>2.2.1.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    
    <dependency>
      <groupId>com.antherd.springcloudgroupId>
      <artifactId>cloud-api-commonsartifactId>
      <version>${project.version}version>
    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>

3. 写yml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

4. 主启动
新建类:com.antherd.springcloud.OrderFeignMain80

package com.antherd.srpingcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
     

  public static void main(String[] args) {
     

    SpringApplication.run(OrderFeignMain80.class, args);
  }
}

新建类:com.antherd.srpingcloud.service.PaymentFeignService

package com.antherd.srpingcloud.service;

import com.antherd.springcloud.entities.CommonResult;
import com.antherd.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
     

  @GetMapping(value = "/payment/{id}")
  CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}

新建类:com.antherd.srpingcloud.contorller.OrderFeignController

package com.antherd.srpingcloud.contorller;

import com.antherd.springcloud.entities.CommonResult;
import com.antherd.springcloud.entities.Payment;
import com.antherd.srpingcloud.service.PaymentFeignService;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class OrderFeignController {
     

  @Resource
  private PaymentFeignService paymentFeignService;

  @GetMapping("/consumer/payment/{id}")
  public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
     
    return paymentFeignService.getPaymentById(id);
  }
}

5. 启动测试

启动:cloud-eureka-server7001,cloud-eureka-server7002,cloud-provider-payment8001,cloud-provider-payment8002,cloud-consumer-order80五个模块

访问:http://localhost/consumer/payment/1

6. 超时处理

默认Feign客户端只等待一秒钟,但是服务端处理需要超过一秒钟,导致Feign客户端不想等待了,直接返回报错。为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。

下面演示Feign超时报错:
cloud-provider-payment8001、cloud-provider-payment8002模块

com.antherd.springcloud.controller.PaymentController添加新接口:

@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout() throws InterruptedException {
     
  TimeUnit.SECONDS.sleep(3);
  return serverPort;
}

cloud-consumer-feign-order80模块

com.antherd.srpingcloud.service.PaymentFeignService添加新方法:

@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeout();

com.antherd.srpingcloud.contorller.OrderFeignController添加新接口:

@GetMapping(value = "/consumer/payment/feign/timeout")
String paymentFeignTimeout() {
     
  // openfeign-ribbon客户端默认等待一秒钟
  return paymentFeignService.paymentFeignTimeout();
}

访问:http://localhost:8001/payment/feign/timeout、http://localhost/consumer/payment/feign/timeout 看一下效果吧

修改cloud-consumer-feign-order80的yml配置文件,设置Feign客户端的超时控制
添加如下配置:

# 设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
# 指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ConnectTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
  ReadTimeout: 5000

访问:http://localhost/consumer/payment/feign/timeout 看一下效果

7. 日志增强

Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节(对Feign接口的调用情况进行监控和输出

日志级别:

  • NONE:默认的,不显示任何日志
  • BASIC:仅记录请求方法、URL、响应状态码及执行时间
  • HEADERS:除了BASIC中定义的信息之外,还有请求和响应头信息
  • FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据

新建类:com.antherd.srpingcloud.config.FeignConfig

package com.antherd.srpingcloud.config;

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;
  }
}

修改cloud-consumer-feign-order80的yml配置文件
添加如下配置:

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.antherd.springcloud.service.PaymentFeignService: debug

访问:http://localhost/consumer/payment/feign/timeout 看一下效果

你可能感兴趣的:(spring,boot,spring,cloud,spring,cloud,alibaba,feign,openfeign)