1.pom中导入坐标

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.6.RELEASE</version>
  9. <relativePath/>
  10. </parent>
  11. <groupId>com.nthink.his</groupId>
  12. <modelVersion>4.0.0</modelVersion>
  13. <artifactId>his-gateway</artifactId>
  14. <version>1.0.0</version>
  15. <dependencies>
  16. <!--nacos服务注册发现依赖-->
  17. <dependency>
  18. <groupId>com.alibaba.cloud</groupId>
  19. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  20. <version>2.2.6.RELEASE</version>
  21. </dependency>
  22. <!--网关gateway依赖-->
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-gateway</artifactId>
  26. <version>2.2.7.RELEASE</version>
  27. </dependency>
  28. </dependencies>
  29. </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