springcloud gateway设置context-path的操作
今天说一下遇到的问题,关于 springcloud gateway 设置 context-path 的问题。
1.使用场景
由于没有申请二级域名,网关使用的地址是 xxx.com/gateway/ 用nginx转发的时候 /gateway/ 也被用来寻址。
gateway 没办法设置 context-path ,针对我这个场景有3个解决方案。
2.解决方案
2.1 增加本地路由(有一个网址指向自己,这里就是 /gateway)
spring:
cloud:
gateway:
routes:
# 网关本身没有contextPath,通过自己转发自己,达到能处理contextPath
- id: self
uri: http://localhost:${server.port}
predicates:
- Path=/${spring.application.name}/**
filters:
- StripPrefix=1
order: -11000
这种方式会丢失请求,暂时没考虑原因,就pass了。
2.2 增加过滤器,改写路径
ApiFilter.java
package com.yiche.ballast.filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.WebFilter;
import reactor.core.publisher.Mono;
@Configuration
public class ApiFilter {
@Value("${spring.cloud.gateway.api-prefix:/gateway}")
private String prefix;
@Bean
@Order(-1)
public WebFilter apiPrefixFilter() {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getRawPath();
if (!path.contains(prefix)) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.BAD_GATEWAY);
DataBuffer buffer = response
.bufferFactory()
.wrap(HttpStatus.BAD_GATEWAY.getReasonPhrase().getBytes());
return response.writeWith(Mono.just(buffer));
}
String newPath = path.replaceFirst(prefix, "");
ServerHttpRequest newRequest = request.mutate().path(newPath).build();
return chain.filter(exchange.mutate().request(newRequest).build());
};
}
}
这样/gateway 请求进来之后,转发到routers 的时候会把 /gateway去掉,缺点是每个请求进来都需要对路径处理一次。
能配置的尽量不写代码。
2.3 修改配置,在所有的router路径前加前缀(这里就是都加上 /gateway)
spring:
cloud:
gateway:
routes:
- id: api-route
filters:
- StripPrefix=1
predicates:
- name: Path
args[pattern]: /gateway/api/**
uri: lb://xxx-api
偷懒的做法,路由多的时候也挺难受。
现在路由不多,选择了第三种方式。看各自的场景选择吧。
springcloud 的gateway踩坑
添加了路由规则的配置以后,SpringCloud无法正常启动,启动的时候报错
1、配置文件中开启debug=true模式
错误信息显示缺少javax.validation.ValidatorException类;
2、在pom文件中添加hibernate-validator(以及所有相关依赖)

3、结果仍旧报错,此时错误信息:
![]()
![]()
不能为空,之前是配置在yml文件中,后来换成了properties,问题就解决了;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Java BigDecimal解决double精度丢失的问题
我们在日常开发中, 有很多时候会遇到小数(double类型)精确计算,本文主要介绍了Java BigDecimal解决double精度丢失的问题,具有一定的参考价值,感兴趣的可以了解一下2023-11-11
Spring中利用SchedulingConfigurer实现动态定时任务配置的示例
定时任务是一项至关重要的功能,它们使得我们能够按照预定的时间执行特定的任务,本文主要介绍了Spring中利用SchedulingConfigurer实现动态定时任务配置的示例,感兴趣的可以了解一下2024-05-05
java 使用ImageIO.writer从BufferedImage生成jpeg图像遇到问题总结及解决
这篇文章主要介绍了java 使用ImageIO.writer从BufferedImage生成jpeg图像遇到问题总结及解决的相关资料,需要的朋友可以参考下2017-03-03
SpringBoot+Redis使用AOP防止重复提交的实现
本文主要介绍了SpringBoot+Redis使用AOP防止重复提交的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-07-07


最新评论