forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jan 28, 2020
1 parent
b94f5cf
commit 360a9db
Showing
16 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-46-sentinel-demo-nacos</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 SpringMVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对 Nacos 作为配置中心的自动化配置 --> | ||
<dependency> | ||
<groupId>com.alibaba.boot</groupId> | ||
<artifactId>nacos-config-spring-boot-starter</artifactId> | ||
<version>0.2.4</version> | ||
</dependency> | ||
|
||
<!-- Sentinel 核心库 --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-core</artifactId> | ||
<version>1.7.1</version> | ||
</dependency> | ||
<!-- Sentinel 接入控制台 --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-transport-simple-http</artifactId> | ||
<version>1.7.1</version> | ||
</dependency> | ||
<!-- Sentinel 对 SpringMVC 的支持 --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-spring-webmvc-adapter</artifactId> | ||
<version>1.7.1</version> | ||
</dependency> | ||
<!-- <dependency>--> | ||
<!-- <groupId>com.alibaba.csp</groupId>--> | ||
<!-- <artifactId>sentinel-datasource-extension</artifactId>--> | ||
<!-- <version>1.7.1</version>--> | ||
<!-- </dependency>--> | ||
<!-- Sentinel 对 Nacos 作为数据源的支持 --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-nacos</artifactId> | ||
<version>1.7.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
...ntinel-demo-nacos/src/main/java/cn/iocoder/springboot/lab46/sentineldemo/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
// 设置系统属性 project.name,提供给 Sentinel 读取 | ||
System.setProperty("project.name", "demo-application-nacos"); | ||
|
||
// 启动 Spring Boot 应用 | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
.../src/main/java/cn/iocoder/springboot/lab46/sentineldemo/config/SentinelConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo.config; | ||
|
||
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; | ||
import com.alibaba.csp.sentinel.datasource.Converter; | ||
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; | ||
import com.alibaba.nacos.api.PropertyKeyConst; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
@Configuration | ||
public class SentinelConfiguration { | ||
|
||
@Bean | ||
public NacosDataSource nacosDataSource(NacosConfigProperties nacosConfigProperties, ObjectMapper objectMapper) { | ||
Properties properties = new Properties(); | ||
properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosConfigProperties.getServerAddr()); | ||
properties.setProperty(PropertyKeyConst.NAMESPACE, nacosConfigProperties.getNamespace()); | ||
NacosDataSource<List<FlowRule>> nacosDataSource = new NacosDataSource<>(properties, | ||
nacosConfigProperties.getGroup(), nacosConfigProperties.getDataId(), | ||
new Converter<String, List<FlowRule>>() { | ||
@Override | ||
public List<FlowRule> convert(String value) { | ||
try { | ||
return Arrays.asList(objectMapper.readValue(value, FlowRule[].class)); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}); | ||
FlowRuleManager.register2Property(nacosDataSource.getProperty()); | ||
return nacosDataSource; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...src/main/java/cn/iocoder/springboot/lab46/sentineldemo/config/SpringMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo.config; | ||
|
||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor; | ||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebTotalInterceptor; | ||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig; | ||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcTotalConfig; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class SpringMvcConfiguration implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
// Add Sentinel interceptor | ||
addSentinelWebTotalInterceptor(registry); | ||
addSentinelWebInterceptor(registry); | ||
} | ||
|
||
private void addSentinelWebInterceptor(InterceptorRegistry registry) { | ||
// 创建 SentinelWebMvcConfig 对象 | ||
SentinelWebMvcConfig config = new SentinelWebMvcConfig(); | ||
config.setHttpMethodSpecify(true); // 是否包含请求方法。即基于 URL 创建的资源,是否包含 Method。 | ||
// config.setBlockExceptionHandler(new DefaultBlockExceptionHandler()); // 设置 BlockException 处理器。 | ||
|
||
// 添加 SentinelWebInterceptor 拦截器 | ||
registry.addInterceptor(new SentinelWebInterceptor(config)).addPathPatterns("/**"); | ||
} | ||
|
||
private void addSentinelWebTotalInterceptor(InterceptorRegistry registry) { | ||
// 创建 SentinelWebMvcTotalConfig 对象 | ||
SentinelWebMvcTotalConfig config = new SentinelWebMvcTotalConfig(); | ||
|
||
// 添加 SentinelWebTotalInterceptor 拦截器 | ||
registry.addInterceptor(new SentinelWebTotalInterceptor(config)).addPathPatterns("/**"); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...cos/src/main/java/cn/iocoder/springboot/lab46/sentineldemo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/demo") | ||
public class DemoController { | ||
|
||
@GetMapping("/echo") | ||
public String echo() { | ||
return "echo"; | ||
} | ||
|
||
@GetMapping("/test") | ||
public String test() { | ||
return "test"; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...os/src/main/java/cn/iocoder/springboot/lab46/sentineldemo/web/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo.web; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@ControllerAdvice(basePackages = "cn.iocoder.springboot.lab46.sentineldemo.controller") | ||
public class GlobalExceptionHandler { | ||
|
||
@ResponseBody | ||
@ExceptionHandler(value = BlockException.class) | ||
public String blockExceptionHandler(BlockException blockException) { | ||
return "请求过于频繁"; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
lab-46/lab-46-sentinel-demo-nacos/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
nacos: | ||
# Nacos 配置中心的配置项,对应 NacosConfigProperties 配置类 | ||
config: | ||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址 | ||
bootstrap: | ||
enable: true # 是否开启 Nacos 配置预加载功能。默认为 false。 | ||
log-enable: true # 是否开启 Nacos 支持日志级别的加载时机。默认为 false。 | ||
data-id: example-sentinel # 使用的 Nacos 配置集的 dataId。 | ||
type: JSON # 使用的 Nacos 配置集的配置格式。默认为 PROPERTIES。 | ||
group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP。 | ||
namespace: # 使用的 Nacos 的命名空间,默认为 null。 |
1 change: 1 addition & 0 deletions
1
lab-46/lab-46-sentinel-demo-nacos/src/main/resources/sentinel.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
csp.sentinel.dashboard.server=127.0.0.1:7070 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-46-sentinel-demo</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 SpringMVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- Sentinel 核心库 --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-core</artifactId> | ||
<version>1.7.1</version> | ||
</dependency> | ||
<!-- Sentinel 接入控制台 --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-transport-simple-http</artifactId> | ||
<version>1.7.1</version> | ||
</dependency> | ||
<!-- Sentinel 对 SpringMVC 的支持 --> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-spring-webmvc-adapter</artifactId> | ||
<version>1.7.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
...-46-sentinel-demo/src/main/java/cn/iocoder/springboot/lab46/sentineldemo/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
// 设置系统属性 project.name,提供给 Sentinel 读取 | ||
System.setProperty("project.name", "demo-application"); | ||
|
||
// 启动 Spring Boot 应用 | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...src/main/java/cn/iocoder/springboot/lab46/sentineldemo/config/SpringMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo.config; | ||
|
||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor; | ||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebTotalInterceptor; | ||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig; | ||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcTotalConfig; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class SpringMvcConfiguration implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
// Add Sentinel interceptor | ||
// addSentinelWebTotalInterceptor(registry); | ||
addSentinelWebInterceptor(registry); | ||
} | ||
|
||
private void addSentinelWebInterceptor(InterceptorRegistry registry) { | ||
// 创建 SentinelWebMvcConfig 对象 | ||
SentinelWebMvcConfig config = new SentinelWebMvcConfig(); | ||
config.setHttpMethodSpecify(true); // 是否包含请求方法。即基于 URL 创建的资源,是否包含 Method。 | ||
// config.setBlockExceptionHandler(new DefaultBlockExceptionHandler()); // 设置 BlockException 处理器。 | ||
|
||
// 添加 SentinelWebInterceptor 拦截器 | ||
registry.addInterceptor(new SentinelWebInterceptor(config)).addPathPatterns("/**"); | ||
} | ||
|
||
private void addSentinelWebTotalInterceptor(InterceptorRegistry registry) { | ||
// 创建 SentinelWebMvcTotalConfig 对象 | ||
SentinelWebMvcTotalConfig config = new SentinelWebMvcTotalConfig(); | ||
|
||
// 添加 SentinelWebTotalInterceptor 拦截器 | ||
registry.addInterceptor(new SentinelWebTotalInterceptor(config)).addPathPatterns("/**"); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...emo/src/main/java/cn/iocoder/springboot/lab46/sentineldemo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/demo") | ||
public class DemoController { | ||
|
||
@GetMapping("/echo") | ||
public String echo() { | ||
return "echo"; | ||
} | ||
|
||
@GetMapping("/test") | ||
public String test() { | ||
return "test"; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...mo/src/main/java/cn/iocoder/springboot/lab46/sentineldemo/web/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab46.sentineldemo.web; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@ControllerAdvice(basePackages = "cn.iocoder.springboot.lab46.sentineldemo.controller") | ||
public class GlobalExceptionHandler { | ||
|
||
@ResponseBody | ||
@ExceptionHandler(value = BlockException.class) | ||
public String blockExceptionHandler(BlockException blockException) { | ||
return "请求过于频繁"; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
lab-46/lab-46-sentinel-demo/src/main/resources/sentinel.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
csp.sentinel.dashboard.server=127.0.0.1:7070 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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> | ||
<artifactId>labs-parent</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-46</artifactId> | ||
<packaging>pom</packaging> | ||
<modules> | ||
<module>lab-46-sentinel-demo</module> | ||
<module>lab-46-sentinel-demo-nacos</module> | ||
</modules> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters