微服务之OpenFeign


SpringCloud OpenFiegn官网→官网

1 OpenFeign是什么

Feign是一个声明式 Web 服务客户端。它使编写 Web 服务客户端更容易。要使用 Feign,请创建一个接口并对其进行注释。它具有可插入的注释支持,包括 Feign 注释和 JAX-RS 注释。Feign 还支持可插拔的编码器和解码器。Spring Cloud 添加了对 Spring MVC 注释的支持,并支持使用HttpMessageConvertersSpring Web 中默认使用的注释。Spring Cloud 集成了 Eureka、Spring Cloud CircuitBreaker 以及 Spring Cloud LoadBalancer,在使用 Feign 时提供负载均衡的 http 客户端。

2 使用步骤

2.1 新建cloud-consumer-feign-order

2.2 修改pom.xml文件

<!--OpenFeign组件-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.3 写application.yml文件

server:
  port: 80


#spring cloud 2021必须配datasource
spring:
  application:
    name: cloud-feign-consumer
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    #mysql5.x的没有cj
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root123

eureka:
  client:
    #表示是否将自己注册进Eurekaserver默认为true。
    register-with-eureka: false
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      # defaultZone: http://localhost:7001/eureka  #Eureka服务端的地址(单机版)
      defaultZone: http://eureka-7010.com:7010/eureka, http://eureka-7011.com:7011/eureka/  #Eureka服务端的地址(集群版)

2.4 主启动类

主启动类添加 @EnableFeignClients 注解

@SpringBootApplication
@EnableFeignClients(basePackages = "com.tl.springcloud.service")
public class ConsumerFeignMain {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerFeignMain.class);
    }
}

2.5 编写业务类

2.5.1 业务逻辑接口+@FeignClient配置调用provider服务

@FeignClient("CLOUD-PAYMENT-SERVICE")
public interface PaymentClient {
    @GetMapping("/payment/get/{id}")
    CommonResult getPaymentById(@PathVariable("id") Long id);
}

2.5.2 控制层Controller

@RestController
public class ConsumerFeignController {

    @Autowired
    PaymentClient paymentClient;

    @GetMapping("/consumer/feign/get/{id}")
    public CommonResult getPaymentByFeign(@PathVariable("id") Long id){
        CommonResult payment = paymentClient.getPaymentById(id);
        return payment;
    }
}

2.5 测试

在这里插入图片描述

2.6 总结

在这里插入图片描述

3 超时设置

open feign默认超时时间

public static final int DEFAULT_CONNECTION_TIMEOUT = 2000;
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;

public static class OkHttp {
    /**
     * {@link OkHttpClient} read timeout; defaults to 60 seconds.
     */
    private Duration readTimeout = Duration.ofSeconds(60);
    public Duration getReadTimeout() {
		return readTimeout;
	}
	public void setReadTimeout(Duration readTimeout) {
	    this.readTimeout = readTimeout;
	}
}

超时报错提示

There was an unexpected error (type=Internal Server Error, status=500).
Read timed out executing GET http://CLOUD-PAYMENT-SERVICE/payment/timeout
feign.RetryableException: Read timed out executing GET http://CLOUD-PAYMENT-SERVICE/payment/timeout

超时控制设置

feign:
  client:
    config:
      default:
        connectTimeout: 1000
        readTimeout: 1000
        loggerLevel: basic

4日志增强

4.1 日志增强分类

  1. NONE:没有日志(默认)。
  2. BASIC:只记录请求方法和URL、响应状态码和执行时间。
  3. HEADERS:记录基本信息以及请求和响应标头。
  4. FULL:记录请求和响应的标头、主体和元数据。

4.2 日志级别设置步骤

4.2.1 修改yml文件

logging:
  level:
    com.tl.springcloud.service.PaymentClient: debug

4.2.2 新建Java配置文件

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

4.2.3 测试控制台效果
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值