Feign声明式服务调用

2021-01-14  本文已影响0人  o_O小薯条

Feign

Feign快速入门

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

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

2. 编写Feign调用接口

package com.itheima.consumer.feign;

import com.itheima.consumer.domain.Goods;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * fegin声明式接口,发起远程调用的
 * 简化 String url = "http://FEIGN_PROVIDER/goods/findOne/" + id;
 * Goods goods = restTemplate.getForObject(url, Goods.class)
 *
 * 1. 定义接口
 * 2.接口上添加注解 @FeginClent, 设置value属性为 服务提供者的应用名称
 *
 */
@FeginClient(value = "FEIGN_PROVIDER")
public interface GoodsFeignClient {
    @GetMapping("goods/findOne/{id}")
    public Goods findGoodsById(@PathVariable("id") int id);
}

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

@EnableFeginClients //开启Feign的功能

4. 测试调用

@Autowired
    private GoodsFeignClient goodsFeignClient;
    @GetMapping("/goods/{id}")
    public Goods findGoodsById(@PathVariable("id") int id) {
        return goodsFeignClient.findGoodsById(id);
    }

Feign其他功能 - 超时设置

ribbon:
      connectTimeout: 1000 #链接超市时间 毫秒
      ReadTimeout: 1000 #逻辑处理超时时间 毫秒

Feign其他功能 - 日志记录

 logging:
      level:
          com.xxxx: debug
/**
* NONE,不记录
*BASIC,记录基本的请求行,响应状态码数据
*HEADERS,记录基本的请求行,响应状态码数据,记录响应头信息
*FULL,记录完整的请求响应数据
*/
@Bean
public Logger.Level feignLoggerLevel() {
      return Logger.Level.FULL;
}
@FeignClient(configuration = XxxConfig.class)
上一篇 下一篇

猜你喜欢

热点阅读