使用Spring Cloud Zuul實現動態路由

來源:互聯網
上載者:User
Zuul 是提供動態路由,監控,彈性,安全等的邊緣服務。Zuul 相當於是裝置和 Netflix 流應用的 Web 網站後端所有請求的前門。

Zuul 可以適當的對多個 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@SpringBootApplicationpublic 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;@RestControllerpublic 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.comzuul.routes.baidu.path=/baidu/**zuul.routes.baidu.url=http://www.baidu.com#反響代理配置#這裡的配置類似nginx的反響代理#當請求/api/**會直接交給listOfServers配置的伺服器處理#當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=falseapi.ribbon.listOfServers=192.168.1.100:8080,192.168.1.101:8080,192.168.1.102:8080#url重寫配置#這裡的配置,相當於訪問/index/** 會直接渲染/home的請求內容(和直接請求/home效果一樣), url地址不變zuul.routes.index.path=/index/**zuul.routes.index.url=forward:/home

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.