sentinel使用(结合gateway)
前
如果你想在Spring Cloud Gateway中使用Sentinel Starter,你需要添加Spring - Cloud -alibaba- Sentinel - Gateway依赖,并添加Spring - Cloud - Starter - Gateway依赖,让Spring Cloud Gateway自动配置类在模块中生效
帮助文档: https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel#spring-cloud-gateway-支持
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
搭建环境
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wzq.example</groupId>
<artifactId>cloud_sentine_gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud_sentine_gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>Hoxton.SR8</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.5.RELEASE</spring.cloud.alibaba.version>
<spring.boot.version>2.2.5.RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意:spring的版本需要一致
重点jar: spring-cloud-starter-alibaba-nacos-discovery , spring-cloud-alibaba-sentinel-gateway , spring-cloud-starter-gateway
application.yml
server:
port: 8085
spring:
application:
name: cloud-sentine-gateway
cloud:
sentinel:
eager: true # 提前触发初始化
transport:
port: 8719
dashboard: 192.168.60.129:8080
nacos:
discovery:
server-addr: 192.168.60.129:8848
gateway:
routes:
# - id: service-provider
# uri: lb://service-provider
# predicates:
# # 匹配所有hello开头的接口 也可以是 context-path 名称
# - Path=/hello/**
- id: cloud-sentine-open-feign-provider
uri: lb://cloud-sentine-open-feign-provider
predicates:
- Path=/hello/**
management:
endpoints:
web:
exposure:
include: "*"
CloudSentineGatewayApplication.java
package com.wzq.example.cloud_sentine_gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class CloudSentineGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(CloudSentineGatewayApplication.class, args);
}
}
测试
在控制台中添加 cloud-sentine-gateway 的限流规则
访问接口要是出现:限流成功
{
"code": 429,
"message": "Blocked by Sentinel: FlowException"
}
sentinel使用(结合gateway)的更多相关文章
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例
sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...
- Spring Cloud Gateway 扩展支持动态限流
之前分享过 一篇 <Spring Cloud Gateway 原生的接口限流该怎么玩>, 核心是依赖Spring Cloud Gateway 默认提供的限流过滤器来实现 原生Request ...
- Spring Cloud gateway 五 Sentinel整合
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- Spring Cloud gateway 六 Sentinel nacos存储动态刷新
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- Spring Cloud gateway 七 Sentinel 注解方式使用
Sentinel 注解支持 @SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项. @SentinelResource 注解包含以下属性: value:资 ...
- 阿里Sentinel支持Spring Cloud Gateway啦
1. 前言 4月25号,Sentinel 1.6.0 正式发布,带来 Spring Cloud Gateway 支持.控制台登录功能.改进的热点限流和注解 fallback 等多项新特性,该出手时就出 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例
sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...
- springcloud gateway整合sentinel
1.引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- SpringCloud Gateway高阶之Sentinel限流、熔断
前言 为什么需要服务熔断和降级?微服务是当前业界的一大趋势,原理就是将单一职责的功能模块独立化为子服务,降低服务间的耦合,服务间互相调用.但是这样也会出现一些问题: 上图中大量微服务互相调用,存在大量 ...
随机推荐
- 『心善渊』Selenium3.0基础 — 26、unittest测试框架的断言
目录 1.断言介绍 2.常用的断言方法 3.断言示例 1.断言介绍 在执行测试用例的过程中,最终用例是否执行通过,是通过判断测试得到的实际结果和预期结果是否相等决定的,这时会用到断言方法. 本着没有消 ...
- SpringBoot自动装配原理之Configuration以及@Bean注解的使用
Configuration以及Bean注解的使用 该知识点在Spring中应该学过,没有学过或者遗忘的的朋友需要预习或温习前置知识点.SpringBoot其实就是Spring的进一步简化,所以前置知识 ...
- CPU 几核
1.设备管理器:打开"处理器",出现几个就是几核
- C语言:位运算加密
数据加密解密是一个常用的功能,如果你不希望让别人看到文件中的内容,可以通过密钥(也称"密码")将文件的内容加密.比如文本文件(.txt),加密前的内容是能够读懂的,加密后的内容是& ...
- SFC style CSS variable injection
摘要 在单文件组件样式中支持使用组件状态驱动的 CSS 变量( CSS 自定义属性). 基础示例 <template> <div class="text"> ...
- 如何热更新长缓存的 HTTP 资源
前言 HTTP 缓存时间一直让开发者头疼.时间太短,性能不够好:时间太长,更新不及时.当遇到严重问题需紧急修复时,尽管后端文件可快速替换,但前端文件仍从本地缓存加载,导致更新长时间无法生效. 对于这个 ...
- oracle 密码详解以及破解
参考的相关资料等: https://docs.oracle.com/en/database/oracle/oracle-database/18/spmsu/finding-and-resetting- ...
- 如何在cmd中运行.py文件
C:\Users\mf>cd C:\Program Files\Python36\ C:\Program Files\Python36>python const.py 切换到.py文件所在 ...
- 医疗器械软件产品经理必读的法规及标准-YY/T0664(一)
医疗器械软件产品经理必读的法规及标准-YY/T0664(一) 医疗器械软件的产品经理,需要熟读医药行业标准,在软件设计开发的整个生存周期过程中,我们需要根据<YY/T 0664 医疗器械软件 软 ...
- python基础问题
包安装相关问:如何安装Python三方包?在命令行如何检查一个包是否已安装?答:安装用pip install 卸载用 pip uninstall 直接import 这个包问:环境变量PATH的作用是什 ...