image.png

image.png

操作和代码分析

1.引入依赖

image.png



com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery



org.springframework.cloud
spring-cloud-starter-gateway

2.配置文件

image.png
server:
port: 10010
spring:
application:
name: gateway
cloud:
nacos:
server-addr: 192.168.94.129:8848 # nacos地址
_gateway:
routes:
- id: userservice
uri: lb://userservice
# 路由的地址,lb,负载均衡
predicates:
- Path=/user/,/address/
- id: orderservice
uri: lb://orderservice
predicates:
- Path=/order/,/pay/
- id: itemservice
uri: lb://itemservice
predicates:
- Path=/item/
- id: searchservice
uri: lb://searchservice
predicates:
- Path=/search/

default-filters:
- AddRequestHeader=authorization, 2
globalcors:
# 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
‘[/**]’:
allowedOrigins:
# 允许哪些网站的跨域请求
- “http://localhost:9001
- “http://localhost:9002
allowedMethods:
# 允许的跨域ajax的请求方式
- “GET”
- “POST”
- “DELETE”
- “PUT”
- “OPTIONS”
allowedHeaders: “*”
# 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期_

image.png_