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

举报
小工匠 发表于 2021/09/11 00:50:39 2021/09/11
【摘要】 文章目录 官方文档Zuul概述引入网关前后调用流程的变化搭建单节点的ZuulStep1. 创建子Module microservice-gateway-zuulStep2. 添加maven依赖St...


在这里插入图片描述

官方文档

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依赖

		<!-- 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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

官方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

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);
	}

}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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


Step4. 配置文件application.yml

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}}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

观察上面的配置文件,是没有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的时候,可以看到如下日志

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]

  
 
  • 1
  • 2

访问 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即可。
在这里插入图片描述
增加配置,开启端点监控

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

重启下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

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/85850470

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200