簡單介紹一下, 其實 Zuul 是來自於 Netflix 的一個 軟體 閘道器, 那你一定會問....
用 Nginx 就好啦, 有比 HAProxy 快嗎?
軟體架構不是只有比快, 彈性也很重要, Zuul 透過我之前介紹自動發現機制, 他可以無須變更軟體組態, 只要服務發現新增了服務, 他就可以做一個內外部的代理功能, 其實自己也是 Service 的一種.
早期 Netflix 拿 Zuul 做 Gateway 的時候, Zuul 裡面也可以包含很多不同程度的功能 如 A/B 測試 金思雀部屬 等等..., 所以你大概可以想像, 他的客製化程度是很強的.
所以來看一下怎麼使用
使用到的組件如下
Web | Cloud Config | Cloud Discovery | Cloud Routing | Cloud Circuit Breaker | Cloud Tracing | Cloud Messaging | Ops |
---|---|---|---|---|---|---|---|
HATEOAS | Config Client | Eureka Discovery | Zuul | Hystrix | Zipkin | Stream Redis | Actuator |
下面是我們會使用到的一些依賴
build.gradle
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
compile('org.springframework.cloud:spring-cloud-starter-zuul')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
把範例的 application.properties 刪掉並新增 bootstrap.properties
spring.application.name=reservation-client
spring.cloud.config.uri=http://localhost:8888
起動程式
ReservationClientApplication.java
package com.example;
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;
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ReservationClientApplication {
public static void main(String[] args) {
SpringApplication.run(ReservationClientApplication.class, args);
}
}
在原先的範例程式上增加 @EnableZuulProxy 跟 @EnableDiscoveryClient
起動後即可 http://localhost:8050/reservation-service/reservations 取得原本 reservation-service 上的資料如下,可以很明顯的得出來多了一層 reservation-service 代理路徑
{
"_embedded": {
"reservations": [
{
"reservationName": "Dr. rod",
"_links": {
"self": {
"href": "http://localhost:8050/reservation-service/reservations/1"
},
"reservation": {
"href": "http://localhost:8050/reservation-service/reservations/1"
}
}
},
{
"reservationName": "Dr. Syer",
"_links": {
"self": {
"href": "http://localhost:8050/reservation-service/reservations/2"
},
"reservation": {
"href": "http://localhost:8050/reservation-service/reservations/2"
}
}
},
{
"reservationName": "Juergen",
"_links": {
"self": {
"href": "http://localhost:8050/reservation-service/reservations/3"
},
"reservation": {
"href": "http://localhost:8050/reservation-service/reservations/3"
}
}
},
{
"reservationName": "ALL THE COMMUNITY",
"_links": {
"self": {
"href": "http://localhost:8050/reservation-service/reservations/4"
},
"reservation": {
"href": "http://localhost:8050/reservation-service/reservations/4"
}
}
},
{
"reservationName": "Josh",
"_links": {
"self": {
"href": "http://localhost:8050/reservation-service/reservations/5"
},
"reservation": {
"href": "http://localhost:8050/reservation-service/reservations/5"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8050/reservation-service/reservations"
},
"profile": {
"href": "http://localhost:8050/reservation-service/profile/reservations"
},
"search": {
"href": "http://localhost:8050/reservation-service/reservations/search"
}
},
"page": {
"size": 20,
"totalElements": 5,
"totalPages": 1,
"number": 0
}
}