一,导入依赖 <dependency> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
<dependency> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency> 二、配置yml server: port: 10010 logging: level: cn.itcast: debug
pattern: dateformat: MM-dd HH:mm:ss:SSS spring: application: name: gateway
cloud: nacos: server-addr: localhost:8848 # nacos地址 gateway: routes: - id:
itemservice # 路由标示,必须唯一 uri: lb://itemservice # 路由的目标地址 predicates: #
路由断言,判断请求是否符合规则 - Path=/item/** # 路径断言,判断路径是否是以/item开头,如果是则符合 - id:
orderservice uri: lb://orderservice predicates: - Path=/order/**,/pay/** - id:
userservice uri: lb://userservice predicates: - Path=/user/**,/address/** - id:
searchservice uri: lb://searchservice predicates: - Path=/search/**
default-filters: - AddRequestHeader=authorization, 2//添加网关的请求头 globalcors:
add-to-simple-url-handler-mapping: true corsConfigurations: '[/**]':
allowedOrigins: //请求地址 allowedMethods: //请求方式
- "GET" - "POST" - "DELETE" - "PUT" - "OPTIONS" allowedHeaders: "*" #
允许在请求中携带的头信息 allowCredentials: true # 是否允许携带cookie maxAge: 360000 # 这次跨域检测的有效期
三、创建引导类