Udemy 課程Microservices with Spring Cloud 41
一樣到SpringIO
因為Zuul instance啟動後也要註冊在Eureka上面
並且將其匯入
一樣要解決pom檔的Unknow error
加入<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
並且改寫application.properties
然後在啟動用的NetflixZuulApiGetwayServerApplication多加兩個annotation
建立一個ZuulLoggingFilter的class並且extends ZuulFilter
會看到四個覆寫的Method
1.filterOrder
指filter的次序,這用在有多個filter情況下
目前我們就只建這個ZuulLoggingFilter class
所以先把return給1
2.shouldFilter
指這個class的filter功能是否啟用
目前當然先改為
3.filterType
有三種回傳結果,指filter什麼時候發動
pre:在request被call之前
post:在request被call之後
error:只在error request發生
目前先改為
4.run
所回傳的是真正的log,通常會回該Reuest的detail
所以要先把Request內容拿出來
另外先在前面把logger建出來,只是印在console來看
並決定不回傳
至此初步建置即完成
接著先把Eureka專案開起來
然後把下面三個專案都啟動
currency-conversion-service
currency-exchange-service
本專案netflix-zuul-api-getway-server
然後進到Eureka網址:http://localhost:8761/
確認currency-exchange-service專案的服務
進到:http://localhost:8087/currency-exchange/from/USD/to/INR
但是上面的使用沒有透過API getway-Zuul
要使用Zuul來取用別的專案服務,要更改網址
首先我們要透過Zuul當然開頭要用Zuul的網址
開頭http:localhost:8765
接著後面接上要取用的專案名
可以到Eureka來複製但要改小寫
或是到applicat.properties複製
所以現在網址改成
http:localhost:8765/currency-exchange-service
接著後面再串服務的網址後面的部分
所以最終的網址變為:
http:localhost:8765/currency-exchange-service/currency-exchange/from/USD/to/INR
但是現在有個問題
currency-conversion-service的服務是透過Eureka直接調用
currency-exchange-service的服務
在此狀況下,currency-conversion-service的服務應改由Zuul來調用
currency-exchange-service的服務
原本的currency-conversion-service內的proxy如下
要改為
並且打到透過Zuul的currency-conversion-service服務!!
同理一樣的網址:
http://localhost:8765/{服務的專案名}/{uri}
http://localhost:8765/currency-conversion-service/currency-converter-feign/from/EUR/to/INR/quantity/5