spring cloud - API Gateway

spring cloud API Gateway

  通過API Gateway,可以統一向外部系統提供REST API。Spring Cloud中使用Zuul作爲API Gateway。Zuul提供了動態路由、監控、回退、安全等功能。

  1. eureka+Zuul配置和使用

(1).準備工作

// 爲了更貼近生產,我們首先配置Host

127.0.0.1 gateway

// 啓動服務:microservice-discovery-eureka
// 啓動服務:microservice-provider-user

(2).Zuul代碼示例


//gradle依賴如下

compile "org.springframework.cloud:spring-cloud-starter-zuul:Brixton.SR5"
compile "org.springframework.cloud:spring-cloud-starter-eureka:Brixton.SR5"

//maven依賴如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>Brixton.SR5</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
    <version>Brixton.SR5</version>
</dependency>
// 啓動類ZuulApiGatewayApplication.java

/**
 * 使用@EnableZuulProxy註解激活zuul。
 * 跟進該註解可以看到該註解整合了@EnableCircuitBreaker、@EnableDiscoveryClient,是個組合註解,目的是簡化配置。
 * @author eacdy
 */
@SpringBootApplication
@EnableZuulProxy
public class ZuulApiGatewayApplication {
  public static void main(String[] args) {
    SpringApplication.run(ZuulApiGatewayApplication.class, args);
  }
}
// 配置文件:application.yml

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
eureka:
  instance:
    hostname: gateway
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/

// 這樣,一個簡單的API Gateway就完成了。      

(3).測試工作


// 啓動microservice-api-gateway項目。還記得我們之前訪問通過http://localhost:8000/1去訪問microservice-provider-user服務中id=1的用戶信息嗎?

// 我們現在訪問http://localhost:8050/microservice-provider-user/1試試。會驚人地看到:

{"id":1,"username":"Tom","age":12}

// 這正是microservice-provider-user服務中id=1的用戶信息

// 所以我們可以總結出規律:訪問

http://GATEWAY:GATEWAY_PORT/想要訪問的Eureka服務id的小寫/**

// 將會訪問到

http://想要訪問的Eureka服務id的小寫:該服務端口/**

(4).自定義路徑

// 上文我們已經完成了通過API Gateway去訪問微服務的目的,是通過

http://GATEWAY:GATEWAY_PORT/想要訪問的Eureka服務id的小寫/**

//的形式訪問的,那麼如果我們想自定義在API Gateway中的路徑呢?譬如想使用

http://localhost:8050/user/1

// 就能夠將請求路由到http://localhost:8000/1呢?

// 只需要做一點小小的配置即可:

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
eureka:
  instance:
    hostname: gateway
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
# 下面整個樹都非必須,如果不配置,將默認使用 http://GATEWAY:GATEWAY_PORT/想要訪問的Eureka服務id的小寫/** 路由到:http://想要訪問的Eureka服務id的小寫:該服務端口/**
zuul:
  routes:
    user:                                               # 可以隨便寫,在zuul上面唯一即可;當這裏的值 = service-id時,service-id可以不寫。
      path: /user/**                                    # 想要映射到的路徑
      service-id: microservice-provider-user            # Eureka中的serviceId

(5).如何忽略某些服務

// 啓動服務:microservice-discovery-eureka
// 啓動服務:microservice-provider-user
// 啓動服務:microservice-consumer-movie-ribbon

// 如果我們現在只想將microservice-consumer-movie-ribbon服務暴露給外部,microservice-provider-user不想暴露,那麼應該怎麼辦呢?

// 依然只是一點小小的配置即可:

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
eureka:
  instance:
    hostname: gateway
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
zuul:
  ignored-services: microservice-provider-user          # 需要忽視的服務(配置後將不會被路由)
  routes:
    movie:                                              # 可以隨便寫,在zuul上面唯一即可;當這裏的值 = service-id時,service-id可以不寫。
      path: /movie/**                                   # 想要映射到的路徑
      service-id: microservice-consumer-movie-ribbon-with-hystrix    # Eureka中的serviceId

// 這樣microservice-provider-user服務就不會被路由,microservice-consumer-movie-ribbon服務則會被路由。      

(7).描述來源於:http://book.itmuch.com/2%20Spring%20Cloud/2.6%20API%20Gateway.html

  1. 使用Zuul不使用Eureka
// Zuul並不依賴Eureka,可以脫離Eureka運行,此時需要配置

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
zuul:
  routes:
    movie:                                              # 可以隨便寫
      path: /user/**
      url: http://localhost:8000/                       # path路由到的地址,也就是訪問http://localhost:8050/user/**會路由到http://localhost:8000/**


// 我們可嘗試訪問http://localhost:8050/user/1 ,會發現被路由到了http://localhost:8000/1 。不過在大多數情況下,筆者並不建議這麼做,因爲得手動大量地配置URL,不是很方便。

// Zuul還支持更多的特性、更多的配置項甚至是定製開發,具體還請讀者自行發掘。

// Zuul只是API Gateway的一種實現,可作爲API Gateway的軟件有很多,譬如Nginx Plus、Kong等等

描述來源於:http://book.itmuch.com/2%20Spring%20Cloud/2.6%20API%20Gateway.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章