尚硅谷—Cloud—OpenFeign 服务接口调用(43~46)

1.概述
2.OpenFeign 使用步骤
3.OpenFeign 超时控制
4.OpenFeign 日志打印功能
5.工作原理
 

一:概述


             1)OpenFeign 是什么:(只需创建接口,在接口上添加注解,就可以调用)




             2)能干嘛:


             3)Feign 和 OpenFeign 两者区别:
                           a:OpenFeign 对 Feign 进行了封装,使其 支持了 SpringMVC 注解 和 HttpMessageConverters。
                                    (Feign 可以和 Eureka Ribbon 组合使用,可支持负载均衡)
                           b:图示:             



                                 - 

二:OpenFeign 使用步骤:

             0)OpenFeign 代替 Ribbon + restTemplate 方式,调用接口。
             1)接口+注解:
微服务调用接口 + @FeignClient
             2)新建 module:cloud-consumerFeign-order-80:
             3)POM:(OpenFeign jar包,内部也引入了 Ribbon)

    <dependencies>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
        </dependency>

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


             4)YML:

server:
  port: 80

spring:
  application:
    name: cloud-consumerFeign-order-80

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


             5)主启动类:

@EnableFeignClients
@SpringBootApplication
public class ConsumerFeignOrder80 {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerFeignOrder80.class, args);
    }

}

             6)业务类:
                           a:业务逻辑接口 + @FeignClient 配置 调用 Provider 服务。

                           b:新建 PaymentFeignService 接口,并新增注解 @FeignClient。

@Service
@FeignClient(value = "CLOUD-PROVIDER-PAYMENT")
public interface PaymentFeignService {

    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody Payment payment);

    @GetMapping(value = "/getPayment")
    public CommonResult getPayment(@RequestParam(value = "id") Long id);
    
}


                           c:控制层 Controller。

@Slf4j
@RestController
public class FeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @PostMapping(value = "/create")
    public CommonResult create() {

        Payment payment1 = new Payment();
        payment1.setSerial("feign");
        CommonResult commonResult
                = paymentFeignService.create(payment1);

        return commonResult;
    }

    @GetMapping(value = "/getPayment")
    public CommonResult getPayment() {

        CommonResult commonResult
                = paymentFeignService.getPayment(1L);

        return commonResult;
    }

}

 

             7)测试:(http://127.0.0.1/create

             8)小总结:Feign 自己就集成了 Ribbon ,天生就带 负载均衡 功能。

三:OpenFeign 超时控制:(超过规定时间,默认 1 s)


             1)超时设置,故意设置超时演示出错情况。
                           a:提供者 线程沉睡 3 秒钟

             2)OpenFeign 默认等待 1 秒种,超过后报错。
                           a:默认 OpenFeign 客户端只等待 1秒钟,但是 如果 服务端处理需要 超过 1 秒钟,导致 OpenFeign 客户端直接返回报错。
                           b:为了避免这种情况,有时候我们需要设置 Feign 客户端的 超时控制。


             3)YML 文件里,需要开启 OpenFeign 客户端超时控制。

ribbon:
  # 指的是:建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  # 指的是:建立连接后,从服务器读取到,可用资源所用的时间
  ConnectTimeout: 5000


 

四:OpenFeign 日志打印功能:


             1)日志打印功能:
                           a:可以通过配置,来调整日志级别,从而了解 Feign 中 HTTP 请求细节。
                           b:说白了,就是 对 Feign 接口的调用情况,进行监控和打印。


             2)日志级别:
                           a:NONE:默认的,不显示任何日志
                           b:BASIC:仅记录请求方法,URL,响应状态吗及执行时间
                           c:HEADERS:除了 BASIC 中定义的信息之外,还有请求 和 响应的头 信息。
                           d:FULL:除了 HEADERS 中定义的信息之外,还有 请求和响应的正文,及元数据。


             4)配置日志 Bean:

@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}


             5)YML 文件里,需要开启日志的 Feign 客户端:

logging:
  level:
    com.cloud.feign.PaymentFeignService: debug


             6)后台日志查看:(localhost/getPayment

2021-05-23 20:55:45.971 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] ---> GET http://CLOUD-PROVIDER-PAYMENT/getPayment?id=1 HTTP/1.1
2021-05-23 20:55:45.971 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] ---> END HTTP (0-byte body)
2021-05-23 20:55:47.372 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] <--- HTTP/1.1 200 (1400ms)
2021-05-23 20:55:47.372 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] connection: keep-alive
2021-05-23 20:55:47.373 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] content-type: application/json
2021-05-23 20:55:47.374 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] date: Sun, 23 May 2021 12:55:47 GMT
2021-05-23 20:55:47.374 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] keep-alive: timeout=60
2021-05-23 20:55:47.374 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] transfer-encoding: chunked
2021-05-23 20:55:47.375 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] 
2021-05-23 20:55:47.379 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] {"code":200,"message":"成功111:","data":"Payment(id=1, serial=方法)--提供者 :端口号为 :8001"}
2021-05-23 20:55:47.380 DEBUG 2614 --- [p-nio-80-exec-3] com.cloud.feign.PaymentFeignService      : [PaymentFeignService#getPayment] <--- END HTTP (110-byte body)



 

五.工作原理

             1)工作原理:



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值