getway
温馨提示:
本文最后更新于 2025年09月10日,已超过 276 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。

基础配置
新建一个模块,在 启动类 标注 @EnableDiscoveryClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class GetwayMainApplication {
public static void main(String[] args) {
SpringApplication.run(GetwayMainApplication.class, args);
}
} xml的依赖
org.springframework.cloud
spring-cloud-starter-loadbalancer
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-gateway
两个配置文件 application.yml
server:
port: 80
spring:
profiles:
include: route
application:
name: getway
cloud:
nacos:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos121nacos121nacos121nacos121nacos121nacos121
import-check:
enabled: false application-route.yml order: 2 对应的数值越大,排序越靠后,数字越小,越先执行
spring:
cloud:
gateway:
routes:
- id: order-route
uri: lb://service-order
predicates:
- Path=/api/order/**
order: 1
- id: product-route
uri: lb://service-product
predicates:
- Path=/api/product/**
order: 2 Controller类 标注 断言的前缀 /api/xxx
@RequestMapping("/api/order") 注意 feign 调用的方法 ,需 添加在请求路径中,基础 网关功能搭建成功。
简便设置 是 在yml 配置 需要的项目前缀,,feign 调用的方法 ,需 添加在请求路径中
server:
servlet:
context-path: /api/product 路由基础原理

断言


路径重写

application-route.yml ==> 此方法 不用 项目中去加 项目前缀,便捷高效
spring:
cloud:
gateway:
routes:
- id: order-route
uri: lb://service-order
predicates:
- Path=/api/order/**
filters:
# 路径重写
- RewritePath=/api/order/?(?.*),/$\{segment}
# 添加请求头参数
- AddResponseHeader=X-Response-acc, 999
order: 2
- id: product-route
uri: lb://service-product
predicates:
- Path=/api/product/**
filters:
- RewritePath=/api/product/?(?.*),/$\{segment}
order: 3
1 自定义 全局拦截器 计算请求时间
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
@Slf4j
public class RtGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
String uri = request.getURI().toString();
log.info("请求 [{}],开始时间 {}", uri, DateUtil.now());
TimeInterval timer = DateUtil.timer();
Mono filter = chain.filter(exchange).doFinally(result -> {
log.info("请求 [{}],结束时间 {} 耗时 {} ", uri, DateUtil.now(), timer.interval() + "ms");
});//放行
return filter;
}
@Override
public int getOrder() {
return 0;
}
}
全局跨域 网关配置 所有访问资源的跨域
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedMethods: "*"
allowedHeaders: "*"
正文到此结束
- 本文标签: SpringCloud
- 本文链接: http://119.91.109.247:8443//article/135
- 版权声明: 本文由张亚东原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权