feat(openFeign): 自定义限流返回内容
This commit is contained in:
parent
2085c6fd50
commit
6cb44520c3
|
@ -36,6 +36,7 @@ public class Result<T> {
|
|||
*/
|
||||
public static <T> Result<T> build(T body, ResultCodeEnum codeEnum) {
|
||||
Result<T> result = build(body);
|
||||
result.setData(body);
|
||||
result.setCode(codeEnum.getCode());
|
||||
result.setMessage(codeEnum.getMessage());
|
||||
return result;
|
||||
|
@ -53,7 +54,7 @@ public class Result<T> {
|
|||
Result<T> result = build(body);
|
||||
result.setCode(code);
|
||||
result.setMessage(message);
|
||||
result.setData(null);
|
||||
result.setData(body);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.alibaba.cloud.nacos.NacosConfigManager;
|
|||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import feign.Logger;
|
||||
import feign.Retryer;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
@ -51,4 +52,13 @@ public class CloudConfiguration {
|
|||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试机制
|
||||
*/
|
||||
@Bean
|
||||
public Retryer retryer() {
|
||||
// 使用默认重试机制,根据之前设置的值配置有关
|
||||
return new Retryer.Default();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package cn.bunny.config;
|
||||
|
||||
import cn.bunny.dao.vo.result.Result;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
@Component
|
||||
public class CustomerBlockExceptionHandler implements BlockExceptionHandler {
|
||||
/**
|
||||
* 自定义限流返回内容
|
||||
*/
|
||||
@Override
|
||||
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s, BlockException e) throws Exception {
|
||||
httpServletResponse.setContentType("application/json;charset=utf-8");
|
||||
Result<Object> result = Result.error(null, 429, "太快了...");
|
||||
|
||||
try (PrintWriter writer = httpServletResponse.getWriter()) {
|
||||
Object json = JSON.toJSON(result);
|
||||
writer.println(json);
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,7 +5,6 @@ spring:
|
|||
- nacos:database.yml?group=order
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
|
@ -5,7 +5,6 @@ spring:
|
|||
- nacos:database.yml?group=order
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
|
@ -5,7 +5,6 @@ spring:
|
|||
- nacos:database.yml?group=order
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
|
@ -7,7 +7,10 @@ spring:
|
|||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.3.132
|
||||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
namespace: ${spring.profiles.active:public} # 后面是默认 namespace
|
||||
openfeign:
|
||||
client:
|
||||
|
@ -28,3 +31,10 @@ spring:
|
|||
# 压缩响应请求
|
||||
response:
|
||||
enabled: true
|
||||
sentinel:
|
||||
transport:
|
||||
dashboard: 192.168.3.132:8858
|
||||
eager: true # 项目启动就连接
|
||||
feign:
|
||||
sentinel:
|
||||
enabled: true
|
|
@ -13,3 +13,29 @@ spring:
|
|||
config:
|
||||
import-check:
|
||||
enabled: false
|
||||
openfeign:
|
||||
client:
|
||||
config:
|
||||
service-cloud1: # 针对自己的这个配置
|
||||
logger-level: full
|
||||
connect-timeout: 3000
|
||||
read-timeout: 5000
|
||||
default: # 针对默认配置
|
||||
logger-level: full
|
||||
connect-timeout: 3000
|
||||
read-timeout: 5000
|
||||
compression:
|
||||
# 压缩请求
|
||||
request:
|
||||
enabled: true
|
||||
min-request-size: 4096 # 压缩最小值 为4096 = 4KB
|
||||
# 压缩响应请求
|
||||
response:
|
||||
enabled: true
|
||||
sentinel:
|
||||
transport:
|
||||
dashboard: 192.168.3.132:8858
|
||||
eager: true # 项目启动就连接
|
||||
feign:
|
||||
sentinel:
|
||||
enabled: true
|
|
@ -62,6 +62,11 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<!-- knife4j -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- alibaba-discovery -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
@ -77,14 +82,15 @@
|
|||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<!-- openfeign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!-- knife4j -->
|
||||
<!-- sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue