1.熔断
(1)jeecg-cloud-gateway 模块中application.yml加入如下配置
# 全局熔断降级配置 default-filters: - name: Hystrix args: name: default #转发地址 fallbackUri: 'forward:/fallback' - name: Retry args: #重试次数,默认值是 3 次 retries: 3 #HTTP 的状态返回码 statuses: BAD_GATEWAY,BAD_REQUEST #指定哪些方法的请求需要进行重试逻辑,默认值是 GET 方法 methods: GET,POST # hystrix 信号量隔离,3秒后自动超时 hystrix: enabled: true shareSecurityContext: true command: default: execution: isolation: strategy: SEMAPHORE thread: timeoutInMilliseconds: 3000
(2)熔断处理器编写
@RestController public class FallbackController { /** * 全局熔断处理 * @return */ @RequestMapping("/fallback") public Mono<String> fallback() { return Mono.just("访问超时,请稍后再试!"); } }
(3)编写测试方法
package org.jeecg.test.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.jeecg.common.api.vo.Result; import org.jeecg.common.system.api.ISysBaseAPI; import org.jeecg.common.system.vo.DictModel; import org.jeecg.test.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.Arrays; import java.util.List; /** * @Author lucky * @Date 2021/10/8 9:15 */ @Slf4j @Api(tags = "test001") @RestController @RequestMapping("/test001") public class TestController { @Resource private TestService testService; @Autowired ISysBaseAPI sysBaseAPI; @GetMapping("/sleep") public Result mocksleep() { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } List<DictModel> list=sysBaseAPI.queryAllDict(); return Result.OK(list); } }
(4)postman测试
访问:http://127.0.0.1:9999/test001/sleep
注意:如果通过网关无法访问到jeecg-cloud-test下面的方法,考虑以下解决方案(1)访问路径是否已经被重复使用,可以修改访问路径(2)重启所有微服务
此时修改测试代码:让睡眠时间为5000ms
Thread.sleep(5000);