增加服务限流降级熔断功能
This commit is contained in:
@@ -19,5 +19,6 @@
|
||||
<module>zlt-ribbon-spring-boot-starter</module>
|
||||
<module>zlt-auth-client-spring-boot-starter</module>
|
||||
<module>zlt-mq-spring-boot-starter</module>
|
||||
<module>zlt-sentinel-spring-boot-starter</module>
|
||||
</modules>
|
||||
</project>
|
||||
-3
@@ -12,7 +12,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
@@ -21,7 +20,6 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -31,7 +29,6 @@ import java.util.Map;
|
||||
* @author zlt
|
||||
* @date 2018/11/6 11:02
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(RedisRepository.class)
|
||||
@EnableConfigurationProperties({RedisProperties.class, CacheManagerProperties.class})
|
||||
@EnableCaching
|
||||
|
||||
@@ -19,35 +19,14 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>HdrHistogram</artifactId>
|
||||
<groupId>org.hdrhistogram</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>HdrHistogram</artifactId>
|
||||
<groupId>org.hdrhistogram</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
-2
@@ -2,7 +2,6 @@ package com.central.common.ribbon;
|
||||
|
||||
import feign.Logger;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Feign统一配置
|
||||
@@ -10,7 +9,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author zlt
|
||||
* @date 2018/9/18 14:04
|
||||
*/
|
||||
@Configuration
|
||||
public class FeignAutoConfigure {
|
||||
|
||||
/**
|
||||
|
||||
-2
@@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
@@ -24,7 +23,6 @@ import org.springframework.web.client.RestTemplate;
|
||||
* @author zlt
|
||||
* @date 2018/11/17
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(RestTemplateProperties.class)
|
||||
public class RestTemplateAutoConfigure {
|
||||
@Autowired
|
||||
|
||||
-5
@@ -2,10 +2,8 @@ package com.central.common.ribbon;
|
||||
|
||||
import com.central.common.ribbon.config.RestTemplateProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
|
||||
import org.springframework.cloud.netflix.ribbon.DefaultPropertiesFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Ribbon扩展配置类
|
||||
@@ -13,11 +11,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author zlt
|
||||
* @date 2018/11/17 9:24
|
||||
*/
|
||||
@Configuration
|
||||
@EnableCircuitBreaker
|
||||
@EnableConfigurationProperties(RestTemplateProperties.class)
|
||||
public class RibbonAutoConfigure {
|
||||
|
||||
@Bean
|
||||
public DefaultPropertiesFactory defaultPropertiesFactory() {
|
||||
return new DefaultPropertiesFactory();
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
/target/
|
||||
*.iml
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-commons</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>zlt-sentinel-spring-boot-starter</artifactId>
|
||||
<description>服务降级、熔断和限流通用组件</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.central.sentinel.config;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.central.common.model.Result;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Sentinel配置类
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/1/22
|
||||
*/
|
||||
public class SentinelAutoConfigure {
|
||||
public SentinelAutoConfigure() {
|
||||
WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler());
|
||||
}
|
||||
|
||||
/**
|
||||
* 限流、熔断统一处理类
|
||||
*/
|
||||
public class CustomUrlBlockHandler implements UrlBlockHandler {
|
||||
@Override
|
||||
public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
|
||||
Result result = Result.failed("flow-limiting");
|
||||
httpServletResponse.getWriter().print(JSONUtil.toJsonStr(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.sentinel.config.SentinelAutoConfigure
|
||||
-2
@@ -10,7 +10,6 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.ParameterBuilder;
|
||||
@@ -38,7 +37,6 @@ import java.util.stream.Collectors;
|
||||
* @author zlt
|
||||
* @date 2018/11/18 9:22
|
||||
*/
|
||||
@Configuration
|
||||
@Import( {
|
||||
Swagger2Configuration.class
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user