Sentinel 整合完成
This commit is contained in:
		@@ -2,6 +2,7 @@
 | 
				
			|||||||
  <configuration default="false" name="BookApplication-01" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="图书服务">
 | 
					  <configuration default="false" name="BookApplication-01" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="图书服务">
 | 
				
			||||||
    <envs>
 | 
					    <envs>
 | 
				
			||||||
      <env name="server.port" value="8201" />
 | 
					      <env name="server.port" value="8201" />
 | 
				
			||||||
 | 
					      <env name="spring.cloud.nacos.discovery.cluster-name" value="Chongqing" />
 | 
				
			||||||
    </envs>
 | 
					    </envs>
 | 
				
			||||||
    <module name="book-service" />
 | 
					    <module name="book-service" />
 | 
				
			||||||
    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BookApplication" />
 | 
					    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BookApplication" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@
 | 
				
			|||||||
  <configuration default="false" name="BookApplication-02" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="图书服务">
 | 
					  <configuration default="false" name="BookApplication-02" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="图书服务">
 | 
				
			||||||
    <envs>
 | 
					    <envs>
 | 
				
			||||||
      <env name="server.port" value="8202" />
 | 
					      <env name="server.port" value="8202" />
 | 
				
			||||||
 | 
					      <env name="spring.cloud.nacos.discovery.cluster-name" value="Chengdu" />
 | 
				
			||||||
    </envs>
 | 
					    </envs>
 | 
				
			||||||
    <module name="book-service" />
 | 
					    <module name="book-service" />
 | 
				
			||||||
    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BookApplication" />
 | 
					    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.BookApplication" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@
 | 
				
			|||||||
  <configuration default="false" name="UserApplication-01" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="用户服务">
 | 
					  <configuration default="false" name="UserApplication-01" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="用户服务">
 | 
				
			||||||
    <envs>
 | 
					    <envs>
 | 
				
			||||||
      <env name="server.port" value="8101" />
 | 
					      <env name="server.port" value="8101" />
 | 
				
			||||||
 | 
					      <env name="spring.cloud.nacos.discovery.cluster-name" value="Chongqing" />
 | 
				
			||||||
    </envs>
 | 
					    </envs>
 | 
				
			||||||
    <module name="user-service" />
 | 
					    <module name="user-service" />
 | 
				
			||||||
    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
 | 
					    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@
 | 
				
			|||||||
  <configuration default="false" name="UserApplication-02" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="用户服务">
 | 
					  <configuration default="false" name="UserApplication-02" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" folderName="用户服务">
 | 
				
			||||||
    <envs>
 | 
					    <envs>
 | 
				
			||||||
      <env name="server.port" value="8102" />
 | 
					      <env name="server.port" value="8102" />
 | 
				
			||||||
 | 
					      <env name="spring.cloud.nacos.discovery.cluster-name" value="Chengdu" />
 | 
				
			||||||
    </envs>
 | 
					    </envs>
 | 
				
			||||||
    <module name="user-service" />
 | 
					    <module name="user-service" />
 | 
				
			||||||
    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
 | 
					    <option name="SPRING_BOOT_MAIN_CLASS" value="com.test.UserApplication" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,10 @@
 | 
				
			|||||||
    </properties>
 | 
					    </properties>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <dependencies>
 | 
					    <dependencies>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>com.alibaba.cloud</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
        <dependency>
 | 
					        <dependency>
 | 
				
			||||||
            <groupId>com.alibaba.cloud</groupId>
 | 
					            <groupId>com.alibaba.cloud</groupId>
 | 
				
			||||||
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 | 
					            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,3 +14,8 @@ spring:
 | 
				
			|||||||
      discovery:
 | 
					      discovery:
 | 
				
			||||||
        # 配置Nacos注册中心地址
 | 
					        # 配置Nacos注册中心地址
 | 
				
			||||||
        server-addr: localhost:8848
 | 
					        server-addr: localhost:8848
 | 
				
			||||||
 | 
					        namespace: dd668135-0bfe-489f-ab2b-24aefb21d156
 | 
				
			||||||
 | 
					    sentinel:
 | 
				
			||||||
 | 
					      transport:
 | 
				
			||||||
 | 
					        # 添加监控页面地址即可
 | 
				
			||||||
 | 
					        dashboard: localhost:8858
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,10 @@
 | 
				
			|||||||
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
					        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
				
			||||||
    </properties>
 | 
					    </properties>
 | 
				
			||||||
    <dependencies>
 | 
					    <dependencies>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>com.alibaba.cloud</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
        <dependency>
 | 
					        <dependency>
 | 
				
			||||||
            <groupId>org.springframework.cloud</groupId>
 | 
					            <groupId>org.springframework.cloud</groupId>
 | 
				
			||||||
            <artifactId>spring-cloud-starter-openfeign</artifactId>
 | 
					            <artifactId>spring-cloud-starter-openfeign</artifactId>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,12 +1,16 @@
 | 
				
			|||||||
package com.test.controller;
 | 
					package com.test.controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.alibaba.csp.sentinel.annotation.SentinelResource;
 | 
				
			||||||
 | 
					import com.alibaba.fastjson.JSONObject;
 | 
				
			||||||
import com.test.entity.UserBorrowDetail;
 | 
					import com.test.entity.UserBorrowDetail;
 | 
				
			||||||
import com.test.service.BorrowService;
 | 
					import com.test.service.BorrowService;
 | 
				
			||||||
import org.springframework.web.bind.annotation.PathVariable;
 | 
					import org.springframework.web.bind.annotation.PathVariable;
 | 
				
			||||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
					import org.springframework.web.bind.annotation.RequestMapping;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.RequestParam;
 | 
				
			||||||
import org.springframework.web.bind.annotation.RestController;
 | 
					import org.springframework.web.bind.annotation.RestController;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.annotation.Resource;
 | 
					import javax.annotation.Resource;
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@@ -22,8 +26,52 @@ public class BorrowController {
 | 
				
			|||||||
    @Resource
 | 
					    @Resource
 | 
				
			||||||
    BorrowService service;
 | 
					    BorrowService service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @RequestMapping("/borrow/{uid}")
 | 
					    @RequestMapping("/borrow1/{uid}")
 | 
				
			||||||
    UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){
 | 
					    UserBorrowDetail findUserBorrows1(@PathVariable("uid") int uid) {
 | 
				
			||||||
        return service.getUserBorrowDetailByUid(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;
 | 
					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.Book;
 | 
				
			||||||
import com.test.entity.Borrow;
 | 
					import com.test.entity.Borrow;
 | 
				
			||||||
import com.test.entity.User;
 | 
					import com.test.entity.User;
 | 
				
			||||||
@@ -12,6 +14,7 @@ import com.test.service.client.UserClient;
 | 
				
			|||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.annotation.Resource;
 | 
					import javax.annotation.Resource;
 | 
				
			||||||
 | 
					import java.util.Collections;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.stream.Collectors;
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -35,6 +38,7 @@ public class BorrowServiceImpl implements BorrowService {
 | 
				
			|||||||
    BookClient bookClient;
 | 
					    BookClient bookClient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
 | 
					    @SentinelResource(value = "details", blockHandler = "blocked")
 | 
				
			||||||
    public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
 | 
					    public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
 | 
				
			||||||
        List<Borrow> borrow = mapper.getBorrowsByUid(uid);
 | 
					        List<Borrow> borrow = mapper.getBorrowsByUid(uid);
 | 
				
			||||||
        User user = userClient.getUserById(uid);
 | 
					        User user = userClient.getUserById(uid);
 | 
				
			||||||
@@ -44,4 +48,9 @@ public class BorrowServiceImpl implements BorrowService {
 | 
				
			|||||||
                .collect(Collectors.toList());
 | 
					                .collect(Collectors.toList());
 | 
				
			||||||
        return new UserBorrowDetail(user, bookList);
 | 
					        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:
 | 
					      discovery:
 | 
				
			||||||
        # 配置Nacos注册中心地址
 | 
					        # 配置Nacos注册中心地址
 | 
				
			||||||
        server-addr: localhost:8848
 | 
					        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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,6 +18,18 @@
 | 
				
			|||||||
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
					        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
				
			||||||
    </properties>
 | 
					    </properties>
 | 
				
			||||||
    <dependencies>
 | 
					    <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-bootstrap</artifactId>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>com.alibaba.cloud</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
        <dependency>
 | 
					        <dependency>
 | 
				
			||||||
            <groupId>com.alibaba.cloud</groupId>
 | 
					            <groupId>com.alibaba.cloud</groupId>
 | 
				
			||||||
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 | 
					            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,7 +24,7 @@ public class UserController {
 | 
				
			|||||||
    //这里以RESTFul风格为例
 | 
					    //这里以RESTFul风格为例
 | 
				
			||||||
    @RequestMapping("/user/{uid}")
 | 
					    @RequestMapping("/user/{uid}")
 | 
				
			||||||
    public User findUserById(@PathVariable("uid") int uid) {
 | 
					    public User findUserById(@PathVariable("uid") int uid) {
 | 
				
			||||||
        System.out.println("调用图书服务");
 | 
					        System.out.println("调用用户服务");
 | 
				
			||||||
        return service.getUserById(uid);
 | 
					        return service.getUserById(uid);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,16 +1,11 @@
 | 
				
			|||||||
server:
 | 
					 | 
				
			||||||
  port: 8101
 | 
					 | 
				
			||||||
spring:
 | 
					spring:
 | 
				
			||||||
  datasource:
 | 
					 | 
				
			||||||
    driver-class-name: com.mysql.cj.jdbc.Driver
 | 
					 | 
				
			||||||
    url: jdbc:mysql://43.143.164.194:3306/mac
 | 
					 | 
				
			||||||
    username: mac
 | 
					 | 
				
			||||||
    password: mactest
 | 
					 | 
				
			||||||
  # 应用名称 userservice
 | 
					 | 
				
			||||||
  application:
 | 
					 | 
				
			||||||
    name: userservice
 | 
					 | 
				
			||||||
  cloud:
 | 
					  cloud:
 | 
				
			||||||
    nacos:
 | 
					    nacos:
 | 
				
			||||||
      discovery:
 | 
					      discovery:
 | 
				
			||||||
        # 配置Nacos注册中心地址
 | 
					        # 配置Nacos注册中心地址
 | 
				
			||||||
        server-addr: localhost:8848
 | 
					        server-addr: localhost:8848
 | 
				
			||||||
 | 
					        namespace: dd668135-0bfe-489f-ab2b-24aefb21d156
 | 
				
			||||||
 | 
					    sentinel:
 | 
				
			||||||
 | 
					      transport:
 | 
				
			||||||
 | 
					        # 添加监控页面地址即可
 | 
				
			||||||
 | 
					        dashboard: localhost:8858
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										14
									
								
								user-service/src/main/resources/bootstrap.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								user-service/src/main/resources/bootstrap.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
				
			|||||||
 | 
					spring:
 | 
				
			||||||
 | 
					  application:
 | 
				
			||||||
 | 
					    # 服务名称和配置文件保持一致
 | 
				
			||||||
 | 
					    name: userservice
 | 
				
			||||||
 | 
					  profiles:
 | 
				
			||||||
 | 
					    # 环境也是和配置文件保持一致
 | 
				
			||||||
 | 
					    active: dev
 | 
				
			||||||
 | 
					  cloud:
 | 
				
			||||||
 | 
					    nacos:
 | 
				
			||||||
 | 
					      config:
 | 
				
			||||||
 | 
					        # 配置文件后缀名
 | 
				
			||||||
 | 
					        file-extension: yml
 | 
				
			||||||
 | 
					        # 配置中心服务器地址,也就是Nacos地址
 | 
				
			||||||
 | 
					        server-addr: localhost:8848
 | 
				
			||||||
		Reference in New Issue
	
	Block a user