提交 97654b2a 编写于 作者: 武汉红喜's avatar 武汉红喜

sentinel

上级 3d42a21b
......@@ -21,7 +21,8 @@ whatsmars-elasticsearch | Elasticsearch
whatsmars-mq | 消息中间件RocketMQ,Kafka等
whatsmars-netty | NIO框架首选
whatsmars-redis | Redis客户端简单封装
whatsmars-rpc | Transporter & Codec & Serialization
whatsmars-rpc | RPC核心实现
whatsmars-sentinel | 流量控制组件
whatsmars-shardingsphere | 分布式数据库中间件
whatsmars-spring | Spring Framework
whatsmars-spring-boot-samples | Spring Boot Samples
......
......@@ -32,6 +32,7 @@
<module>whatsmars-spring-data</module>
<module>whatsmars-archetypes</module>
<module>whatsmars-netty</module>
<module>whatsmars-sentinel</module>
</modules>
<!--这里的properties会覆盖父pom里的重名的配置-->
......@@ -61,6 +62,7 @@
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<protobuf.version>3.2.0</protobuf.version>
<rocketmq.version>4.5.1</rocketmq.version>
<sentinel.version>1.8.0</sentinel.version>
<zkclient.version>0.9</zkclient.version>
</properties>
......
<?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>whatsmars-parent</artifactId>
<groupId>org.hongxi</groupId>
<version>Rocket.S9</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>whatsmars-sentinel</artifactId>
<packaging>pom</packaging>
<modules>
<module>whatsmars-sentinel-webmvc</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>${sentinel.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>${sentinel.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
<version>${sentinel.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
<?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>whatsmars-sentinel</artifactId>
<groupId>org.hongxi</groupId>
<version>Rocket.S9</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>whatsmars-sentinel-webmvc</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hongxi</groupId>
<artifactId>whatsmars-common</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.hongxi.whatsmars.sentinel.webmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by shenhongxi on 2020/9/15.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package org.hongxi.whatsmars.sentinel.webmvc.config;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.hongxi.whatsmars.common.result.Result;
import org.hongxi.whatsmars.common.result.ResultHelper;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by shenhongxi on 2020/9/15.
*/
@ControllerAdvice
@Order(0)
public class GlobalExceptionHandler {
@ExceptionHandler(BlockException.class)
@ResponseBody
public Result<Void> handleBlockException(BlockException e) {
System.out.println("Blocked by Sentinel: " + e.getRule());
return ResultHelper.newErrorResult(-1, "Blocked by Sentinel");
}
}
package org.hongxi.whatsmars.sentinel.webmvc.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.callback.DefaultBlockExceptionHandler;
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;
/**
* Config sentinel interceptor
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// Add Sentinel interceptor
addSpringMvcInterceptor(registry);
}
private void addSpringMvcInterceptor(InterceptorRegistry registry) {
SentinelWebMvcConfig config = new SentinelWebMvcConfig();
// Depending on your situation, you can choose to process the BlockException via
// the BlockExceptionHandler or throw it directly, then handle it
// in Spring web global exception handler.
// config.setBlockExceptionHandler((request, response, e) -> { throw e; });
// Use the default handler.
config.setBlockExceptionHandler(new DefaultBlockExceptionHandler());
// Custom configuration if necessary
config.setHttpMethodSpecify(true);
// By default web context is true, means that unify web context(i.e. use the default context name),
// in most scenarios that's enough, and it could reduce the memory footprint.
// If set it to false, entrance contexts will be separated by different URLs,
// which is useful to support "chain" relation flow strategy.
// We can change it and view different result in `Resource Chain` menu of dashboard.
config.setWebContextUnify(true);
config.setOriginParser(request -> request.getHeader("S-user"));
// Add sentinel interceptor
registry.addInterceptor(new SentinelWebInterceptor(config)).addPathPatterns("/**");
}
private void addSpringMvcTotalInterceptor(InterceptorRegistry registry) {
//Config
SentinelWebMvcTotalConfig config = new SentinelWebMvcTotalConfig();
//Custom configuration if necessary
config.setRequestAttributeName("my_sentinel_spring_mvc_total_entity_container");
config.setTotalResourceName("my-spring-mvc-total-url-request");
//Add sentinel interceptor
registry.addInterceptor(new SentinelWebTotalInterceptor(config)).addPathPatterns("/**");
}
}
package org.hongxi.whatsmars.sentinel.webmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Controller
public class WebMvcTestController {
@GetMapping("/hello")
@ResponseBody
public String apiHello() {
doBusiness();
return "Hello!";
}
@GetMapping("/err")
@ResponseBody
public String apiError() {
doBusiness();
return "Oops...";
}
@GetMapping("/foo/{id}")
@ResponseBody
public String apiFoo(@PathVariable("id") Long id) {
doBusiness();
return "Hello " + id;
}
@GetMapping("/exclude/{id}")
@ResponseBody
public String apiExclude(@PathVariable("id") Long id) {
doBusiness();
return "Exclude " + id;
}
@GetMapping("/forward")
public ModelAndView apiForward() {
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");
return mav;
}
private void doBusiness() {
Random random = new Random(1);
try {
TimeUnit.MILLISECONDS.sleep(random.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册