Spring Cloud Zuul 路由是微服务架构的不可或缺的一部分,提供动态路由、监控、弹性、安全等的边缘服务。Zuul 是 Netflix 出品的一个基于 JVM 路由和服务端的负载均衡器。

准备工作

我们将用到之前实现的几个应用,包括:

  • eureka-server:服务注册中心
  • service-producer:服务提供者
  • service-consumer-ribbon:使用 Ribbon实现的服务消费者

首先创建一个基础的 Spring Boot 项目,命名为:service-api-gateway

POM 依赖

在pom.xml引入以下依赖

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

配置文件

在配置文件 application.yml 中加入服务名、端口号、Eureka 注册中心的地址

server:
port: 9100
spring:
application:
name: service-api-gateway
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:8761/eureka/

启动类

使用@EnableZuulProxy注解开启 Zuul 的功能

 package com.carry.springcloud;

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

测试

依次启动

  • eureka-server
  • service-producer
  • service-consumer-ribbon
  • service-api-gateway

由于 Spring Cloud Zuul 在整合了 Eureka 之后,具备默认的服务路由功能,即:当我们这里构建的service-api-gateway应用启动并注册到 Eureka 之后,服务网关会发现上面我们启动的两个服务service-producer和service-consumer-ribbon,这时候 Zuul 就会创建两个路由规则。每个路由规则都包含两部分,一部分是外部请求的匹配规则,另一部分是路由的服务 ID。针对当前示例的情况,Zuul 会创建下面的两个路由规则:

  • 转发到service-producer服务的请求规则为:/service-producer/**
  • 转发到service-consumer-ribbon服务的请求规则为:/service-consumer-ribbon/**

最后,我们可以通过访问9100端口的服务网关来验证上述路由的正确性:

浏览器中访问http://localhost:9100/service-consumer-ribbon/getPoducerInfo

Spring Cloud学习笔记【七】服务网关 Zuul(路由)的更多相关文章

  1. Spring Cloud(六)服务网关 zuul 快速入门

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

  2. Spring Cloud 系列之 Gateway 服务网关(四)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) Spring Cl ...

  3. Spring Cloud 系列之 Gateway 服务网关(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Gateway 服务网关(一) 本篇文章讲解 Gateway 网关的多种路由规则.动态路由规则(配合服务发现的路由规则 ...

  4. Spring Cloud 系列之 Gateway 服务网关(三)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) 本篇文章讲解 Ga ...

  5. Spring Cloud 系列之 Gateway 服务网关(一)

    什么是 Spring Cloud Gateway Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由 ...

  6. Spring Cloud学习笔记-009

    API网关服务:Spring Cloud Zuul API网关是一个更为智能的应用服务器,它的定义类似于面向对象设计模式中的Façade模式,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户 ...

  7. Spring Cloud学习笔记-005

    服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...

  8. Spring Cloud 学习笔记(一)——入门、特征、配置

    [TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...

  9. Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)

    在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...

随机推荐

  1. 前端的console.log的效果写法

    不说废话,直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  2. Vim常用命令及配置方案

    Vim常用命令及配置方案   几句话 很久之前就接触到vim,初学那阵觉得vim很酷炫,但确实对新手不是很友好.我也就简单看了下基本操作就上手了,但又不是长期在vim下工作,这就导致了每一次重新使用v ...

  3. [Python随笔]>>range()函数?

    因为自己在考核的时候没有记清range()函数的具体用法,所以特意去查了下 Python range() 函数用法 python range() 函数可创建一个整数列表,一般用在 for 循环中 函数 ...

  4. angular7升级到angular8

    1.首先我们对:angular的命令的安装 ng install -g @angular/cli的安装则会升级到最新的版本,并且再次创建项目的时候,我们就能够使用ng version查看到已经是最新的 ...

  5. redis搭建与安装2

    第一步redis安装:1.首先确认下载包为64位的还是32位的2.下载http://code.google.com/p/servicestack/downloads3.解压下载包得到以下文件:cygw ...

  6. caioj 1157 线性筛选素数

    注意这道题开得非常大,有2*1e7 自己可以养成一种习惯,如果数据是很容易的话,可以自己手动输入极限数据来测试自己的程序 #include<cstdio> #include<algo ...

  7. Opencv 三对角线矩阵(Tridiagonal Matrix)解法之(Thomas Algorithm)

    1. 简介 三对角线矩阵(Tridiagonal Matrix),结构如公式(1)所示: aixi−1+bixi+cixx+1=di(1) 其中a1=0,cn=0.写成矩阵形式如(2): ⎡⎣⎢⎢⎢⎢ ...

  8. MAC中Parallels Desktop windows忘记密码的解决办法

    由于工作或是生活,在国内的环境下我们总有些时候要用到Windows才能完成某些任务,对于不经常使用Windows的用户,相信在虚拟机上安装一个Windows是不错的选择.小编就使用了Paralles ...

  9. Android使用ShowcaseView加入半透明操作提示图片的方法

    http://beeder.me/2014/11/11/how-to-add-a-semi-transparent-demo-screen-using-showcaseview/ 这篇文章具体介绍了如 ...

  10. jmeter名词解释之时间(Elapsed Time/ Latency Time/Connection Time)

    转载时请标注源自:http://blog.csdn.net/musen518 jmeter报告结果中会出现三个时间 1. Elapsed time    经过的时间(= Sample time = L ...