转:https://blog.csdn.net/forezp/article/details/69939114
最新版本:
史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)(Finchley版本):https://blog.csdn.net/forezp/article/details/81041012

在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个简答的微服务系统如下图:


注意:A服务和B服务是可以相互调用的,作图的时候忘记了。并且配置服务也是注册到服务注册中心的。

在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求首先经过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),然后再到具体的服。,服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理(下一篇文章讲述),配置服务的配置文件放在git仓库,方便开发人员随时改配置。

一、Zuul简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。zuul有以下功能:Authentication
Insights
Stress Testing
Canary Testing
Dynamic Routing
Service Migration
Load Shedding
Security
Static Response handling
Active/Active traffic management

二、准备工作

继续使用上一节的工程。在原有的工程上,创建一个新的工程。

三、创建service-zuul工程

其pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forezp</groupId><artifactId>service-zuul</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-zuul</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>
</project>

在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {public static void main(String[] args) {SpringApplication.run(ServiceZuulApplication.class, args);}
}

加上配置文件application.yml加上以下的配置代码:

eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
server:port: 8769
spring:application:name: service-zuul
zuul:routes:api-a:path: /api-a/**serviceId: service-ribbonapi-b:path: /api-b/**serviceId: service-feign

首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8769,服务名为service-zuul;以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务;
依次运行这五个工程;打开浏览器访问:http://localhost:8769/api-a/hi?name=forezp ;浏览器显示:

hi forezp,i am from port:8762

打开浏览器访问:http://localhost:8769/api-b/hi?name=forezp ;浏览器显示:

hi forezp,i am from port:8762

这说明zuul起到了路由的作用

四、服务过滤

zuul不仅只是路由,并且还能过滤,做一些安全验证。继续改造工程;

@Component
public class MyFilter extends ZuulFilter{private static Logger log = LoggerFactory.getLogger(MyFilter.class);@Overridepublic String filterType() {return "pre";}@Overridepublic int filterOrder() {return 0;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run() {RequestContext ctx = RequestContext.getCurrentContext();HttpServletRequest request = ctx.getRequest();log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));Object accessToken = request.getParameter("token");if(accessToken == null) {log.warn("token is empty");ctx.setSendZuulResponse(false);ctx.setResponseStatusCode(401);try {ctx.getResponse().getWriter().write("token is empty");}catch (Exception e){}return null;}log.info("ok");return null;}
}

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:

pre:路由之前
routing:路由之时
post: 路由之后
error:发送错误调用
filterOrder:过滤的顺序
shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。

这时访问:http://localhost:8769/api-a/hi?name=forezp ;网页显示:

token is empty

访问 http://localhost:8769/api-a/hi?name=forezp&token=22 ;
网页显示:

hi forezp,i am from port:8762

本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter5

五、参考资料

http://projects.spring.io/spring-cloud/spring-cloud.html#_router_and_filter_zuul

史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)相关推荐

  1. 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)

    最新Finchley版本请访问: https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/ 或者 http://blog.csd ...

  2. 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

    转:https://blog.csdn.net/forezp/article/details/70037291 最新版本: 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spr ...

  3. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)

    转:https://blog.csdn.net/forezp/article/details/69934399 最新版本: 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix) ...

  4. 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)

    转:https://blog.csdn.net/forezp/article/details/69808079 最新版本: 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign ...

  5. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)--有BUG,注意看我的备注

    转载请标明出处:  http://blog.csdn.net/forezp/article/details/69934399  本文出自方志朋的博客 在微服务架构中,根据业务来拆分成一个个的服务,服务 ...

  6. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)--里面有BUG,所以我转载改一下

    017年04月09日 21:14:05 阅读数:271535 转载请标明出处:  http://blog.csdn.net/forezp/article/details/69934399  本文出自方 ...

  7. 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/81041045 本文出自方志朋的博客 个人博客纯净版:https://www.fangzhi ...

  8. 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心

    转自:https://blog.csdn.net/forezp/article/details/81041101 文章 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eu ...

  9. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/81040990 本文出自方志朋的博客 个人博客纯净版:https://www.fangzhi ...

最新文章

  1. 教你用U盘安装Ubuntu
  2. python编程题-基本编程题 --python
  3. shell `-c`参数 如何使用
  4. windows7下修改hosts文件无效解决办法
  5. Django框架实现支付宝第三方支付
  6. Dapr + .NET 实战(九)本地调试
  7. (转ORCLE导入导出命令)
  8. HOG + SVM 实现图片分类(python3)
  9. rto初始化和计算_TCP系列15—重传—5、Linux中RTO的计算
  10. ★LeetCode(429)——N叉树的层序遍历(JavaScript)
  11. BZOJ.2555.SubString(后缀自动机 LCT)
  12. 后台运行 命令_Docker基本命令
  13. 【译】给小白准备的Web架构基础知识
  14. 最实用的自媒体爆文采集工具,简单易出爆文
  15. .NET反编译工具:de4dot
  16. 博客导航——一站式搜索
  17. Tomcat 服务器搭建
  18. 入行10年后,我总结了这份FPGA学习路线
  19. SCL译码matlab,CRC_polar_SC_SCL 码的SC译码算法,可设置不同信噪比, 码长 改为SCL 和CA 。核心 matlab 272万源代码下载- www.pudn.com...
  20. 面向对象程序设计c 语言,C 语言如何实现面向对象编程

热门文章

  1. python 多个配置文件_django中如何如何针对不同的环境使用多个配置文件?
  2. python3环境下用matplotlib库实现UI交互
  3. VTK:图像正弦曲线用法实战
  4. OpenCASCADE:使用 扩展数据交换XDE之编辑形状
  5. boost::sort::pdqsort相关的测试程序
  6. boost::range模块replaced_if相关的测试程序
  7. boost::hana::fold_right用法的测试程序
  8. boost::integer::mod_inverse用法的测试程序
  9. bgss::spherical_side_formula用法的测试程序
  10. boost::fibers::shared_future的用法实例