Sentinel 整合学习完成
This commit is contained in:
parent
d50c81ecaa
commit
2ee2961731
@ -1,7 +1,9 @@
|
||||
package com.test.controller;
|
||||
|
||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.test.entity.User;
|
||||
import com.test.entity.UserBorrowDetail;
|
||||
import com.test.service.BorrowService;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@ -10,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
|
||||
/**
|
||||
@ -32,8 +34,15 @@ public class BorrowController {
|
||||
}
|
||||
|
||||
@RequestMapping("/borrow2/{uid}")
|
||||
UserBorrowDetail findUserBorrows2(@PathVariable("uid") int uid) {
|
||||
return service.getUserBorrowDetailByUid(uid);
|
||||
@SentinelResource(value = "findUserBorrows2", blockHandler = "test")
|
||||
UserBorrowDetail findUserBorrows2(@PathVariable("uid") int uid) throws InterruptedException {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
UserBorrowDetail test(int uid, BlockException e) {
|
||||
// 输出降级异常
|
||||
System.out.println(e.getClass());
|
||||
return new UserBorrowDetail(new User(), Collections.emptyList());
|
||||
}
|
||||
|
||||
@RequestMapping("/blocked")
|
||||
@ -60,10 +69,9 @@ public class BorrowController {
|
||||
// return t.getMessage();
|
||||
// }
|
||||
@RequestMapping("/test")
|
||||
@SentinelResource("test") //注意这里需要添加@SentinelResource才可以,用户资源名称就使用这里定义的资源名称
|
||||
String findUserBorrows2(@RequestParam(value = "a", required = false) String a,
|
||||
@RequestParam(value = "b", required = false) String b,
|
||||
@RequestParam(value = "c",required = false) String c) {
|
||||
@SentinelResource("test")
|
||||
//注意这里需要添加@SentinelResource才可以,用户资源名称就使用这里定义的资源名称
|
||||
String findUserBorrows2(@RequestParam(value = "a", required = false) String a, @RequestParam(value = "b", required = false) String b, @RequestParam(value = "c", required = false) String c) {
|
||||
return "请求成功!a = " + a + ", b = " + b + ", c = " + c;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,21 @@
|
||||
package com.test.service.client;
|
||||
|
||||
import com.test.entity.User;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* ClassName: UserClientImpl
|
||||
* Package: com.test.service.client
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/8/17 22:25
|
||||
*/
|
||||
@Component
|
||||
public class UserClientImpl implements UserClient{
|
||||
@Override
|
||||
public User getUserById(int uid) {
|
||||
User user = new User();
|
||||
user.setName("我是替代方案");
|
||||
return user;
|
||||
}
|
||||
}
|
@ -28,3 +28,6 @@ spring:
|
||||
# 关闭Context收敛,这样被监控方法可以进行不同链路的单独控制
|
||||
web-context-unify: false
|
||||
block-page: /blocked
|
||||
feign:
|
||||
circuitbreaker:
|
||||
enabled: true
|
||||
|
Loading…
Reference in New Issue
Block a user