1.下載Sentinel.jar可以圖形界面配置限流和降級規(guī)則
地址:可能需要翻墻
下載jar文件
2.引入maven依賴
<!-- spring cloud gateway整合sentinel的依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- sentinel的依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.8.0</version>
</dependency>
3.寫個自動注入Resource的過濾器類(可以不寫注解直接使用)
@Configuration
public class GatewayConfiguration {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
}
4.寫配置文件 application.properties
# 服務(wù)端口
server.port=80
# 服務(wù)名
spring.application.name=service-gateway
#服務(wù)熔斷
spring.cloud.sentinel.transport.dashboard=localhost:18080
# nacos服務(wù)地址
spring.cloud.nacos.discovery.server-addr=192.168.56.1:8848
spring.main.allow-bean-definition-overriding=true
spring.profiles.active=dev
#使用服務(wù)發(fā)現(xiàn)路由
spring.cloud.gateway.discovery.locator.enabled=true
#設(shè)置路由id
spring.cloud.gateway.routes[0].id=service-cmn
#設(shè)置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-cmn
#設(shè)置路由斷言,代理servicerId為auth-service的/auth/路徑
spring.cloud.gateway.routes[0].predicates= Path=/*/cmn/**
5.cmd命令行啟動jar文件訪問localhost:18080頁面,自己設(shè)置QPS
java -jar -server.port=18080 sentinel-dashboard.jar
--------不在微服務(wù)中使用,在普通springboot也可以使用--------
1.maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.service中寫
@Service
public class UserService {
//不可以用在類上
@SentinelResource(value = "sayHello",fallback = "sayHellofail")
public String sayHello(){
return "Hello,World";
}
public String sayHellofail(){ //限流的方法
return "I'am sorry";
}
}
3.controller
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/hello")
public String hello(){
return userService.sayHello();
}
}
3.sentinel控制臺查看文章來源:http://www.zghlxwxcb.cn/news/detail-723346.html
//快速訪問文章來源地址http://www.zghlxwxcb.cn/news/detail-723346.html
到了這里,關(guān)于SpringCloud之Gateway整合Sentinel服務(wù)降級和限流的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!