Feign-Hystrix 使用
1. order 配置开关 feign: hystrix: enabled: true
2. product 注解绑定 @FeignClient(name = "product", fallback = ProductClient.ProductClientFallback.class)
(注意ProductClientFallback类加@Component注解)
3. order 注意包扫描 @ComponentScan(basePackages = "com.imooc")
Product 微服务项目
package com.imooc.product.client; import com.imooc.product.common.DecreaseStockInput; import com.imooc.product.common.ProductInfoOutput; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.List; @FeignClient(name = "product", fallback = ProductClient.ProductClientFallback.class) public interface ProductClient { @PostMapping("/product/listForOrder") List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList); @PostMapping("/product/decreaseStock") void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList); @Component static class ProductClientFallback implements ProductClient { @Override public List<ProductInfoOutput> listForOrder(List<String> productIdList) { return null; } @Override public void decreaseStock(List<DecreaseStockInput> decreaseStockInputList) { } } }
Order 微服务项目
spring: application: name: order cloud: config: discovery: enabled: true service-id: CONFIG profile: test stream: bindings: myMessage: group: order content-type: application/json eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 1000 getProductInfoList: execution: isolation: thread: timeoutInMilliseconds: 3000 feign: hystrix: enabled: true
package com.imooc.order; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; @EnableFeignClients(basePackages = "com.imooc.product.client") //@SpringBootApplication //@EnableDiscoveryClient //@EnableCircuitBreaker @SpringCloudApplication @ComponentScan(basePackages = "com.imooc") public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
- 这里需要包扫描是因为虽然服务降级处理方法代码是写在 product 里的,但是 product 打包后会被 order 识别到,这是就需要包扫描找到它才可以打通。
package com.imooc.order.service.impl; import com.imooc.order.dataobject.OrderDetail; import com.imooc.order.dataobject.OrderMaster; import com.imooc.order.dto.OrderDTO; import com.imooc.order.enums.OrderStatusEnum; import com.imooc.order.enums.PayStatusEnum; import com.imooc.order.enums.ResultEnum; import com.imooc.order.exception.OrderException; import com.imooc.order.repository.OrderDetailRepository; import com.imooc.order.repository.OrderMasterRepository; import com.imooc.order.service.OrderService; import com.imooc.order.utils.KeyUtil; import com.imooc.product.client.ProductClient; import com.imooc.product.common.DecreaseStockInput; import com.imooc.product.common.ProductInfoOutput; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.transaction.Transactional; import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @Service public class OrderServiceImpl implements OrderService { @Autowired private OrderDetailRepository orderDetailRepository; @Autowired private OrderMasterRepository orderMasterRepository; @Autowired private ProductClient productClient; @Override @Transactional public OrderDTO create(OrderDTO orderDTO) { String orderId = KeyUtil.genUniqueKey(); // 查询商品信息(调用商品服务) List<String> productIdList = orderDTO.getOrderDetailList().stream() .map(OrderDetail::getProductId) .collect(Collectors.toList()); // 断点这里看 productInfoList 是否为 null 就知道服务是否降级成功:null 则成功,否则不成功 List<ProductInfoOutput> productInfoList = productClient.listForOrder(productIdList); // 计算总价 //... //扣库存(调用商品服务) //... } }