SpringCloud-路由网关(Zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、只能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。
在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求首先经过负载均衡(zuul、Nginx),再到达服务网关(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如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?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.SpringCloud</groupId> <artifactId>service-zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-zuul</name> <description>service-zuul-description</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.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> <spring-cloud.version>Edgware.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> </project> |
在程序入口application类加上注解@EnableZuulProxy,开启zuul的功能
1 2 3 4 5 6 7 8 9 | @EnableZuulProxy @EnableEurekaClient @SpringBootApplication public class ServiceZuulApplication { public static void main(String[] args) { SpringApplication.run(ServiceZuulApplication. class , args); } } |
application.yml如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | spring: application: name: service-zuul server: port: 8769 eureka: client: serviceUrl: defaultZone: http: //localhost:8761/eureka/ zuul: routes: api-a: path: /api-a/** serviceId: service-ribbon api-b: path: /api-b/** serviceId: service-feign |
以/api-a/开头的请求都转发给service-ribbon服务,以/api-b/开头的请求都转发给service-feign服务
启动eureka-server, eureka-client,service-ribbon,service-feign,service-zuul
请求http://localhost:8769/api-a/hi?name=fz
http://localhost:8769/api-b/hi?name=fz
这说明zuul起到了路由的作用
服务过滤
zuul不仅只是路由,并且还能过滤,做一些安全验证,如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | @Component public class MyFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(MyFilter. class ); @Override public String filterType() { return "pre" ; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true ; } @Override public 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) { e.printStackTrace(); } return null ; } log.info( "ok" ); return null ; } } |
filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四中不同生命周期的过滤器类型,具体如下:
pre : 路由之前
routing : 路由之时
post:路由之后
error:发送错误调用
filterOrder:过滤的顺序
shouldFilter:这里可以下逻辑判断,是否需要过滤,true,永远过滤
run:过滤器的具体逻辑。可用很复杂,包括查SQL,nosql去判断该请求到底有没有权限访问。
重启项目测试
加入token
成功
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示