前言
讀者:對spring、spring boot有一定了解
難度:初中級,旨在快速應用於項目
參考文獻:網絡、書籍、官方文檔,有任何錯誤歡迎大家留言拍磚指正
實戰模擬源碼:https://github.com/yhqnh/demo-springcloud,使用spring-cloud dalston版本
Spring cloud zuul對Netflix zuul進行了封裝實現
新建spring boot項目demo-springcloud-api-gateway,創建啓動類ZuulApplication,@EnableZuulProxy啓動網關代理的配置,它也是組合註解,啓用了服務註冊發現和熔斷器
項目關鍵依賴spring-cloud-starter-zuul對網關支持
配置文件application.properties,路由配置主要有傳統路由和服務路由,傳統路由就是ip和端口這種,服務路由就是使用服務名作爲路由。傳統路由我們配置爲/tradition/**,服務路由配置爲/serviceToClient和serviceToConsumer
啓動註冊中心,啓動服務提供者,啓動demo-springcloud-api-gateway,瀏覽器訪問http://localhost:4441/tradition/hello和http://localhost:4441/serviceToClient/hello和http://localhost:4441/serviceToConsumer/hello和http://localhost:4441/demo-springcloud-consumer/hello均正常訪問