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路由轉發

尹吉歡我不差錢啊喜歡做者

相關文章
相關標籤/搜索