Springcloud---B站学习总结---新一代网关Gateway路由

Springcloud—B站学习总结—新一代网关Gateway路由

 

9527网关如何做映射路由

在这里插入图片描述

配置文件:

server:
  port: 9527

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      #routes表示路由有很多个,不只是一个,某个controller里面的rest风格接口都可以做路由
      routes:
        - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8001          #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/get/**         # 断言,路径相匹配的进行路由(提供服务的rest风格的url)

        - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8001          #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/lb/**         # 断言,路径相匹配的进行路由(提供服务的rest风格的url)
            

注意:Gateway网关不需要引入这两个依赖,否则会报错

在这里插入图片描述

所以说我们要移除下面的两个依赖


<dependency>
	<groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

带有网关之后的访问说明:访问cloud-provider-payment8001服务

在这里插入图片描述

通过Gateway网关的访问cloud-provider-payment8001服务的访问说明

配置文件指向对应的rest风格的url
在这里插入图片描述

通过Gateway网关路由的两种配置方法:代码注入RouteLocator的Bean

在这里插入图片描述
代码注入例子:

@Configuration
public class GateWayConfig
{
    //RouteLocator:路由构建器
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder)
    {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

        routes.route( id:"path_route_atguigu",
                r -> r.path(...partterns:"/guonei")
                        .uri("http://news.baidu.com/guonei")).build();

        return routes.build();
    }
}

 

通过微服务名实现动态路由

在这里插入图片描述

配置YML配置文件(包含网关和将服务注册进eureka服务中心)

YML配置文件需要注意:
在这里插入图片描述

server:
  port: 9527

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
      #routes表示路由有很多个,不只是一个,某个controller里面的rest风格接口都可以做路由
      routes:
        - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
          #uri: http://localhost:8001          #匹配后提供服务的路由地址
          uri: lb://cloud-payment-service #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/get/**         # 断言,路径相匹配的进行路由

        - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
          #uri: http://localhost:8001          #匹配后提供服务的路由地址
          uri: lb://cloud-payment-service #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/lb/**         # 断言,路径相匹配的进行路由
            #- After=2020-02-21T15:51:37.485+08:00[Asia/Shanghai]
            #- Cookie=username,zzyy
            #- Header=X-Request-Id, \d+  # 请求头要有X-Request-Id属性并且值为整数的正则表达式

eureka:
  instance:
    hostname: cloud-gateway-service
#    instance-id: gateway77777777
  client: #服务提供者provider注册进eureka服务列表内
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://eureka7001.com:7001/eureka
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值