SpringCloud H版系列6--OpenFeign服务接口调用

一、OpenFeign

Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:@FeignClient注解。

Feign有可插拔的注解,包括@Feign注解和JAX-RS注解。Feign也支持编码器和解码器,Spring Cloud Open Feign对Feign进行增强支持Spring MVC注解,可以像Spring Web一样使用HttpMessageConverters等。

Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。

功能可插拔的注解支持,包括Feign注解和JAX-RS注解。支持可插拔的HTTP编码器和解码器(Gson,Jackson,Sax,JAXB,JAX-RS,SOAP)。

支持Hystrix和它的Fallback。支持Ribbon的负载均衡。支持HTTP请求和响应的压缩。

灵活的配置:基于 name 粒度进行配置支持多种客户端:JDK URLConnection、apache httpclient、okhttp,ribbon)支持日志支持错误重试url支持占位符可以不依赖注册中心独立运行。

1)name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
value 同name
2): contextId:当name相同时,存在冲突,此时可以通过contextId来处理。
比如:

@FeignClient(value = "${feign.cms.service.name}", name = "${feign.cms.service.name}",contextId = "CmsConfigClient")
public interface CmsConfigClient extends ICmsConfigController {

}

二、cloud-consumer-feign-order80

pom.xml

<!--   引入eureka客户端     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--  open feign      -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

application.yml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false   #是否将自己注册到注册中心,集群必须设置为true配合ribbon
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

#设置feign客户端超时时间,5秒钟
ribbon:
  ReadTimeout: 5000
  ConnectTimeout: 5000
  
# feign日志以什么级别监控哪个接口
logging:
  level:
    com.springcloud.service.PaymentFeignService: debug

主启动类OrderFeignMain80

package com.atguigu.springcloud;

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) {
        /*Feign是一个声明式WebService客户端,使用feign能让编写WebService客户端更加简单,
        *使用方式-定义一个服务接口然后在上面添加注解
        * Feign可以与Eureka和Ribbon结合使用以支持负载均衡
        */
        SpringApplication.run(OrderFeignMain80.class,args);
    }
}

OpenFeign日志增强

openfeign提供了日志打印功能。

Logger有四种类型:NONE(默认)、BASIC、HEADERS、FULL,通过注册Bean来设置日志记录级别

package com.atguigu.springcloud.config;

import feign.Logger;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {
    Logger.Level feignLoggerLevel(){
        return  Logger.Level.FULL;
    }
}

OrderFeignController

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentFeignService;
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.RestController;

@RestController
public class OrderFeignController {
    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
         return paymentFeignService.getPaymentById(id);
    }
    超时控制
    @GetMapping(value = "/consumer/payment/feign/timeout")
    public String paymentFeignTimeOut(){
        //客户端默认等待1秒钟
        return paymentFeignService.paymentFeignTimeOut();
    }
}

PaymentFeignService

package com.atguigu.springcloud.service;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.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") //8001微服务名称
//使用方式:微服务调用接口+注解 即->提供方8001微服务调用接口PaymentService+@FeignClient
public interface PaymentFeignService {
    //这是8001微服务接口地址
    @GetMapping("/payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);

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

三、测试

依次启动7001、7002、8001、8002、OrderFeignMain80。
启动后访问http://eureka7001.com:7001/得到注册的服务状况
在这里插入图片描述

3.1 测试负载均衡

访问http://localhost:80/consumer/payment/get/1会轮询落在8001和8002服务上
在这里插入图片描述
请添加图片描述

3.2 测试超时控制

访问8001,正常访问
在这里插入图片描述
第一次访问http://localhost:80/consumer/payment/feign/timeout,正常
在这里插入图片描述
过几秒钟再访问,已经无法访问
在这里插入图片描述
解决超时方案
yml配置中添加

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

源码下载地址

spring-cloud-01

参考文章
https://blog.csdn.net/weixin_45821811/article/details/117401512

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值