init
This commit is contained in:
44
sentinel/sentinel-demo/sentinel-demo-spring-webmvc/pom.xml
Normal file
44
sentinel/sentinel-demo/sentinel-demo-spring-webmvc/pom.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?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>sentinel-demo</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<version>1.8.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sentinel-demo-spring-webmvc</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.1.3.RELEASE</spring.boot.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.spring.webmvc;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* <p>Add the JVM parameter to connect to the dashboard:</p>
|
||||
* {@code -Dcsp.sentinel.dashboard.server=127.0.0.1:8080 -Dproject.name=sentinel-demo-spring-webmvc}
|
||||
*
|
||||
* @author kaizi2009
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class WebMvcDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebMvcDemoApplication.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.spring.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
|
||||
*
|
||||
* @author kaizi2009
|
||||
*/
|
||||
@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("/**");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.spring.webmvc.config;
|
||||
|
||||
import com.alibaba.csp.sentinel.demo.spring.webmvc.vo.ResultWrapper;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Spring configuration for global exception handler.
|
||||
* This will be activated when the {@code BlockExceptionHandler}
|
||||
* throws {@link BlockException directly}.
|
||||
*
|
||||
* @author kaizi2009
|
||||
*/
|
||||
@ControllerAdvice
|
||||
@Order(0)
|
||||
public class SentinelSpringMvcBlockHandlerConfig {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@ExceptionHandler(BlockException.class)
|
||||
@ResponseBody
|
||||
public ResultWrapper sentinelBlockHandler(BlockException e) {
|
||||
logger.warn("Blocked by Sentinel: {}", e.getRule());
|
||||
// Return the customized result.
|
||||
return ResultWrapper.blocked();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.spring.webmvc.controller;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Test controller
|
||||
* @author kaizi2009
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.spring.webmvc.vo;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* @author kaizi2009
|
||||
*/
|
||||
public class ResultWrapper {
|
||||
|
||||
private Integer code;
|
||||
private String message;
|
||||
|
||||
public ResultWrapper(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public static ResultWrapper blocked() {
|
||||
return new ResultWrapper(-1, "Blocked by Sentinel");
|
||||
}
|
||||
|
||||
public String toJsonString() {
|
||||
return JSONObject.toJSONString(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
server.port=10000
|
||||
Reference in New Issue
Block a user