一、什么是Zuul 服务网关

Zuul 是 Netflix 提供的⼀个开源的 API ⽹关服务器,是客户端和⽹站后端所有请求的中间层,对外开放 ⼀个 API,将所有请求导⼊统⼀的⼊⼝,屏蔽了服务端的具体实现逻辑,Zuul 可以实现反向代理的功 能,在⽹关内部实现动态路由、身份认证、IP 过滤、数据监控等。Zuul也是Spring Cloud集成的组件,通过它来实现服务网关。

二、Zuul的功能列表

\1. 身份认证与安全:识别每个资源的验证要求,并拒绝那些与要求不符合的请求。

\2. 审查与监控:在边缘位置追踪有意义的数据和统计结果,从而带来精确的生产视图。

\3. 动态路由:动态的将请求路由到不同的后端集群。

\4. 压力测试:逐渐增加指向集群的流量,以了解性能。

\5. 负载分配:为每一种负载类型分配对应容量,并弃用超出限定值的请求。

\6. 静态响应处理:在边缘位置直接建立部分响应,从而避免其转发到内部集群。

\7. 多区域弹性:跨越AWS Region进行请求路由,旨在实现ELB(Elastic Load Balancing)使用的多样化,以及让系统的便越更贴近系统的使用者。

三、实战!

1.创建maven工程,pom.xml配置如下:

    <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>

2.创建配置文件application.yml配置如下:

server:
port: 8030
spring:
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
provider: /p/**

属性说明

* zuul.routes.provider: 给服务提供者 provider 设置映射,可以不需要再记住服务提供者的端口

3.创建启动类,代码如下:

package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy
@EnableAutoConfiguration
public class ZuulApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ZuulApplication.class, args);
} }

注解说明

* @EnableZuulProxy : 包含了 @EnableZuulServer ,设置该类是⽹关的启动类。

* @EnableAutoConfiguration :可以帮助 Spring Boot 应⽤将所有符合条件的 @Configuration 配置加载到当前 Spring Boot 创建并使⽤的 IOC 容器中。

四、Zuul的负载均衡

1.修改服务提供者的 controller 层代码,具体如下:

package com.zing.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.zing.entity.Student;
import com.zing.repository.StudentRepository; @RestController //在Spring中@RestController的作用等同于@Controller + @ResponseBody。
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository; @Value("${server.port}")
private String port; @GetMapping("/findAll")
public Collection<Student> findAll(){
return studentRepository.findAll();
} @GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id) {
return studentRepository.findById(id);
} @PostMapping("/save")
public void save(@RequestBody Student s) {
studentRepository.saveOrUpdate(s);
} @PutMapping("/Update")
public void Update(@RequestBody Student s) {
studentRepository.saveOrUpdate(s);
} @DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id) {
studentRepository.deleteById(id);
} @GetMapping("/index")
public String index() {
return "当前端口:" + this.port;
} }

注解说明

* @Value("${server.port}"):获取当前项目application.yml配置文件中的server.port属性值。

2.首先启动注册中心,然后启动服务提供者,修改端口为8011后,再建一个启动类再次启动,我们就可以在注册中心看到两个prider服务,如图:

3.启动zuul的启动类,我们可以访问 http://localhost:8030/p/student/index查看调用服务的端口,我们不断的刷新,可以看到端口是交替出现的:

五、总结

zuul 服务网关是微服务架构中不可或缺的的部分。通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由、负载均衡之外,它还具备了权限控制等功能。

Spring Cloud05: Zuul 服务网关的更多相关文章

  1. Spring Cloud 系列之 Netflix Zuul 服务网关

    什么是 Zuul Zuul 是从设备和网站到应用程序后端的所有请求的前门.作为边缘服务应用程序,Zuul 旨在实现动态路由,监视,弹性和安全性.Zuul 包含了对请求的路由和过滤两个最主要的功能. Z ...

  2. spring cloud: zuul: 微网关-简单使用与路由配置

    spring cloud: zuul: 微网关-简单使用与路由配置 首先引入依赖 <dependency> <groupId>org.springframework.cloud ...

  3. Spring Cloud (14) 服务网关-过滤器

    Spring Cloud Zuul作为网关所具备的最基本的功能:路由,还具备另外一个核心的功能:过滤器. 过滤器 通过Spring Cloud Zuul实现的路由功能,我们的微服务可以通过统一的API ...

  4. Spring Cloud Gateway 服务网关快速上手

    Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 ...

  5. Spring Cloud Zuul与网关中间件

    Spring Cloud Zuul与网关中间件_网易订阅 http://dy.163.com/v2/article/detail/DC7L8UV10511HSJK.html

  6. spring cloud 系列第6篇 —— zuul 服务网关 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.zuul简介 1.1 API 网关 api 网关是整个微服务系统的门面 ...

  7. Spring Cloud (13) 服务网关-路由配置

    传统路由配置 所谓传统路由配置方式就是在不依赖于服务发现机制情况下,通过在配置文件中具体制定每个路由表达式与服务实例的映射关系来实现API网关对外部请求的路由.没有Eureka服务治理框架帮助的时候, ...

  8. Spring Cloud (12) 服务网关-基础

    通过前几篇介绍,已经可以构建一个简单的微服务架构了,如下图: 通过eureka实现服务注册中心以及服务注册发现,通过ribbon或feign实现服务的消费以及负载均衡,通过spring cloud c ...

  9. Spring Cloud 之 服务网关

    在微服务架构体系中,使用API 服务网关后的系统架构图如下: API服务网关的主要作用如下: 服务访问的统一入口 服务访问的负载均衡功能 服务访问的路由功能 在SpringCloud中,基于Netfl ...

随机推荐

  1. POJ3687拓扑排序+贪心

    题意:       给你n个求,他们的重量是1-n(并不是说1号求的重量是1...),然后给你m组关系a,b,表示a的重量小于b的重量,然后让你输出满足要求的前提下每个球的重量,要求字典序最小. 思路 ...

  2. Tomcat管理弱口令页面Getshell

    目录 弱口令Getshell 利用Burpsuite对tomcat账号密码进行爆破 弱口令Getshell Tomcat安装完成后会有如下页面,点击该页面的 Manager App 处会弹出输入用户名 ...

  3. 16.PHP_Ajax模拟服务器登录验证

    Ajax模拟登陆验证 index.php <script language="javascript">    var http_request = false;     ...

  4. C++ Socket 简单封装

    以下代码一部分来自于<网络多人游戏架构与编程>, 其它的都是我瞎写的. 备忘. 一个简单的Socket封装,没有做什么高级的操作(比如IO完成端口等等). 1 #pragma once 2 ...

  5. 一个或多个筛选器或者Listeners启动失败 的问题探索以及解决方案

    2020年10月9日更新 经过本人对SSM框架理解的加深和对IDEA工具使用的熟悉,现提出一种新的解决办法,以前的解决办法可能存在问题 1. 问题描述: 使用IDEA作为开发工具,使用Maven作为项 ...

  6. 异常检测算法Robust Random Cut Forest(RRCF)关键定理引理证明

    摘要:RRCF是亚马逊发表的一篇异常检测算法,是对周志华孤立森林的改进.但是相比孤立森林,具有更为扎实的理论基础.文章的理论论证相对较为晦涩,且没给出详细的证明过程.本文不对该算法进行详尽的描述,仅对 ...

  7. NtQuerySystemInformation获取进程/线程状态

    __kernel_entry NTSTATUS NtQuerySystemInformation( SYSTEM_INFORMATION_CLASS SystemInformationClass, P ...

  8. 一道codeforces题目引发的差分学习

    Codeforces Round #688 (Div. 2) 题目:B. Suffix Operations 题意:给定一个长为n的数组a,你可以进行两种操作:1).后缀+1;     2)后缀-1: ...

  9. 初窥软件工程 2020BUAA软件工程$\cdot$个人博客作业

    初窥软件工程 2020BUAA软件工程\(\cdot\)个人博客作业 目录 初窥软件工程 2020BUAA软件工程$\cdot$个人博客作业 一.作业要求简介 二.正文 (一) 快速看完整部教材,列出 ...

  10. opencv实战——图像矫正算法深入探讨

    摘要 在机器视觉中,对于图像的处理有时候因为放置的原因导致ROI区域倾斜,这个时候我们会想办法把它纠正为正确的角度视角来,方便下一步的布局分析与文字识别,这个时候通过透视变换就可以取得比较好的裁剪效果 ...