利用Spring Cloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由示例代碼
前言
本文主要給大家介紹了關(guān)于Spring Cloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
Zuul 是提供動(dòng)態(tài)路由,監(jiān)控,彈性,安全等的邊緣服務(wù)。Zuul 相當(dāng)于是設(shè)備和 Netflix 流應(yīng)用的 Web 網(wǎng)站后端所有請(qǐng)求的前門。
Zuul 可以適當(dāng)?shù)膶?duì)多個(gè) Amazon Auto Scaling Groups 進(jìn)行路由請(qǐng)求。
首先新建maven項(xiàng)目,加入如下依賴
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>1.1.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies>
package com.pp.zuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }
package com.pp.zuul; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @RequestMapping("/index") public Object index() { return "index"; } @RequestMapping("/home") public Object home() { return "home"; } }
配置文件:application.properties
server.port=8181 #這里的配置表示,訪問(wèn)/baidu/** 直接重定向到http://www.baidu.com zuul.routes.baidu.path=/baidu/** zuul.routes.baidu.url=http://www.baidu.com #反響代理配置 #這里的配置類似nginx的反響代理 #當(dāng)請(qǐng)求/api/**會(huì)直接交給listOfServers配置的服務(wù)器處理 #當(dāng)stripPrefix=true的時(shí)候 (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/user/list) #當(dāng)stripPrefix=false的時(shí)候(http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/api/user/list) zuul.routes.api.path=/api/** zuul.routes.api.stripPrefix=false api.ribbon.listOfServers=192.168.1.100:8080,192.168.1.101:8080,192.168.1.102:8080 #url重寫配置 #這里的配置,相當(dāng)于訪問(wèn)/index/** 會(huì)直接渲染/home的請(qǐng)求內(nèi)容(和直接請(qǐng)求/home效果一樣), url地址不變 zuul.routes.index.path=/index/** zuul.routes.index.url=forward:/home
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Spring Cloud 網(wǎng)關(guān)服務(wù) zuul 動(dòng)態(tài)路由的實(shí)現(xiàn)方法
- SpringCloud Zuul實(shí)現(xiàn)動(dòng)態(tài)路由
- springcloud Zuul動(dòng)態(tài)路由的實(shí)現(xiàn)
- 詳解Spring Cloud Zuul中路由配置細(xì)節(jié)
- SpringCloud實(shí)戰(zhàn)小貼士之Zuul的路徑匹配
- Spring Cloud OAuth2 實(shí)現(xiàn)用戶認(rèn)證及單點(diǎn)登錄的示例代碼
- Spring Cloud Zuul路由規(guī)則動(dòng)態(tài)更新解析
相關(guān)文章
Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過(guò)程解析
這篇文章主要介紹了Mybatis實(shí)現(xiàn)插入數(shù)據(jù)后返回主鍵過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06JDK動(dòng)態(tài)代理過(guò)程原理及手寫實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了JDK動(dòng)態(tài)代理過(guò)程原理及手寫實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
MyBatisFlex是一款優(yōu)秀的持久層框架,本文主要介紹了SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06