update spring-cloud-alibaba to v2.1.2.RELEASE
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<java.version>8</java.version>
|
<java.version>8</java.version>
|
||||||
<spring-cloud-alibaba-dependencies.version>2.1.1.RELEASE</spring-cloud-alibaba-dependencies.version>
|
<spring-cloud-alibaba-dependencies.version>2.1.2.RELEASE</spring-cloud-alibaba-dependencies.version>
|
||||||
<spring-boot-dependencies.version>2.1.12.RELEASE</spring-boot-dependencies.version>
|
<spring-boot-dependencies.version>2.1.12.RELEASE</spring-boot-dependencies.version>
|
||||||
<spring-cloud-dependencies.version>Greenwich.SR5</spring-cloud-dependencies.version>
|
<spring-cloud-dependencies.version>Greenwich.SR5</spring-cloud-dependencies.version>
|
||||||
<commons-collections4.version>4.4</commons-collections4.version>
|
<commons-collections4.version>4.4</commons-collections4.version>
|
||||||
|
|||||||
@@ -40,5 +40,11 @@
|
|||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webflux</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
+43
-12
@@ -1,34 +1,65 @@
|
|||||||
package com.central.sentinel.config;
|
package com.central.sentinel.config;
|
||||||
|
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
|
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
|
||||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
|
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager;
|
||||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
|
||||||
import com.central.common.model.Result;
|
import com.central.common.model.Result;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sentinel配置类
|
* Sentinel配置类
|
||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2019/1/22
|
* @date 2019/1/22
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
public class SentinelAutoConfigure {
|
public class SentinelAutoConfigure {
|
||||||
public SentinelAutoConfigure() {
|
/**
|
||||||
WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler());
|
* 限流、熔断统一处理类
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(HttpServletRequest.class)
|
||||||
|
public static class WebmvcHandler {
|
||||||
|
public WebmvcHandler(SentinelWebMvcConfig config) {
|
||||||
|
config.setBlockExceptionHandler(webmvcBlockExceptionHandler());
|
||||||
|
}
|
||||||
|
|
||||||
|
public BlockExceptionHandler webmvcBlockExceptionHandler() {
|
||||||
|
return (request, response, e) -> {
|
||||||
|
response.setStatus(429);
|
||||||
|
Result result = Result.failed(e.getMessage());
|
||||||
|
response.getWriter().print(JSONUtil.toJsonStr(result));
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 限流、熔断统一处理类
|
* 限流、熔断统一处理类
|
||||||
*/
|
*/
|
||||||
public class CustomUrlBlockHandler implements UrlBlockHandler {
|
@Configuration
|
||||||
@Override
|
@ConditionalOnClass(ServerResponse.class)
|
||||||
public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
|
public static class WebfluxHandler {
|
||||||
Result result = Result.failed("flow-limiting");
|
public WebfluxHandler() {
|
||||||
httpServletResponse.getWriter().print(JSONUtil.toJsonStr(result));
|
WebFluxCallbackManager.setBlockHandler(webfluxBlockExceptionHandler());
|
||||||
|
}
|
||||||
|
|
||||||
|
public BlockRequestHandler webfluxBlockExceptionHandler() {
|
||||||
|
return (exchange, t) ->
|
||||||
|
ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.body(BodyInserters.fromObject(Result.failed(t.getMessage())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ spring.profiles.active=dev
|
|||||||
|
|
||||||
##### nacos(注册中心和配置中心)地址
|
##### nacos(注册中心和配置中心)地址
|
||||||
spring.cloud.nacos.server-addr=192.168.28.130:8848
|
spring.cloud.nacos.server-addr=192.168.28.130:8848
|
||||||
|
spring.cloud.nacos.username=nacos
|
||||||
|
spring.cloud.nacos.password=nacos
|
||||||
spring.cloud.nacos.config.file-extension=yml
|
spring.cloud.nacos.config.file-extension=yml
|
||||||
spring.cloud.nacos.config.shared-dataids=common.yml
|
spring.cloud.nacos.config.shared-dataids=common.yml
|
||||||
spring.cloud.nacos.config.refreshable-dataids=common.yml
|
spring.cloud.nacos.config.refreshable-dataids=common.yml
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ spring:
|
|||||||
- StripPrefix=1
|
- StripPrefix=1
|
||||||
# sentinel:
|
# sentinel:
|
||||||
# datasource.ds1.nacos:
|
# datasource.ds1.nacos:
|
||||||
# server-addr: ${zlt.nacos.server-addr}
|
# server-addr: ${spring.cloud.nacos.server-addr}
|
||||||
# data-id: ${spring.application.name}-sentinel-gw-flow
|
# data-id: ${spring.application.name}-sentinel-gw-flow
|
||||||
# group-id: DEFAULT_GROUP
|
# group-id: DEFAULT_GROUP
|
||||||
# ruleType: gw-flow
|
# ruleType: gw-flow
|
||||||
|
|||||||
@@ -14,14 +14,14 @@
|
|||||||
# 限流
|
# 限流
|
||||||
# ds1:
|
# ds1:
|
||||||
# nacos:
|
# nacos:
|
||||||
# server-addr: ${zlt.nacos.server-addr}
|
# server-addr: ${spring.cloud.nacos.server-addr}
|
||||||
# dataId: ${spring.application.name}-sentinel-gw-flow
|
# dataId: ${spring.application.name}-sentinel-gw-flow
|
||||||
# groupId: DEFAULT_GROUP
|
# groupId: DEFAULT_GROUP
|
||||||
# rule-type: gw-flow
|
# rule-type: gw-flow
|
||||||
# api分组
|
# api分组
|
||||||
# ds2:
|
# ds2:
|
||||||
# nacos:
|
# nacos:
|
||||||
# server-addr: ${zlt.nacos.server-addr}
|
# server-addr: ${spring.cloud.nacos.server-addr}
|
||||||
# dataId: ${spring.application.name}-sentinel-gw-api-group
|
# dataId: ${spring.application.name}-sentinel-gw-api-group
|
||||||
# groupId: DEFAULT_GROUP
|
# groupId: DEFAULT_GROUP
|
||||||
# rule-type: gw-api-group
|
# rule-type: gw-api-group
|
||||||
|
|||||||
Reference in New Issue
Block a user