1、主配置文件
server:
port: 8401
spring:
application:
name: cloud-alibaba-sentinel-service
cloud:
#nacos服务注册中心地址
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
#配置sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
exclude: '*'
2、主启动类
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MainApp8401 {
public static void main(String[] args) {
SpringApplication.run(MainApp8401.class, args);
}
}
3、控制器
package com.springcloud.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.TimeUtil;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Time;
import java.util.concurrent.TimeUnit;
@RestController
public class FlowLimitController {
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey")
public String testHotKey(String p1, String p2){
return "---------------testHotKey";
}
public String deal_testHotKey(String p1, String p2, BlockException exception) {
return "---------------testHotKey,/(ㄒoㄒ)/~~";
}
}