Sentinel 整合完成
This commit is contained in:
		@@ -18,6 +18,10 @@
 | 
			
		||||
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
			
		||||
    </properties>
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.alibaba.cloud</groupId>
 | 
			
		||||
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.cloud</groupId>
 | 
			
		||||
            <artifactId>spring-cloud-starter-openfeign</artifactId>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,16 @@
 | 
			
		||||
package com.test.controller;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
 | 
			
		||||
import com.alibaba.fastjson.JSONObject;
 | 
			
		||||
import com.test.entity.UserBorrowDetail;
 | 
			
		||||
import com.test.service.BorrowService;
 | 
			
		||||
import org.springframework.web.bind.annotation.PathVariable;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RequestParam;
 | 
			
		||||
import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -22,8 +26,52 @@ public class BorrowController {
 | 
			
		||||
    @Resource
 | 
			
		||||
    BorrowService service;
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("/borrow/{uid}")
 | 
			
		||||
    UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){
 | 
			
		||||
    @RequestMapping("/borrow1/{uid}")
 | 
			
		||||
    UserBorrowDetail findUserBorrows1(@PathVariable("uid") int uid) {
 | 
			
		||||
        return service.getUserBorrowDetailByUid(uid);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("/borrow2/{uid}")
 | 
			
		||||
    UserBorrowDetail findUserBorrows2(@PathVariable("uid") int uid) {
 | 
			
		||||
        return service.getUserBorrowDetailByUid(uid);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("/blocked")
 | 
			
		||||
    JSONObject blocked() {
 | 
			
		||||
        JSONObject object = new JSONObject();
 | 
			
		||||
        object.put("code", 403);
 | 
			
		||||
        object.put("success", false);
 | 
			
		||||
        object.put("massage", "您的请求频率过快,请稍后再试!");
 | 
			
		||||
        return object;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    @RequestMapping("/test")
 | 
			
		||||
//    @SentinelResource(value = "test", fallback = "except",    //fallback指定出现异常时的替代方案
 | 
			
		||||
//            blockHandler = "blocked",
 | 
			
		||||
////            特别注意这种方式会在没有配置blockHandler的情况下,将Sentinel机制内(也就是限流的异常)的异常也一并处理了,如果配置了blockHandler,那么在出现限流时,依然只会执行blockHandler指定的替代方案(因为限流是在方法执行之前进行的)
 | 
			
		||||
//            exceptionsToIgnore = IOException.class)
 | 
			
		||||
//        //忽略那些异常,也就是说这些异常出现时不使用替代方案
 | 
			
		||||
//    String test() {
 | 
			
		||||
//        throw new RuntimeException("HelloWorld!");
 | 
			
		||||
//    }
 | 
			
		||||
//
 | 
			
		||||
//    //替代方法必须和原方法返回值和参数一致,最后可以添加一个Throwable作为参数接受异常
 | 
			
		||||
//    String except(Throwable t) {
 | 
			
		||||
//        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) {
 | 
			
		||||
        return "请求成功!a = "+a+", b = "+b+", c = "+c;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //模拟慢调用
 | 
			
		||||
    @RequestMapping("/slowCall/{uid}")
 | 
			
		||||
    String slowCall(@PathVariable("uid") int uid) throws InterruptedException {
 | 
			
		||||
        //模拟慢调用
 | 
			
		||||
        Thread.sleep(1000);
 | 
			
		||||
        return "Hello World";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
package com.test.service.impl;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
 | 
			
		||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
 | 
			
		||||
import com.test.entity.Book;
 | 
			
		||||
import com.test.entity.Borrow;
 | 
			
		||||
import com.test.entity.User;
 | 
			
		||||
@@ -12,6 +14,7 @@ import com.test.service.client.UserClient;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
@@ -35,6 +38,7 @@ public class BorrowServiceImpl implements BorrowService {
 | 
			
		||||
    BookClient bookClient;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @SentinelResource(value = "details", blockHandler = "blocked")
 | 
			
		||||
    public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
 | 
			
		||||
        List<Borrow> borrow = mapper.getBorrowsByUid(uid);
 | 
			
		||||
        User user = userClient.getUserById(uid);
 | 
			
		||||
@@ -44,4 +48,9 @@ public class BorrowServiceImpl implements BorrowService {
 | 
			
		||||
                .collect(Collectors.toList());
 | 
			
		||||
        return new UserBorrowDetail(user, bookList);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //替代方案,注意参数和返回值需要保持一致,并且参数最后还需要额外添加一个BlockException
 | 
			
		||||
    public UserBorrowDetail blocked(int uid, BlockException e) {
 | 
			
		||||
        return new UserBorrowDetail(null, Collections.emptyList());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -14,3 +14,17 @@ spring:
 | 
			
		||||
      discovery:
 | 
			
		||||
        # 配置Nacos注册中心地址
 | 
			
		||||
        server-addr: localhost:8848
 | 
			
		||||
        # 将ephemeral修改为false,表示非临时实例(用于持续监控)
 | 
			
		||||
        ephemeral: false
 | 
			
		||||
        cluster-name: Chengdu
 | 
			
		||||
        namespace: dd668135-0bfe-489f-ab2b-24aefb21d156
 | 
			
		||||
    loadbalancer:
 | 
			
		||||
      nacos:
 | 
			
		||||
        enabled: true
 | 
			
		||||
    sentinel:
 | 
			
		||||
      transport:
 | 
			
		||||
        # 添加监控页面地址即可
 | 
			
		||||
        dashboard: localhost:8858
 | 
			
		||||
      # 关闭Context收敛,这样被监控方法可以进行不同链路的单独控制
 | 
			
		||||
      web-context-unify: false
 | 
			
		||||
      block-page: /blocked
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user