1.配置application.yml文件
spring:
application:
name: gateway-admin
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedMethods:
- GET
- POST
- PUT
- DELETE
routes:
- id: file
uri: http://localhost:8002
filters:
- StripPrefix=1
predicates:
- Path=/brand/**
2.配置解析
首先,我的微服务的地址为http://localhost:8002
,网关端口为9001
,我们需要通过网关访问微服务,那么配置路由过滤为- Path=/brand/**
,意识就是访问地址http://localhost/brand/后接地址
的所有请求都会映射到http://localhost:8002/后接地址
。
注意:按照网上的文档,这里直接配置-Path=/brand/**
即可,但事实很慢访问结果为404
,需要加上- StripPrefix=1`,才能访问成功。
总结
公众号“程序港湾”已经开通,扫描以下二维码关注,海量教程等你来选,还有工程师在线答疑。