Feign客户端-声明式REST调用

Feign的简介

  • Feign是NetFlix的声明式、模板化的HTTP客户端,其灵感来自Retrofit,JAXRS-2.0以及webSocket,Feign可以帮助我们更加便捷的调用HTTP API
  • 在spring cloud中,使用feign非常简单-----创建一个接口,并在接口添加一些注解代码就完成了。feign支持多种注解

在spring-cloud中使用feign:

在这里插入图片描述

接下来我们将在订单微服务为案例,增加对Feign的支持。
步骤如下:
1.导入相应依赖

<!--springboot 整合fegnin客户端-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.创建一个FeignProductClient 的接口

package com.zyc.order.fegin;


import com.zyc.order.entity.Product;
import com.zyc.order.fallback.ProductServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


@FeignClient(value = "service-product",fallback = ProductServiceFallback.class)
public interface FeignProductClient {
    
    

    @GetMapping("/product")
    public List<Product> getAllProduct();

    @GetMapping("/product/{id}")
    public List<Product> getProductById(@PathVariable("id") Integer id);

}

3.Order工程改进(将fallback方法放到类中):
不在方法上使用@HystrixCommand注解

    @Autowired
    private FeignProductClient feignProductClient;

//@HystrixCommand(fallbackMethod = "queryProductFallbackMethod")
    public Order createOrder3(){
    
    
        //1.创建订单对象
        Order order= new Order();
        order.setOid(100010);
        order.setUid(3309);
        //2.1调用商品服务--查询所有商品的信息----存在服务调用的硬编码
        //Product[] productArray = restTemplate.getForObject("http://127.0.0.1:8081/product", Product[].class);
        //2.2通过eureka解决服务硬编码问题
        //Product[] productArray = restTemplate.getForObject("http://service-product/product", Product[].class);
        //order.setProductList(Arrays.asList(productArray));

        //2.3 feign调用
        List<Product> allProduct = feignProductClient.getAllProduct();
        order.setProductList(allProduct);
        //2.返回
        return order;
    }

4.OrderController

package com.zyc.order.controller;


import com.zyc.order.entity.Order;
import com.zyc.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/order")
public class OrderController {
    
    
    @Autowired
    private OrderService orderService;

    @PostMapping
    public Order create() {
    
    
        Order order = orderService.createOrder();
        return order;
    }
    //hystrix
    @PostMapping("/create2")
    public Order create2(){
    
    
        Order order = orderService.createOrder2();

        return order;
    }
    //feign
    @PostMapping("/create3")
    public Order create3() {
    
    
        Order order = orderService.createOrder3();

        return order;
    }
}

5.创建回调类

package com.zyc.order.fallback;


import com.zyc.order.entity.Product;
import com.zyc.order.fegin.FeignProductClient;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
@Component
public class ProductServiceFallback implements FeignProductClient {
    
    
    @Override//查全部产品出问题
    public List<Product> getAllProduct() {
    
    
        return Arrays.asList(new Product(1,"服务出问题了,商品查询错误",new BigDecimal("0.0")));
    }

    @Override//查单个产品出问题
    public List<Product> getProductById(Integer id) {
    
    
        return Arrays.asList(new Product(1,"服务出问题了,商品查询错误",new BigDecimal("0.0")));
    }
}

6.在Feign客户端中添加fallback属性

package com.zyc.order.fegin;


import com.zyc.order.entity.Product;
import com.zyc.order.fallback.ProductServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


@FeignClient(value = "service-product",fallback = ProductServiceFallback.class)
public interface FeignProductClient {
    
    

    @GetMapping("/product")
    public List<Product> getAllProduct();

    @GetMapping("/product/{id}")
    public List<Product> getProductById(@PathVariable("id") Integer id);

}

7.配置文件中开启hystrix

application.yml

#开启hystrix断路器
feign:
  hystrix:
    enabled: true

8.重新启动应用测试
正常情况:
在这里插入图片描述

当将客户端服务器关闭时:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47723535/article/details/109477288