Java Gateway网关两种配置文件写法:.properties和.yml

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/**

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值