SpringCloud_05_Zuul入门实例

前言

Zuul 是从设备和网站到后端应用程序所有请求的前门,为内部服务提供可配置的对外URL到服务的映射关系。

简单来说,zuul就是一个微服务网关,是其他各个微服务的入口,其具备以下功能:

  • 认证与鉴权
  • 压力控制
  • 金丝雀测试
  • 动态路由
  • 负载削减
  • 静态响应处理
  • 主动流量管理

其底层基于Servlet,本质组件是一系列Filter所构成的责任链。

一、创建服务消费者

1.创建子模块

这里我们创建一个子模块,创建步骤同SpringCloud_01_Discovery_01_Eureka入门示例

子模块信息如下:

group = 'com.ray.study'
artifact ='spring-cloud-05-gateway-zuul'

2.引入依赖

2.1 继承父工程依赖

在父工程spring-cloud-seedssettings.gradle加入子工程

rootProject.name = 'spring-cloud-seeds'
include 'spring-cloud-01-discovery-01-eureka-server'
include 'spring-cloud-01-discovery-01-eureka-client'
include 'spring-cloud-01-discovery-02-consul-client'
include 'spring-cloud-02-consumer-ribbon'
include 'spring-cloud-03-consumer-feign'
include 'spring-cloud-04-consumer-hystrix-feign'
include 'spring-cloud-05-gateway-zuul'

这样,子工程spring-cloud-05-gateway-zuul就会自动继承父工程中subprojects 函数里声明的项目信息

2.2 引入 zuul 依赖

将子模块spring-cloud-05-gateway-zuulbuild.gradle修改为如下内容:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    // eureka client
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

    // zuul
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'

}

3. 修改配置

3.1 修改application.yml

开启 hystrix

server:
  port: 8767

spring:
  application:
    name: gateway-zuul   #指定服务名

eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:   #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔
      defaultZone: http://localhost:8761/eureka/    # 指定 Eureka Server 地址

zuul:
  routes:
    eureka-client:
      path: /client/**
      serviceId: eureka-client


上面配置的路由为:将所欲 /client/开头的URL映射到 client-a这个服务中去,这样就实现了网关的作用,所有的请求,先经过网关,再由网关路由到具体的服务上。

3.2 启用ZuulProxy

在启动类上

  • 添加@EnableDiscoveryClient注解可启用服务发现
  • 添加@EnableZuulProxy注解可启用 zuul 网关
package com.ray.study.springcloud05gatewayzuul;

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


@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class SpringCloud05GatewayZuulApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloud05GatewayZuulApplication.class, args);
	}

}

4.测试

下面,我们将使用 SpringCloud_01_Discovery_01_Eureka入门示例 这一节中创建的 Eureka 注册中心和Eureka Client 服务提供者来进行演示

依次启动

  • eurka-server: 服务注册中心
  • eureka-client:服务提供者
  • gateway-zuul:本节创建的网关

下面通过网关 gateway-zuul 来访问eureka-client 服务提供者的的 HelloController

http://localhost:8767/client/hello

在这里插入图片描述

如上图,显示提供服务的是 8762 端口的 eureka-client

参考资料

  1. 《重新定义Spring Cloud实战》(F版)
  2. 唐亚峰__一起来学SpringCloud之 - 路由网关(Zuul)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值