SpringCloud:Feign组件之服务调用

x33g5p2x  于2021-10-20 转载在 Spring  
字(3.9k)|赞(0)|评价(0)|浏览(321)

一、Feign概述

通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下

Feign 是一个声明式的 REST 客户端,它用了基于接口的注解方式,很方便实现客户端配置

而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。Feign整合了Ribbon和Hystrix,可以让我们不再需要显式地使用这两个组件

二、快速入门

1.项目结构

  • eureka-server:注册中心
  • eureka-comsumer:服务消费端
  • eureka-client,eureka-provider:服务提供端

2.消费端引入依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

3.定义feign接口

这里feign接口里面的方法要跟服务提供者的接口保存一致

package com.mye.eurekaconsumer.feign;


import com.mye.eurekaconsumer.pojo.Goods;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/** * * feign声明式接口。发起远程调用的。 * String url = "http://FEIGN-PROVIDER/goods/findOne/"+id; Goods goods = restTemplate.getForObject(url, Goods.class); * * 1. 定义接口 * 2. 接口上添加注解 @FeignClient,设置value属性为 服务提供者的 应用名称 * 3. 编写调用接口,接口的声明规则 和 提供方接口保持一致。 * 4. 注入该接口对象,调用接口方法完成远程调用 */
@FeignClient(value = "EUREKA-PROVIDER")
public interface GoodsFeignClient {
    @GetMapping("/goods/findOne/{id}")
    public Goods findOne(@PathVariable("id") int id);
}

4.修改controller

package com.mye.eurekaconsumer.controller;

import com.mye.eurekaconsumer.feign.GoodsFeignClient;
import com.mye.eurekaconsumer.pojo.Goods;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @GetMapping("/goods/{id}")
    public Goods findOne(@PathVariable("id") int id){
        return goodsFeignClient.findOne(id);
    }
}

5.在启动类 添加 @EnableFeignClients 注解,开启Feign功能

package com.mye.eurekaconsumer;

import com.mye.eurekaconsumer.config.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
/* 配置Ribbon的负载均衡策略 name:设置服务提供方的应用名称 configuration:设置负载均衡的Bean */
@RibbonClient(name="EUREKA-PROVIDER",configuration= MySelfRule.class)
@EnableFeignClients//开启Feign的功能
public class EurekaConsumerApplication {

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

}

三、 Feign超时配置

Feign 底层依赖于 Ribbon 实现负载均衡和远程调用。

Ribbon默认1秒超时。

# 设置Ribbon的超时时间
ribbon:
  ConnectTimeout: 1000 # 连接超时时间 默认1s 默认单位毫秒
  ReadTimeout: 3000 # 逻辑处理的超时时间 默认1s 默认单位毫秒

四、Feign日志记录

Feign 只能记录 debug 级别的日志信息

# 设置当前的日志级别 debug,feign只支持记录debug级别的日志
logging:
  level:
    com.mye.eurekaconsumer: debug

定义Feign日志级别Bean

package com.mye.eurekaconsumer.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignLogConfig {
    /* NONE,不记录 BASIC,记录基本的请求行,响应状态码数据 HEADERS,记录基本的请求行,响应状态码数据,记录响应头信息 FULL;记录完成的请求 响应数据 */
    @Bean
    public Logger.Level level(){
        return Logger.Level.FULL;
    }
}

启动bean

/** * * feign声明式接口。发起远程调用的。 * String url = "http://FEIGN-PROVIDER/goods/findOne/"+id; Goods goods = restTemplate.getForObject(url, Goods.class); * * 1. 定义接口 * 2. 接口上添加注解 @FeignClient,设置value属性为 服务提供者的 应用名称 * 3. 编写调用接口,接口的声明规则 和 提供方接口保持一致。 * 4. 注入该接口对象,调用接口方法完成远程调用 */
@FeignClient(value = "EUREKA-PROVIDER", configuration = FeignLogConfig.class)
public interface GoodsFeignClient {
    @GetMapping("/goods/findOne/{id}")
    public Goods findOne(@PathVariable("id") int id);
}

相关文章