1.pom中导入坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.nthink.his</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>his-gateway</artifactId>
<version>1.0.0</version>
<dependencies>
<!--nacos服务注册发现依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!--网关gateway依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
</dependencies>
</project>
2.导入坐标时总是显示红色
server:
port: 10010 # 网关端口
spring:
application:
name: his-gateway # 服务名称
cloud:
nacos:
server-addr: 10.10.10.21:8848 # nacos地址
gateway:
routes: # 网关路由配置
- id: his-resource # 路由id,自定义,只要唯一即可
# uri: http://10.10.10.102:10003
uri: lb://his-resource-service # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/resource/** # 这个是按照路径匹配,只要以/resource/一个服务公用一个就可以了
filters:
- RewritePath=/resource/?(?<segment>.*),/$\{segment}
#- id: his-resource-02 # 路由id,自定义,只要唯一即可
#uri: lb://his-resource-service # 路由的目标地址 lb就是负载均衡,后面跟服务名称
#predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
# - Path=/itemMain/** # 这个是按照路径匹配,一个Controller就要配置一个
# - Path=/itemUnits/** # 这个是按照路径匹配,一个Controller就要配置一个
# - Path=/scheduleFees/** # 这个是按照路径匹配,一个Controller就要配置一个
# - Path=/scheduleMain/** # 这个是按照路径匹配,一个Controller就要配置一个
globalcors: # 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]':
allowedOrigins: # 允许哪些网站的跨域请求
- "http://10.10.10.102:10003"
allowedMethods: # 允许的跨域ajax的请求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期
debug: true
logging:
level:
root: debug
server:
port: 10010 # 网关端口
spring:
application:
name: his-gateway # 服务名称
cloud:
nacos:
server-addr: 10.10.10.21:8848 # nacos地址
gateway:
routes: # 网关路由配置
- id: user-service1 # 路由id,自定义,只要唯一即可
# uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址
uri: lb://his-resource-service # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/itemMain/**,/scheduleMain/**,/itemUnits/**,/scheduleFees/** # 这个是按照路径匹配,只要以/user/开头就符合要求
- id: user-service2
url: lb://his-resource-service
predicates:
- Path=/resource/**
filters:
- RewritePath=/resource/?(?<segment>.*),/$\{segment}
globalcors: # 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]':
allowedOrigins: # 允许哪些网站的跨域请求
- "http://10.10.10.102:10003"
allowedMethods: # 允许的跨域ajax的请求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期
debug: true
logging:
level:
root: debug
备份
server:
port: 10010 # 网关端口
spring:
application:
name: his-gateway # 服务名称
cloud:
nacos:
server-addr: 10.10.10.21:8848 # nacos地址
gateway:
routes: # 网关路由配置
- id: user-service1 # 路由id,自定义,只要唯一即可
# uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址
uri: lb://his-resource-service # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/itemMain/**,/scheduleMain/**,/itemUnits/**,/scheduleFees/** # 这个是按照路径匹配,只要以/user/开头就符合要求
- id: user-service2
uri: lb://his-resource-service
predicates:
- Path=/resource/**
filters:
- RewritePath=/resource/?(?<segment>.*),/$\{segment}
globalcors: # 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]':
allowedOrigins: # 允许哪些网站的跨域请求
- "http://10.10.10.102:10003"
allowedMethods: # 允许的跨域ajax的请求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期
debug: true
logging:
level:
root: debug