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

fix:添加备注

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