利用Spring Cloud Zuul實現(xiàn)動態(tài)路由示例代碼
前言
本文主要給大家介紹了關(guān)于Spring Cloud Zuul實現(xiàn)動態(tài)路由的相關(guān)內(nèi)容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
Zuul 是提供動態(tài)路由,監(jiān)控,彈性,安全等的邊緣服務(wù)。Zuul 相當于是設(shè)備和 Netflix 流應(yīng)用的 Web 網(wǎng)站后端所有請求的前門。
Zuul 可以適當?shù)膶Χ鄠€ Amazon Auto Scaling Groups 進行路由請求。
首先新建maven項目,加入如下依賴
<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 #這里的配置表示,訪問/baidu/** 直接重定向到http://www.baidu.com zuul.routes.baidu.path=/baidu/** zuul.routes.baidu.url=http://www.baidu.com #反響代理配置 #這里的配置類似nginx的反響代理 #當請求/api/**會直接交給listOfServers配置的服務(wù)器處理 #當stripPrefix=true的時候 (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/user/list) #當stripPrefix=false的時候(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重寫配置 #這里的配置,相當于訪問/index/** 會直接渲染/home的請求內(nèi)容(和直接請求/home效果一樣), url地址不變 zuul.routes.index.path=/index/** zuul.routes.index.url=forward:/home
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Mybatis實現(xiàn)插入數(shù)據(jù)后返回主鍵過程解析
這篇文章主要介紹了Mybatis實現(xiàn)插入數(shù)據(jù)后返回主鍵過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06SpringBoot使用MyBatis-Flex實現(xiàn)靈活的數(shù)據(jù)庫訪問
MyBatisFlex是一款優(yōu)秀的持久層框架,本文主要介紹了SpringBoot使用MyBatis-Flex實現(xiàn)靈活的數(shù)據(jù)庫訪問,具有一定的參考價值,感興趣的可以了解一下2024-06-06