本節將基於Zuul來實現API網關。作為Spring Cloud 的一部分,集成Zuul會變得非常簡單。
Zuul簡介
路由是微服務架構中必需的一部分,如「」可能映射到Web程序上、「/api/users」可能映射到用戶服務上、「/api/shop」可能映射到商品服務商。通過路由,讓不同的服務都集中到統一的入口上來,這就是API網關的作用。
Zuul是Netflix出品的一個基於JVM路由和服務端的負載均衡器。
Zuul 功能如下。
Zuul的規則引擎允許通過任何JVM語言來編寫規則和過濾器,支持基於Java和Groovy的構建。
在micro-weather-cureka-client的基礎上稍作修改,即可成為一個新的應用micro-weather-cure-ka-client-zuul,將其作為示例。
所需環境
為了演示本例,需要採用如下開發環境。
.JDK8。
. Gradle 4.0。
Spring Boot 2.0.0.M3。
.Spring Cloud Starter Netflix Eureka Client Finchley.M2。
.Spring Cloud Starter Netflix Zuul Finchley.M2。
更改配置
要使用Zuul,最簡單的方式莫過於添加Zuul依賴。
dependencies{/l...//添加Spring Cloud Starter Netflix Zuul依賴compile(&39;)}
使用Zuul
要啟用Zuul,最簡單的方式就是在應用的根目錄Application類上添加org.springframework.cloud.netflix.zuul.EnableZuulProxy註解。
package com.waylau.spring.cloud.weather;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;/**主應用程式.**@since 1.0.0 2017年11月05日* author <a href=&34;>Way Lau</a>*/@SpringBootApplicationEnableDiscoveryClient@EnableZuulPrOxypublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}}
其中,@EnableZuulProxy啟用了Zuul作為反向代理伺服器。
最後,修改application.properties。修改為如下配置。
spring.application.name: micro-weather-eureka-client-zuuleureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/zuul.routes.hi.path: /hi/**zuul.routes.hi.serviceId: micro-weather-eureka-client
其中:
運行和測試
啟動在之前章節中創建的micro-weather-eureka-server和micro-weather-eureka-client兩個項目,以及本例的micro-weather-eureka-client-zuul項目。
如果一切正常,那麼micro-weather-eureka-server運行的管理界面能看到上述服務的信息。
通過瀏覽器訪問micro-weather-zuul服務(本例地址為http:/localhost:8080),當試圖訪問接口時,如果一切正常,可以在控制臺看到「Hello world」字樣,這就是轉發請求到micro-weather-eu-reka-client服務時響應的內容,如圖10-3所示。
源碼
本節示例所涉及的源碼,見micro-weather-cureka-server、micro-weather-cureka-client,以及mi-cro-weather-cureka-client-zuul。
本節將在天氣預報系統中使用API網關。
下面基於Zuul來實現API網關,由這個API網關來處理所有的用戶請求。API網關將根據不同的請求路徑,將請求路由到不同的微服務中去。
之前的天氣預報微服務msa-weather-report-eureka-feign最初是依賴於天氣數據API微服務及城市數據API微服務。現在把這兩個API微服務都合併到了API網關中,由API網關來負責請求的轉發。那麼,最後新的天氣預報微服務就只需要依賴於API網關即可。這裡將新的應用命名為msa-weather-report-eureka-feign-gatewayo
在前面創建的micro-weather-eureka-client-zuul應用基礎之上,再創建一個新的應用msa-weath-er-eureka-client-zuul,作為本節的API網關示例程序。
配置API網關
修改msa-weather-eureka-client-zuul的 application.properties配置文件。修改為如下配置。
spring.application.name: msa-weather-eureka-client-zuuleureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/zuul.routes.city.path:/city/**zuul.routes.city.serviceId: msa-weather-city-eurekazuul.routes.data.path:/data/**zuul.routes.data.serviceId:msa-weather-data-eureka
圖10-4所示的是API網關的路由規則:當訪問的路徑匹配「city」時,則API網關將請求轉發到msa-weather-city-eureka微服務中去;當訪問的路徑匹配「data」時,則API網關將請求轉發到msa-weather-data-eureka微服務中去。
修改新的天氣預報微服務
在msa-weather-report-cureka-feign的基礎上稍作修改,就能成為新版的msa-weather-report-cu-reka-feign-gateway。
主要的修改項集中在Feign&34;https://waylau.com&34;msa-weather-eureka-client-zuul&34;/city/cities&34;/data/weather/cityId/{cityId}&34;cityId&34;https://waylau.com&34;https://waylau.com&34;/report&34;/cityId/{cityId}&34;cityId&34;獲取城市信息異常!&34;獲取城市信息異常!&34;title&34;老衛的天氣預報&34;cityId&34;cityList&34;report&34;weather/report&34;reportModel&熱部署靜態文件spring.thymeleaf.cache=falsespring.application.name: msa-weather-report-eureka-feign-gatewayeureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/feign.client.config.feignName.connectTimeout:5000feign.client.config.feignName.readTimeout: 5000
運行微服務實例
首先運行Eureka Server實例micro-weather-eureka-server,它在8761埠啟動。
其次要運行Redis伺服器。
而後分別在8081和8082上啟動msa-weather-collection-cureka-feign實例兩個,在8083和8084上啟動msa-weather-data-eureka實例兩個,在8085和 8086上啟動msa-weather-city-eureka實例兩個,在8087和8088上啟動msa-weather-report-cureka-feign-gateway實例兩個,在8089上啟動msa-weather-cureka-client-zuul API 網關實例。啟動腳本如下。
java-jar msa-weather-collection-eureka-feign-1.0.0.jar --server.port=8081java-jar msa-weather-collection-eureka-feign-1.0.0.jar--server.port=8082java -jar msa-weather-data-eureka-1.0.0.jar --server.port=8083java -jar msa-weather-data-eureka-1.0.0.jar --server.port=8084java -jar msa-weather-city-eureka-1.0.0.jar --server.port=8085java -jar msa-weather-city-eureka-1.0.0.jar--server.port=8086java-jar msa-weather-report-eureka-feign-gateway-1.0.0.jar --server.port=8087java -jar msa-weather-report-eureka-feign-gateway-1.0.0.jar --server.port=8088java-jar msa-weather-eureka-client-zuul-1.0.0.jar --server.port=8089
這樣,就可以在Eureka Server上看到這8個實例的信息。訪問http:/localhost:8761,可以看到如圖9-3所示的Eureka Server自帶的UI管理界面。
訪問天氣預報微服務的任意一個實例,都能夠正常使用天氣數據微服務和城市數據微服務。如在瀏覽器訪問其中一個實例( http://localhost:8088/report/cityld/101280601)來進行測試。
源碼
本節示例所涉及的源碼,見micro-weather-eureka-server、msa-weather-collection-cureka-feign、msa-weather-data-eureka、msa-weather-city-eureka、msa-weather-report-eureka-feign-gateway .micro-weather-eureka-client-zuul,以及msa-weather-eureka-client-zuul。