Spring Cloud中的动态路由怎么利用 Zuul实现

发布时间:2020-12-04 15:56:35 作者:Leah
来源:亿速云 阅读:200

今天就跟大家聊聊有关Spring Cloud中的动态路由怎么利用 Zuul实现,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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 
@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配置的服务器处理 
#当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的请求内容(和直接请求/home效果一样), url地址不变 
zuul.routes.index.path=/index/** 
zuul.routes.index.url=forward:/home 

看完上述内容,你们对Spring Cloud中的动态路由怎么利用 Zuul实现有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. 基于Spring Cloud Gateway的路由实践
  2. Spring Cloud Zuul路由规则动态更新的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springcloud zuul 动态路由

上一篇:Commons lang组件如何在Java项目中运用

下一篇:数组中的重复数据怎么利用Java去除

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》