简体   繁体   English

Spring Cloud Stream与Hystrix冲突

[英]Spring Cloud Stream conflicts with Hystrix

I have a RestController with Hystrix and Spring Cloud Stream channels: 我有一个带有HystrixSpring Cloud Stream通道的RestController

@RestController
@RequestMapping(value = "/api/products")
@EnableBinding(ProductProcessor.class)
public class ProductUiController {

    /*******
     * LISTENERS
     *******/
    private List<Product> listenAllProducts;
    private Product listenProduct;

    /*******
     * CHANNELS
     *******/
    @Autowired
    @Qualifier(ProductProcessor.OUTPUT_DELETE)
    private MessageChannel channel_delete;
    @Autowired
    @Qualifier(ProductProcessor.OUTPUT_CREATE)
    private MessageChannel channel_create;
    @Autowired
    @Qualifier(ProductProcessor.OUTPUT_UPDATE)
    private MessageChannel channel_update;

    Logger logger = Logger.getLogger(ProductUiController.class);


    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    RabbitAdmin rabbitAdmin;

    @Autowired
    private LoadBalancerClient loadBalancer;

    /**
     * returns all the products in the database
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "getAllProductsFallback")
    public List<Product> getAllProducts() {
        try {
            ServiceInstance instance = loadBalancer.choose("product-service");
            URI uri = instance.getUri();
            URL obj = new URL(uri.toString() + "/products");

            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.getResponseCode();

        } catch (MalformedURLException e) {
            logger.error(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return this.listenAllProducts;
    }

    /**
     * Returns a specific product from product-service
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Product getProduct(@PathVariable("id") Long id) {
        try {
            ServiceInstance instance = loadBalancer.choose("product-service");
            URI uri = instance.getUri();

            URL obj = new URL(uri.toString() + "/products/" + id);

            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.getResponseCode();
        } catch (MalformedURLException e) {
            logger.error(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        System.out.println("RETURNED PRODUCT = " + listenProduct);
        return listenProduct;
    }

    @HystrixCommand(fallbackMethod = "addProductFallback")
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Product addProduct(@RequestBody Product product) {

        logger.info("SENT VIA OUTPUT_CREATE: " + product);
        channel_create.send(MessageBuilder.withPayload(product).build());

        return product;
    }


    @HystrixCommand(fallbackMethod = "removeProductFallback")
    @RequestMapping(method = RequestMethod.DELETE)
    public void removeProduct(@RequestParam("id") Long id) {

        logger.info("SENT VIA OUTPUT_DELETE: " + id);
        this.channel_delete.send(MessageBuilder.withPayload(id).build());
    }


    /**************************************/
    /**********LISTENERS*******************/
    /**************************************/

    /**
     * Receiver for all products
     *
     * @param products
     */
    @StreamListener(ProductProcessor.INPUT_GETALL)
    public void listenerGetAllProducts(List<Product> products) {
        logger.info("RECEIVED FROM INPUT_GETALL: " + products);
        listenAllProducts = products;
    }

    /**
     * Receiver for product
     *
     * @param product
     */
    @StreamListener(ProductProcessor.INPUT_GET)
    public void listenerGetProduct(Product product) {
        logger.info("RECEIVED FROM INPUT_GET: " + product);
        listenProduct = product;
    }


    /**************************************/
    /**********FALLBACK METHODS************/
    /**************************************/

    /**
     * Fallback method for getAllProducts
     *
     * @return
     */
    public List<Product> getAllProductsFallback(Throwable e) {
        logger.info("getAllProductsFallback");
        return listenAllProducts;
    }

    /**
     * Fallback method for addProduct
     *
     * @param product
     * @return
     */
    Product addProductFallback(Product product) {
        logger.info("addProductFallback");
        return null;
    }

    /**
     * Fallback method for delete
     *
     * @param id
     */
    void removeProductFallback(Long id) {
        logger.info("removeProductFallback");
    }

}

In the implementation HystrixCommand makes a getDeclaredMethod to ProductUiController to find the fallback methods but @EnableBinding hides class methods from reflection, any clue of a workaround? 在实施HystrixCommand使得getDeclaredMethodProductUiController找到备用方法,但@EnableBinding隐藏类的方法从反思,一种解决方法的任何线索?

解决方案:将@EnableBinding移到主SpringBootApplication类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM