首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Spring Cloud【Finchley】-14 微服务网关Zuul的搭建与使用

Spring Cloud【Finchley】-14 微服务网关Zuul的搭建与使用

作者头像
小小工匠
发布2021-08-17 15:40:02
发布2021-08-17 15:40:02
5670
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构

文章目录

官方文档

https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_router_and_filter_zuul


Zuul概述

Zuul的主要功能是路由转发和过滤器

路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。


引入网关前后调用流程的变化

在微服务架构中,后端服务往往不直接开放给调用端,而是通过一个API网关根据请求的url,路由到相应的服务。网关直接与调用方通信进行权限控制,后将请求均衡分发给后台服务端

简单画2个图,来说明下引入网关后,调用流程的变化。

不使用网关的情况:

引入网关后:


搭建单节点的Zuul

这里我们会把zuul注册到Eureka上

Step1. 创建子Module microservice-gateway-zuul


Step2. 添加maven依赖

代码语言:javascript
复制
		<!-- eureka client 依赖  . Eureka不是必须的 ,在没有注册中心的情况下,也可以使用zuul-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	
		<!-- zuul 依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>

官方Note: the Zuul starter does not include a discovery client, so, for routes based on service IDs, you need to provide one of those on the classpath as well (Eureka is one choice). 因为我们使用serverID去做路由,所以我们这里引入了Eureka


Step3. 启动类添加注解 @EnableZuulProxy

代码语言:javascript
复制
package com.artisan.microservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

}

通过注解@EnableZuulProxy声明一个zuul代理,这个代理整合了Ribbon来定位注册在Eureka上的微服务,同时还整合了hystrix实现容错,所有经过zuul的请求都会在Hystrix命令中执行。


Step4. 配置文件application.yml

代码语言:javascript
复制
server:
  port: 4534
  
spring:
  application:
    name: microservice-gateway-zuul

eureka:
  client:
    service-url:
      defaultZone: http://artisan:artisan123@localhost:8761/eureka
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

观察上面的配置文件,是没有zuul相关的配置的,我们仅仅添加了一个zuul的依赖,同时将zuul注册到Eureka上。 至此,一个单节点的最精简版的zuul就搭建完成了,当然了zuul支持各种配置,我们的这个demo只是没有用到而已。


Step6. 网关功能-路由规则测试

  1. 启动注册中心Eureka Server 项目 microservice-discovery-eureka
  2. 启动服务提供者micorservice-provider-user
  3. 启动服务消费者 micorservice-consumer-movie-ribbon
  4. 启动zuul网关microservice-gateway-zuul

启动zuul的时候,可以看到如下日志

代码语言:javascript
复制
Mapped URL path [/microservice-provider-user/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
Mapped URL path [/micorservice-consumer-movie-ribbon/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]

访问 Eureka Server页面 http://localhost:8761/ ,查看服务注册情况

验证下路由转发的功能

通过配置文件,我们知道zuul服务的启动端口为 4534 ,

通过zuul访问服务提供者提供的服务看下 http://localhost:4534/microservice-provider-user/user/3

url中的microservice-provider-user为注册在eureka上的微服务的名称

服务被转发到了microservice-provider-user微服务中 ,相当于请求 http://localhost:8900/user/3

同理,通过zuul访问服务消费者 http://localhost:4534/micorservice-consumer-movie-ribbon/movie/4

服务被转发到了micorservice-consumer-movie-ribbon微服务中 ,相当于请求 http://localhost:7902/movie/4

默认情况下,zuul会代理所有注册在Eureka Server上的微服务,并且Zuul的路由规则为 http://zuul_host:zuul_port/微服务在EurekaServer上的serviceId/** 被转发到serviceId对应的微服务上。


Step7. 网关功能-负载均衡测试

  1. 启动注册中心Eureka Server 项目 microservice-discovery-eureka
  2. 启动多个服务提供者micorservice-provider-user ,在sts中换个端口,可启动多个,再加个8901端口上的服务
  3. 启动服务消费者 micorservice-consumer-movie-ribbon
  4. 启动zuul网关microservice-gateway-zuul

访问 Eureka Server页面 http://localhost:8761/ ,查看服务注册情况

访问两次服务提供者提供的服务,观察后台日志 http://localhost:4534/microservice-provider-user/user/3 ,

8900:

8901:

说明zuul整合了Ribbon负载均衡的功能


Step8. 网关功能-Hystrix监控测试

根据前几篇的学习 Spring Cloud【Finchley】-10Hystrix监控 我们知道要想实现Hystrix监控中,必须要有如下几个依赖

查看zuul微服务的pom依赖

前两个具备了,只需要修改下applicaiton.yml即可。

增加配置,开启端点监控

代码语言:javascript
复制
#actuator  启用所有的监控端点 “*”号代表启用所有的监控端点,可以单独启用,例如,health,info,metrics
#  spring boot 升为 2.0 后,为了安全,默认 Actuator 只暴露了2个端点,heath 和 info
management:
  endpoints:
    web:
      exposure:
        include: "*" 
  endpoint:
      health:
        show-details: ALWAYS

重启下microservice-gateway-zuul微服务

访问zuul的hystrix.stream http://localhost:4534/actuator/hystrix.stream

说明application.yml中的配置生效了。 ping 说明还未有服务调用,接下来调用下服务 ,就可以看到了

接下来我们用Dashboard来直观的看下

访问micorservice-hystrix-dashboard提供的页面 http://localhost:8888/hystrix

地址输入zuul服务的 hystrix stream地址 http://localhost:4534/actuator/hystrix.stream , title 任意,点击Monitor Stream

多访问几次 http://localhost:4534/microservice-provider-user/user/3 ,观察数据的变化 ,如下

通过以上示例,说明zuul已经整合了Hystrix监控。 容错后面来单独讨论


代码

https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-gateway-zuul

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/01/05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 官方文档
  • Zuul概述
  • 引入网关前后调用流程的变化
  • 搭建单节点的Zuul
    • Step1. 创建子Module microservice-gateway-zuul
    • Step2. 添加maven依赖
    • Step3. 启动类添加注解 @EnableZuulProxy
    • Step4. 配置文件application.yml
    • Step6. 网关功能-路由规则测试
    • Step7. 网关功能-负载均衡测试
    • Step8. 网关功能-Hystrix监控测试
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档