Spring Cloud Gateway 整合Eureka路由转发

前面咱们对Spring Cloud Gateway进行了一个入门的学习,具体文章能够查看《Spring Cloud Gateway 网关尝鲜》进行学习。git

网关负责转发工做,那么它须要知道后端的服务信息,今天咱们来学习下Spring Cloud Gateway 整合Eureka的操做,实现服务转发功能。github

在以前的基础上添加eureka-client的依赖:spring

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

下面就是配置具体的转发规则了,这边须要注意下的是uri的配置:express

server:
  port: 8084
spring:
  cloud:
    gateway:
      routes:
      - id: fsh-house
        uri: lb://fsh-house
        predicates:
        - Path=/house/**

  application:
    name: fangjia-gateway

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://yinjihuan:123456@master:8761/eureka/

uri以lb://开头(lb表明从注册中心获取服务),后面接的就是你须要转发到的服务名称,这个服务名称必须跟eureka中的对应,不然会找不到服务,错误以下:编程

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for fsh-house1

若是引入了spring-cloud-starter-netflix-eureka-client包,但你不想整合Eureka,也能够经过下面的配置关闭:小程序

eureka.client.enabled=false

说完了直接配置路由的方式,咱们来讲说不配置的方式也能转发,有用过Zuul的同窗确定都知道,Zuul默认会为全部服务都进行转发操做,只须要在访问路径上指定要访问的服务便可,经过这种方式就不用为每一个服务都去配置转发规则,当新加了服务的时候,不用去配置路由规则和重启网关。后端

在Spring Cloud Gateway中固然也有这样的功能,只须要经过配置便可开启,配置以下:微信

spring.cloud.gateway.discovery.locator.enabled=true

开启以后咱们就能够经过地址去访问服务了,格式以下:app

http://网关地址/服务名称(大写)/**

http://localhost:8084/FSH-HOUSE/house/1

这个大写的名称仍是有很大的影响,若是咱们从Zull升级到Spring Cloud Gateway的话意味着请求地址有改变,或者从新配置每一个服务的路由地址,经过源码我发现能够作到兼容处理,再增长一个配置便可:ide

spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true
配置完成以后咱们就能够经过小写的服务名称进行访问了,以下:

http://网关地址/服务名称(小写)/**

http://localhost:8084/fsh-house/house/1
配置源码:org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties

@ConfigurationProperties("spring.cloud.gateway.discovery.locator")
public class DiscoveryLocatorProperties {

    /** Flag that enables DiscoveryClient gateway integration */
    private boolean enabled = false;

    /**
     * The prefix for the routeId, defaults to discoveryClient.getClass().getSimpleName() + "_".
     * Service Id will be appended to create the routeId.
     */
    private String routeIdPrefix;

    /**
     * SpEL expression that will evaluate whether to include a service in gateway integration or not,
     * defaults to: true
     */
    private String includeExpression = "true";

    /** SpEL expression that create the uri for each route, defaults to: 'lb://'+serviceId */
    private String urlExpression = "'lb://'+serviceId";

    /**
     * Option to lower case serviceId in predicates and filters, defaults to false.
     * Useful with eureka when it automatically uppercases serviceId.
     * so MYSERIVCE, would match /myservice/**
     */
    private boolean lowerCaseServiceId = false;

    private List<PredicateDefinition> predicates = new ArrayList<>();

    private List<FilterDefinition> filters = new ArrayList<>();
}

文章源码参考地址:
https://github.com/yinjihuan/spring-cloud/tree/master/fangjia-gateway

猿天地第五期送书活动中奖名单以下:

加入VIP群的小伙伴中奖者是下面这位朋友:
Spring Cloud Gateway 整合Eureka路由转发

留言点赞数最高的中奖朋友是下面这位朋友:
Spring Cloud Gateway 整合Eureka路由转发

抽奖小程序的中奖朋友是下面这位朋友:

Spring Cloud Gateway 整合Eureka路由转发

请上面中奖的朋友联系我,微信jihuan900。
Spring Cloud Gateway 整合Eureka路由转发
猿天地

点击图片查看更多推荐内容
↓↓↓

编程道路上的困难—怎么克服?

大牛坐镇|高端JAVA纯技术群你要加入吗?

Spring Cloud Gateway 网关尝鲜

更多技术分享尽在微信群,加群请关注公众号,点击加群按钮。

Spring Cloud Gateway 整合Eureka路由转发

尹吉欢我不差钱啊喜欢做者