提交 52c11347 编写于 作者: Q qinyingjie

fix:添加备注

上级 73476c30
......@@ -5,6 +5,13 @@ import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* feign的降级
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/14 16:47
*/
@Component
public class ComputeClientHystrix implements ComputeClient {
@Override
......
......@@ -29,7 +29,7 @@ management:
web:
base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
exposure:
include: *
include: "*"
health:
show-details: always #显示健康具体信息 默认不会显示详细信息
......
......@@ -28,8 +28,8 @@ management:
endpoints:
web:
base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
health:
show-details: always #显示健康具体信息 默认不会显示详细信息
exposure:
include: "*"
#mybatis-plus配置
mybatis-plus:
......
......@@ -28,8 +28,8 @@ management:
endpoints:
web:
base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
health:
show-details: always #显示健康具体信息 默认不会显示详细信息
exposure:
include: "*"
#mybatis-plus配置
mybatis-plus:
......
......@@ -35,6 +35,16 @@ public class PortalController {
return portalService.hystrix(id);
}
@GetMapping(value = "/limit/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result limit(@PathVariable String id) {
return portalService.limit(id);
}
/**
* 通过主键查询单条数据
*
......
......@@ -2,6 +2,7 @@ package com.kwan.springcloud.service;
import com.kwan.springcloud.response.Result;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -26,26 +27,46 @@ public class PortalService {
//调用远程的controller
return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
}
@HystrixCommand(fallbackMethod = "addServiceFallback")
public Result hystrix(String id) {
@HystrixCommand(
fallbackMethod = "addServiceFallback",
commandProperties = {
@HystrixProperty(name = "execution.timeout.enabled", value = "true"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "6000")}
)
public Result hystrix(String id) {
//1·服务超时,会降级
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//2.服务异常,会降级
String str = null;
if (str == null) {
throw new RuntimeException("服务异常了.");
}
// try {
// Thread.sleep(6000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// //2.服务异常,会降级
// String str = null;
// if (str == null) {
// throw new RuntimeException("服务异常了.");
// }
return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
}
public Result addServiceFallback(String id) {
log.info("失败id={}", id);
// return Result.error("请求超时 服务不可用 请求失败");
return Result.error("请求异常 服务不可用 请求失败");
}
@HystrixCommand(
fallbackMethod = "addServiceFallback",
threadPoolKey = "goods",
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "2"),
@HystrixProperty(name = "maxQueueSize", value = "1"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "100")
}
)
public Result limit(String id) {
return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
}
}
......@@ -12,14 +12,23 @@ eureka:
service-url:
defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
#开启feign开始hystrix的支持
feign:
hystrix:
enabled: true
feign.hystrix.enabled=true
hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMi
1liseconds=5000
如果hystrix.command.default.execution.timeout.enabled为true,则会有两
个执行方法超时的配置,一个就是ribbon的ReadTimeout,一个就是熔断器
I
hystrix的timeoutInMilliseconds,此时谁的值小谁生效;
如果hystrix.command.default.execution.timeout.enabled为false,则熔断
\ No newline at end of file
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000 #hystrix超时时间
timeout:
enabled: true #开启hystrix超时管理
ribbon:
ReadTimeout: 2000 #请求超时时间
http:
client:
enabled: true #开启ribbon超时管理
ConnectTimeout: 2000 #连接超时时间
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册