SpringBoot整合Gateway简单案例

一、相关核心依赖


  org.springframework.cloud
  spring-cloud-starter-gateway


  org.springframework.cloud
  spring-cloud-starter-netflix-eureka-client

二、相关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();
    }
}

三、测试是否整合成功

SpringBoot整合Gateway简单案例_第1张图片

SpringBoot整合Gateway简单案例_第2张图片

SpringBoot整合Gateway简单案例_第3张图片

你可能感兴趣的:(spring,boot,gateway,eureka)