当前位置:网站首页>Create a time blocker yourself
Create a time blocker yourself
2022-06-26 18:26:00 【[email protected]】
One , Create a class to implement the interface

Directory structure

Class inheritance interface
package com.jt.provider.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
/**
* Spring MVC Interceptor object in ,
* This object can be used when you execute the target Controller Method is executed before
*/
public class TimeInterceptor implements HandlerInterceptor {
/**
* This method performs on your goal Controller Method is executed before
* @return Return value true To release , Can continue to execute request Follow up business
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("===preHandle===");
// Business : Allow in 8 After the point , On the evening of 9 Access before
LocalTime now = LocalTime.now();// Get the current time ,
int hour = now.getHour();// Gets the hour unit of the current time
if(hour<11||hour>=21)
throw new RuntimeException(" Please visit at the specified time 11~21");
return true;
}
}
Two , Register interceptors
Create a class to implement the interface
package com.jt;
import com.jt.provider.interceptor.TimeInterceptor;
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 SpringWebConfig implements WebMvcConfigurer {
// Register interceptors
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TimeInterceptor())
.addPathPatterns("/provider/sentinel01");
}
}
Intercept path
package com.jt.provider.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.jt.provider.service.ResourceService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
@RestController
@RequestMapping("/provider")
public class ProviderSentinelController {
/**
* Current limiting : Completed
* expand : Limit the access time of this method through a design ?( for example 12306 Booking for , Tickets can only be booked at the specified time )
* What's your plan ? be based on springmvc Interceptors in (15:10 Explain )
* @return
*/
@GetMapping("/sentinel01")
public String doSentinel01(){
return "sentinel 01 test-------";
}3、 ... and , stay pom.xml Add to file sentinel rely on
<!--sentinel rely on , After adding this dependency , An interceptor object will be added to the project , This object will be used for
Requests to this service , To intercept , After the request is intercepted, it will be connected with sentinel Compare the current limit rules defined on the console ,
If it is within the allowable range , Continue to visit , Otherwise, conduct current limiting or degrading operations -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>Four , test
1, Set the specified time access

2, Access in browser

3, Look at the background output

版权声明
本文为[[email protected]@yxg]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261820088925.html
边栏推荐
猜你喜欢

LeetCode 238 除自身以外数组的乘积

Introduction to Ethereum Technology Architecture

Boyun, standing at the forefront of China's container industry

50 lines of code to crawl TOP500 books and import TXT documents

idea中文插件chinese(simplified) language pack

Plt How to keep show() not closed

Vscode 基础必备 常用插件

【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)

零时科技 | 智能合约安全系列文章之反编译篇

CD-CompactDisk
随机推荐
Paging query and join Association query optimization
Get and set settings in 26class
LeetCode 128最长连续序列
[unity] use C in unity to execute external files, such as Exe or bat
【Mysql系列】工作常用sql集锦(持续更新)
Request method 'POST' not supported
A little experience of next (ITER (dataloader))
成功解决之Jenkins报错:The goal you specified requires a project to execute but there is no POM
Ethereum技术架构介绍
ARM裸板调试之串口打印及栈初步分析
Enter n integers and output the number of occurrences greater than or equal to half the length of the array
transforms. The input of randomcrop() can only be PIL image, not tensor
Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
Résumé des points de connaissance
Numpy's Matplotlib
How to create and enforce indexes
Conditional compilation in precompiling instructions
图像二值化处理
微信小程序 自定义 弹框组件
The cross compilation environment appears So link file not found problem