Spring Cloud Zuul
Spring Cloud Zuul路由是微服務架構的不可或缺的一部分,提供動態路由,監控,彈性,安全等的邊緣服務。Zuul是Netflix出品的一個基於JVM路由和服務端的負載均衡器。
下面我們通過代碼來了解Zuul是如何工作的
簡單使用
1、添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
引入spring-cloud-starter-zuul包
2、配置文件
spring.application.name=gateway-service-zuul
server.port=8888
#這裏的配置表示,訪問/it/** 直接重定向到http://www.ityouknow.com/**
zuul.routes.baidu.path=/it/**
zuul.routes.baidu.url=http://www.ityouknow.com/
3、啓動類
@SpringBootApplication
@EnableZuulProxy
public class GatewayServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServiceZuulApplication.class, args);
}
}
啓動類添加@EnableZuulProxy,支持網關路由。
史上最簡單的zuul案例就配置完了
4、測試
啓動gateway-service-zuul-simple項目,在瀏覽器中訪問:http://localhost:8888/it/spring-cloud,看到頁面返回了:http://www.ityouknow.com/spring-cloud 頁面的信息。
我們以前面文章的示例代碼spring-cloud-producer爲例來測試請求的重定向,在配置文件中添加:
zuul.routes.hello.path=/hello/**
zuul.routes.hello.url=http://localhost:9000/
啓動spring-cloud-producer,重新啓動gateway-service-zuul-simple,訪問:http://localhost:8888/hello/hello?name=%E5%B0%8F%E6%98%8E,返回:hello 小明,this is first messge
說明訪問gateway-service-zuul-simple的請求自動轉發到了spring-cloud-producer,並且將結果返回。
服務化
通過url映射的方式來實現zull的轉發有侷限性,比如每增加一個服務就需要配置一條內容,另外後端的服務如果是動態來提供,就不能採用這種方案來配置了。實際上在實現微服務架構時,服務名與服務實例地址的關係在eureka server中已經存在了,所以只需要將Zuul註冊到eureka server上去發現其他服務,就可以實現對serviceId的映射。
我們結合示例來說明,在上面示例項目gateway-service-zuul-simple的基礎上來改造。
1、添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
增加spring-cloud-starter-eureka包,添加對eureka的支持。
2、配置文件
配置修改爲:
spring.application.name=gateway-service-zuul
server.port=8888
zuul.routes.api-a.path=/producer/**
zuul.routes.api-a.serviceId=spring-cloud-producer
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3、測試
依次啓動 spring-cloud-eureka、 spring-cloud-producer、gateway-service-zuul-eureka,訪問:http://localhost:8888/producer/hello?name=%E5%B0%8F%E6%98%8E,返回:hello 小明,this is first messge
說明訪問gateway-service-zuul-eureka的請求自動轉發到了spring-cloud-producer,並且將結果返回。
爲了更好的模擬服務集羣,我們複製spring-cloud-producer項目改爲spring-cloud-producer-2,修改spring-cloud-producer-2項目端口爲9001,controller代碼修改如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is two messge";
}
}
修改完成後啓動spring-cloud-producer-2,重啓gateway-service-zuul-eureka。測試多次訪問http://localhost:8888/producer/hello?name=%E5%B0%8F%E6%98%8E,依次返回:
hello 小明,this is first messge
hello 小明,this is two messge
hello 小明,this is first messge
hello 小明,this is two messge
說明通過zuul成功調用了producer服務並且做了均衡負載。
網關的默認路由規則
但是如果後端服務多達十幾個的時候,每一個都這樣配置也挺麻煩的,spring cloud zuul已經幫我們做了默認配置。默認情況下,Zuul會代理所有註冊到Eureka Server的微服務,並且Zuul的路由規則如下:http://ZUUL_HOST:ZUUL_PORT/微服務在Eureka上的serviceId/**會被轉發到serviceId對應的微服務。
我們註銷掉gateway-service-zuul-eureka項目中關於路由的配置:
#zuul.routes.api-a.path=/producer/**
//社交電商平臺源碼請加企鵝求求:一零三八七七四六二六。
#zuul.routes.api-a.serviceId=spring-cloud-producer
重新啓動後,訪問http://localhost:8888/spring-cloud-producer/hello?name=%E5%B0%8F%E6%98%8E,測試返回結果和上述示例相同,說明Spring cloud zuul默認已經提供了轉發功能。
到此zuul的基本使用我們就介紹完了。關於zuul更高級使用,我們下篇再來介紹。