TestController.java 1.04 KB
package com.meishu.controller;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    // 熔断注解
    @PostMapping("rongduan")
    @CircuitBreaker(name = "orderService", fallbackMethod = "fallback")
    public String createOrder(String orderNo) {
        System.out.println("===== 真正执行业务:用户"  + "提交 =====");

        // 模拟接口很慢(2秒)
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        // 也可以模拟失败
        // throw new RuntimeException("提交失败");

        return "提交成功";
    }

    // 降级方法
    public String fallback(String orderNo, Throwable t) {
        return "服务熔断降级:" + orderNo;
    }

}