使用Spring Cloud Zuul实现动态路由

来源:互联网 发布:淘宝店铺年度运营计划 编辑:程序博客网 时间:2024/06/04 18:50
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

0 0
原创粉丝点击