为什么需要整合?
因为 当我限流降级或者容错的发生时候 需要自定义自己的处理逻辑 那么就需要进行一个整合处理 。
1 在配置文件当中进行修改 开启feign对Sentinel的支持
feign:
sentinel:
enabled: true
- 创建自定义自己处理逻辑的类
package com.itzhouwei.shop.order.feign.facback;
import com.itzhouwei.shop.domain.Product;
import com.itzhouwei.shop.order.feign.ProductFeignApi;
import org.springframework.stereotype.Component;
@Component
public class ProductFeignFacback implements ProductFeignApi {
@Override
public Product findByPid(Long pid) {
Product cs=new Product();
cs.setPid(pid);
cs.setPprice(1.00);
cs.setPname("自己处理事情");
cs.setStock(1);
return cs;
}
}
- 在feign接口中进行配置自定义的类
package com.itzhouwei.shop.order.feign;
import com.itzhouwei.shop.domain.Product;
import com.itzhouwei.shop.order.feign.facback.ProductFeignFacback;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/****
* 使用FeignApi 调用
* @FeignClient(name = "product-service",fallback = ProductFeignFacback.class) 在该注解里面配置这个 fallback =你自己写的业务逻辑处理类
*/
@FeignClient(name = "product-service",fallback = ProductFeignFacback.class)
public interface ProductFeignApi {
@GetMapping("product")
Product findByPid(@RequestParam("pid") Long pid);
}
4 结果如下