当前位置: 首页 > news >正文

6、spring-cloud-gateway

依赖

    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</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-nacos-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency></dependencies>

配置文件

server:port: 8080spring:application:name: gatewaycloud:nacos:discovery:server-addr: localhost:8848gateway:routes:- id: ms1uri: lb://ms1predicates:- Path=/ms1/**filters:- RewritePath=/ms1/?(?<segment>.*),/$\{segment}- id: ms2uri: lb://ms2predicates:- Path=/ms2/**filters:- RewritePath=/ms2/?(?<segment>.*),/$\{segment}- id: ms3uri: lb://ms3predicates:- Path=/ms3/**filters:- RewritePath=/ms3/?(?<segment>.*),/$\{segment}

配置类

可自定义断言工厂、拦截器工厂、全局拦截器

自定义全局拦截器

@Slf4j
@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest request = exchange.getRequest();ServerHttpResponse response = exchange.getResponse();String uri = request.getURI().toString();StopWatch stopWatch = new StopWatch();stopWatch.start();log.info("请求【{}】开始,时间:{}", uri, LocalDateTime.now());// 放行Mono<Void> mono = chain.filter(exchange).doFinally((result) -> {stopWatch.stop();log.info("请求【{}】结束,时间:{},耗时:{}s", uri, LocalDateTime.now(), stopWatch.getTotalTimeSeconds());});return mono;}@Overridepublic int getOrder() {// 数字越小,优先级越高return 0;}
}

相关文章:

  • rollup使用讲解
  • My SQL 索引
  • 山东大学软件学院项目实训-基于大模型的模拟面试系统-Token过期重定向问题
  • uniapp微信小程序实现sse
  • 基于Matlab求解矩阵电容等效容值
  • 嵌入式人工智能应用-第三章 opencv操作 5 二值化、图像缩放
  • 2.2 BackgroundWorker的使用介绍
  • CentOS 10 /root 目录重新挂载到新分区槽
  • Qt中修改了UI设计文件后编译不生效问题的解决办法
  • 拼多多面经,暑期实习Java一面
  • 20. git diff
  • 博客系统-RabbitMQ
  • 【自然语言处理与大模型】Linux环境下Ollama下载太慢了该怎么处理?
  • 计算机网络中的网络层:架构、功能与重要性
  • IPv4地址分类与常用网络地址详解
  • 易语言验证京东滑块轨迹算法
  • Beszel​​ 轻量级服务器监控平台的详细安装步骤
  • 软考复习——综合布线
  • 用键盘实现控制小球上下移动——java的事件控制
  • C语言自增自减题目
  • 沙龙 | 新书分享:中国电商崛起的制度密码
  • 商务部新闻发言人就美国以关税手段胁迫其他国家限制对华经贸合作事答记者问
  • 圆桌|耐心资本对科技创新有何意义?天使投资最关注哪些要素?
  • 2025上海半马鸣枪,多个“首次”冲击一城双白金
  • 再现逆转!蒯曼击败伊藤美诚晋级澳门世界杯女单决赛
  • “特朗普的欧洲耳语者”:梅洛尼白宫之行真能打破美欧关税僵局?