feign入门案例

1.引入依赖

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

2.配置调用接口

@FeignClient(name="service-product")
public interface ProductFeignClient {

    /*
    *配置需要调用的微服务接口
    * */
    @RequestMapping(value = "/product/{id}",method = RequestMethod.GET)
    public Product findById(@PathVariable("id") Long id);

}

3.激活feign @EnableFeignClients

@SpringBootApplication
@EntityScan("com.yxkj.eneity")
@EnableFeignClients
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }
}

4.测试调用远程微服务


    @Autowired
    private ProductFeignClient productFeignClient;

    @RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
    public Product findById(@PathVariable Long id){
        Product product = null;
        product = productFeignClient.findById(id);
        return product;
    }

5.配置feign日志的输出

#配置feign日志的输出
#日志配置 NONE:不输出日志(高) BASIC:使用于生产环境追踪问题;
#HEADERS:在BASIC的基础上,记录请求和响应头信息 FULL:记录所有
feign:
  client:
    config:
      service-product: #需要调用服务的名称
        loggerLevel: FULL
logging:
  level:
    com.yxkj.feign.ProductFeignClient: debug
posted @ 2021-07-01 18:13  风飘落叶  阅读(187)  评论(0编辑  收藏  举报