package com.pack.example.Services;
import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class PaymentServices {
/**
* 正常访问,肯定OK
*
* @param id
* @return
*/
public String paymentInfoOK(Integer id) {
return "线程池: " + Thread.currentThread().getName() + " paymentInfoOK,id: " + id + "\t"
+ "O(∩_∩)O哈哈~";
}
/**
* 超时访问,设置自身调用超时的峰值,峰值内正常运行,超过了峰值需要服务降级 自动调用fallbackMethod 指定的方法
* 超时异常或者运行异常 都会进行服务降级
*
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
})
public String paymentInfoTimeOut(Integer id) {
// int age = 10/0;
int second = 3;
long start = System.currentTimeMillis();
try {
TimeUnit.SECONDS.sleep(second);
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end - start);
return "线程池: " + Thread.currentThread().getName() + " paymentInfoTimeOut,id: " + id + "\t"
+ "O(∩_∩)O哈哈~" + " 耗时(秒): " + second;
}
/**
* paymentInfoTimeOut 方法失败后 自动调用此方法 实现服务降级 告知调用者 paymentInfoTimeOut 目前无法正常调用
*
* @param id
* @return
*/
public String paymentInfoTimeOutHandler(Integer id) {
return "线程池: " + Thread.currentThread().getName() + " paymentInfoTimeOutHandler8001系统繁忙或者运行报错,请稍后再试,我是服务提供端,id: " + id + "\t"
+ "o(╥﹏╥)o";
}
/**
* 服务熔断 超时、异常、都会触发熔断
* 1、默认是最近10秒内收到不小于10个请求,<br/>
* 2、并且有60%是失败的<br/>
* 3、就开启断路器<br/>
* 4、开启后所有请求不再转发,降级逻辑自动切换为主逻辑,减小调用方的响应时间<br/>
* 5、经过一段时间(时间窗口期,默认是5秒),断路器变为半开状态,会让其中一个请求进行转发。<br/>
* 5.1、如果成功,断路器会关闭,<br/>
* 5.2、若失败,继续开启。重复4和5<br/>
*
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "paymentCircuitBreakerFallback", commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),/* 是否开启断路器*/
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
// @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")// 超时处理
})
public String paymentCircuitBreaker(Integer id) {
if (id < 0) {
throw new RuntimeException("******id 不能负数");
}
//测试异常
// int age = 10 / 0;
// int second = 3;
// try {
// TimeUnit.SECONDS.sleep(second);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
String serialNumber = IdUtil.simpleUUID();
return Thread.currentThread().getName() + "\t" + "调用成功,流水号: " + serialNumber;
}
/**
* paymentCircuitBreaker 方法的 fallback,<br/>
* 当断路器开启时,主逻辑熔断降级,该 fallback 方法就会替换原 paymentCircuitBreaker 方法,处理请求
*
* @param id
* @return
*/
public String paymentCircuitBreakerFallback(Integer id) {
return Thread.currentThread().getName() + "\t" + "id 不能负数或超时或自身错误,请稍后再试,/(ㄒoㄒ)/~~ id: " + id;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
spring cloud相关的代码
共386个文件
java:115个
class:114个
yml:61个
需积分: 5 0 下载量 35 浏览量
2022-07-10
15:35:36
上传
评论
收藏 292KB RAR 举报
温馨提示
spring cloud相关的代码
资源详情
资源评论
资源推荐
收起资源包目录
spring cloud相关的代码 (386个子文件)
$CACHE_FILE$ 497B
OrderController.class 4KB
PaymentController.class 4KB
Order.class 4KB
Account.class 3KB
CommenResult.class 3KB
CommonResult.class 3KB
CommonResult.class 3KB
CommonResult.class 3KB
CommonResult.class 3KB
CommenResult.class 3KB
Storage.class 3KB
PaymentServices.class 3KB
PaymentServices.class 3KB
configGateway.class 3KB
MyLogGateWayFilter.class 3KB
OrderServiceImpl.class 3KB
flowController.class 3KB
OrderHystrixController.class 2KB
PaymentController.class 2KB
DataSourceProxyConfig.class 2KB
DataSourceProxyConfig.class 2KB
DataSourceProxyConfig.class 2KB
myController.class 2KB
rateLimitController.class 2KB
myfilter.class 2KB
Payment.class 2KB
controllermy.class 2KB
MessageProviderImp.class 2KB
hystrixMain.class 2KB
AccountServiceImpl.class 2KB
feignController.class 2KB
mycontroller.class 2KB
mycontroller.class 2KB
AccountController.class 1KB
myfilter$RequestWrapper$1.class 1KB
StorageServiceImpl.class 1KB
StorageController.class 1KB
OrderController.class 1KB
CircleBreakerController.class 1KB
PaymentController.class 1KB
PaymentController.class 1KB
myfilter$RequestWrapper.class 1KB
SeataStorageService2002Application.class 1KB
SeataAccountService2003Application.class 1KB
SeataOrderService2001Application.class 1KB
CloudalibabaConsumerNacosOrder84Application.class 1KB
PaymentController.class 1KB
clientController.class 1KB
PaymentController.class 1KB
CloudalibabaProviderPayment9002Application.class 1KB
CloudalibabaProviderPayment9001Application.class 1KB
consulController.class 1KB
PaymentController.class 1KB
configBoot.class 1KB
feignMain.class 969B
PaymentFallbackServices.class 961B
customerBlockHandler.class 957B
hystrixMain.class 949B
PaymentServiceImpl.class 943B
bootmain.class 930B
paymentBoot.class 925B
MainStart.class 923B
clientBoot.class 919B
gatewayApp.class 917B
bootmain.class 912B
feignMain.class 909B
bootmain.class 904B
paymentBoot.class 903B
MainStart.class 901B
OrderBoot.class 901B
PaymentServiceImpl.class 888B
PaymentFallbackServiceImpl.class 884B
MyBootMain.class 877B
configClientMain.class 869B
bootMain.class 867B
configController.class 841B
paymentBook.class 836B
bootMain.class 825B
sendMeesgaController.class 824B
mycontroller.class 820B
hystrixMain.class 818B
myController.class 803B
PaymentHystrixService.class 789B
ApplicationContextConfig.class 785B
eurekaMain.class 765B
eurekaMain.class 743B
paymentBook.class 741B
ApplicationContext.class 735B
PaymentService.class 721B
applicaitonConfig.class 715B
applicaitonConfig.class 711B
myconfig.class 695B
feignConfig.class 688B
tets.class 652B
PaymentServices.class 644B
PaymentDao.class 640B
PaymentDao.class 629B
AccountService.class 629B
StorageService.class 629B
共 386 条
- 1
- 2
- 3
- 4
初心魏
- 粉丝: 282
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0