springcloud--Feign组件

springcloud–Feign组件

Feign声明式服务调用

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

Feign 最初由 Netflix 公司提供,但不支持SpringMVC注解,后由 SpringCloud 对其封装,支持了SpringMVC注
解,让使用者更易于接受。

Feign 快速入门

  1. 在消费端引入 open-feign 依赖
  2. 编写Feign调用接口
  3. 在启动类 添加 @EnableFeignClients 注解,开启Feign功能
  4. 测试调用
  • 定义:是微服务之间通过http协议调用的简化使用的框架
  • 事实:
    • Feign自动集成Ribbon,且默认开启相关功能,所以Fegin的远程调用是基于Ribbon的再封装

1、在消费端引入 open-feign 依赖

<!--feign-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、编写Feign调用接口

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

@FeignClient(value = "FEIGN-PROVIDER")
public interface GoodsFeignClient {
	
    //编写调用接口,接口的声明规则 和 提供方接口保持一致。参数和返回值
    @GetMapping("/goods/findOne/{id}")
    public Goods findGoodsById(@PathVariable("id") int id);

}

3、在启动类 添加 @EnableFeignClients 注解,开启Feign功能

@EnableDiscoveryClient // 激活DiscoveryClient
@EnableEurekaClient
@SpringBootApplication

@EnableFeignClients //开启Feign的功能
public class ConsumerApp {


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

4、测试调用

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    @GetMapping("/goods/{id}")
    public Goods findGoodsById(@PathVariable("id") int id){

        /*
        String url = "http://FEIGN-PROVIDER/goods/findOne/"+id;
        // 3. 调用方法
        Goods goods = restTemplate.getForObject(url, Goods.class);

        return goods;*/

        Goods goods = goodsFeignClient.findGoodsById(id);

        return goods;
    }


}
Fegin----超时设置
  • Feign 底层依赖于 Ribbon 实现负载均衡和远程调用。

  • Ribbon默认1秒超时。

  • 超时配置: 再消费端进行设置,
    当消费端连接服务端 连接时间超过 1s 就抛出异常
    当消费端等待服务端的处理结果超过 1s 就抛出异常

    ribbon:
     ConnectTimeout: 1000 #连接超时时间,毫秒
     ReadTimeout: 1000 #逻辑处理超时时间,毫秒
    
Feign ---- 日志记录
  • Feign 只能记录 debug 级别的日志信息。

    logging:
     level:
     com.xst: debug
    
  • 定义Feign日志级别Bean

    @Configuration
    public class FeignLogConfig {
        /*
            NONE,不记录
            BASIC,记录基本的请求行,响应状态码数据
            HEADERS,记录基本的请求行,响应状态码数据,记录响应头信息
            FULL;记录完成的请求 响应数据
         */
    
        @Bean
        public Logger.Level level(){
            return Logger.Level.FULL;
        }
    }
    
  • 启用该Bean:再Fegin的接口类上 设置configuration属性,值为自己定义的日志级别的类

    @FeignClient(value = "FEIGN-PROVIDER",configuration = FeignLogConfig.class)
    public interface GoodsFeignClient {
    	
       
        @GetMapping("/goods/findOne/{id}")
        public Goods findGoodsById(@PathVariable("id") int id);
    
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值