Spring Cloud(Finchley版本)系列教程(五) 服务网关(Zuul)

本文是Spring Cloud Finchley系列教程第五篇,主要介绍Zuul服务网关的使用。Zuul是Netflix的边缘服务,提供路由、监控、安全等功能。内容包括Zuul简介、创建Zuul工程、启用和配置Zuul,以及通过实例验证Zuul的路由转发功能。
摘要由CSDN通过智能技术生成

Spring Cloud(Finchley版本)系列教程(五) 服务网关(Zuul)

为了更好的浏览体验,欢迎光顾勤奋的凯尔森同学个人博客http://www.huerpu.cc:7000

一、Zuul简介

Zuul 是 Netflix OSS 中的一员,是一个基于 JVM 路由器服务端的负载均衡器。提供路由、监控、弹性、安全等方面的服务框架。Zuul 能够与 Eureka、Ribbon、Hystrix 等组件配合使用。

zuul核心功能是过滤器、路由、异常处理,通过过滤器还能扩展出其他功能:

1)动态路由、2)请求监控、3)认证鉴权、4)压力测试、5)灰度发布

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

二、创建Zuul工程

复制eurekaClient工程,修改名称为hepServiceZuul。

<artifactId>hepServiceZuul</artifactId>
<name>hepServiceZuul</name>
<description>hepServiceZuul</description>

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

三、启用并配置Zuul

启动类HepServiceZuulApplication加上注解@EnableZuulProxy,开启zuul的功能

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class HepServiceZuulApplication {

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

}

在application.yml上增加/hep-a/**/hep-b/**

zuul:
  routes:
    hep-a:
      path: /hep-a/**
      serviceId: hepServiceRibbonHystrix
    hep-b:
      path: /hep-b/**
      serviceId: hepServiceOpenFeignHystrix

application.yml文件内容如下

server:
  port: 8011 # 端口号

spring:
  application:
    name: hepServiceZuul # Eureka名称


zuul:
  routes:
    hep-a:
      path: /hep-a/**
      serviceId: hepServiceRibbonHystrix
    hep-b:
      path: /hep-b/**
      serviceId: hepServiceOpenFeignHystrix


eureka:
  instance:
    prefer-ip-address: false
    hostname: hepServiceZuul
  client:
    healthcheck:
      enabled: true
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka:eurekapwd@www.huerpu.cc:1678/eureka/

指定服务注册中心的地址为http://eureka:eurekapwd@www.huerpu.cc:1678/eureka/,服务的端口为8011,服务名为hepServiceZuul;以/hep-a/ 开头的请求都转发给hepServiceRibbonHystrix服务;以/hep-b/开头的请求都转发给hepServiceOpenFeignHystrix服务;

win11的host文件增加了下面三个

127.0.0.1   hepServiceZuul
127.0.0.1   hepServiceRibbonHystrix
127.0.0.1   hepServiceOpenFeignHystrix

四、验证Zuul

打开浏览器访问http://localhost:8011/hep-a/getUserById

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

访问http://localhost:8011/hep-b/getUserById

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

这说明zuul起到了路由的作用。

参考文章https://forezp.blog.csdn.net/article/details/81041012

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的凯尔森同学

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值