SpringBoot整合Gateway简单案例

一、相关核心依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

二、相关yml配置

1)、固定ip配置

server:
  port: 9527
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_routh
          uri: http://localhost:8001
          predicates:
           - Path=/payment/getById/**
        - id: payment_routh2
          uri: http://localhost:8001
          predicates:
           - Path=/payment/lb/**
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    #表示是否将自己注册进Eureka Server 默认为true
    register-with-eureka: true
    #是否从Eureka Server抓取已有的注册信息,默认为true。单节点无所谓。集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/


2)、根据服务名动态负载均衡

server:
  port: 9527
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名称进行路由
      routes:
        - id: payment_routh #路由的id,没有固定规则但要求唯一,建议配合服务名
          #uri: http://localhost:8001 固定ip
          uri: lb://cloud-payment-service #根据服务名称
          predicates:
           - Path=/payment/getById/**
        - id: payment_routh2
          #uri: http://localhost:8001
          uri: lb://cloud-payment-service #根据服务名称
          predicates:
           - Path=/payment/lb/**
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    #表示是否将自己注册进Eureka Server 默认为true
    register-with-eureka: true
    #是否从Eureka Server抓取已有的注册信息,默认为true。单节点无所谓。集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/


3)、Gateway也可以通过代码形式配置,例如:

@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes().
                route("path_toute_zhq1", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).
                route("path_toute_zhq1", r -> r.path("/guoji").uri("http://news.baidu.com/guoji")).
                build();
    }
}

三、测试是否整合成功

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值