yml格式
# 网关端口
server:
port: 10010
# log config
logging:
config: classpath:logback.xml
# 服务名称
spring:
application:
name: admin-gateway
cloud:
# 全局跨域处理
gateway:
enabled: true
globalcors:
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题(浏览器跨域预检请求)
cors-configurations:
'[/**]':
allowed-origins: * # 允许哪些网站的跨域请求
# - "http://localhost:8090"
# - "http://localhost:8091"
allowed-headers: * # 允许在请求中携带的头信息
allowed-methods: * # 允许的跨域ajax的请求方式
# - "GET"
# - "POST"
# - "DELETE"
# - "PUT"
# - "OPTIONS"
allow-credentials: true # 是否允许携带cookie
routes: # 网关路由配置
- id: admin_route1 # 路由id 不重复就行
uri: http://localhost:8001 # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/api/** # 这里是按照路径匹配
filters: #过滤器
- AddRequestHeader=This is a header! # 添加请求头
- id: admin_route2
uri: http://localhost:8002
predicates:
- Path=/api/user/**
filters:
- RewritePath=/api/(?<segment>.*),/renren-fast/${segment}
properties格式
# 网关端口
server.port=10010
# log config
logging.config=classpath:logback.xml
# 服务名称
spring.application.name = admin-gateway
# 全局跨域处理 ##########################
spring.cloud.gateway.enabled=true
# 解决options请求被拦截问题(浏览器跨域预检请求)
spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping=true
# 允许哪些网站的跨域请求
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-origins=*
# 允许在请求中携带的头信息
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-headers=*
# 允许的跨域ajax的请求方式
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-methods=*
# 是否允许携带cookie
spring.cloud.gateway.globalcors.cors-configurations.[/**].allow-credentials=true
# 网关路由配置 ##########################
# 路由id 不重复就行
spring.cloud.gateway.routes[0].id=admin_route1
# 路由的目标地址 lb就是负载均衡,后面跟服务名称
spring.cloud.gateway.routes[0].uri=http://localhost:8001
# 路由断言,也就是判断请求是否符合路由规则的条件 # 这里是按照路径匹配
spring.cloud.gateway.routes[0].predicates=Path=/api/**
# 过滤器 例:添加请求头
spring.cloud.gateway.routes[0].filters=AddRequestHeader=This is a header!
spring.cloud.gateway.routes[1].id=admin_route2
spring.cloud.gateway.routes[1].uri=http://localhost:8002
spring.cloud.gateway.routes[1].predicates=Path=/api/**
该文展示了SpringCloudGateway的配置,包括服务器端口、日志设置、服务名称,重点在于全局跨域处理和网关路由配置。全局跨域配置允许所有来源,支持多种HTTP方法,并允许携带cookie。路由配置示例中,定义了两个路由,分别指向不同端口,其中一个添加了自定义请求头,另一个进行了路径重写。
1451

被折叠的 条评论
为什么被折叠?



