• I第六章:(1)Zuul路由网关


    一、Zuul 概述

    1、Zuul 是什么

    Zuul包含了对请求的路由和过滤两个最主要的功能: 其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。

    注意:Zuul服务最终还是会注册进Eureka

    Zuul提供了代理+路由+过滤三大功能,主要来做路由和过滤。

    2、官网资料

    https://github.com/Netflix/zuul/wiki/Getting-Started

    二、路由基本配置

    1、新建网关模块

    新建Module模块microservicecloud-zuul-gateway-9527

    2、添加POM依赖坐标

        <dependencies>
            <!-- zuul路由网关 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zuul</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!-- actuator监控 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!--  hystrix容错-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <!-- 日常标配 -->
            <dependency>
                <groupId>com.njf.springcloud</groupId>
                <artifactId>microservicecloud-api</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <!-- 热部署插件 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
        </dependencies>

    3、设置YML配置信息

    server:
      port: 9527

    spring:
      application:
        name: microservicecloud-zuul-gateway

    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
      instance:
        instance-id: gateway-9527.com
        prefer-ip-address: true


    info:
      app.name: njf-microcloud
      company.name: www.njf.com
      build.artifactId: $project.artifactId$
      build.version: $project.version$

    4、修改hosts文件

    在 C:\Windows\System32\drivers\etc 目录中找到 hosts添加域名映射关系 127.0.0.1 myzuul.com

    5、修改主启动类

    @SpringBootApplication
    @EnableZuulProxy
    public class Zuul_9527_StartSpringCloudApp
    {
        public static void main(String[] args)
        
    {
            SpringApplication.run(Zuul_9527_StartSpringCloudApp.classargs);
        }
    }

    6、启动

    (1)启动三个Eureka集群

    (2)一个服务提供类microservicecloud-provider-dept-8001

    (3)一个路由

    7、测试

    • 不启用路由

    http://localhost:8001/dept/get/2

    • 启用路由

    http://myzuul.com:9527/microservicecloud-dept/dept/get/2

    三、路由访问映射规则

    1、代理名称

    此时是通过域名+服务名称进行访问的,如果我们要屏蔽掉服务名(形成一定的保护),可以设置代理名称:

    在 YML 配置文件中设置:

    zuul: 
      routes: 
        mydept.serviceId: microservicecloud-dept
        mydept.path: /mydept/**

    路径访问OK

    http://myzuul.com:9527/mydept/dept/get/1

    原路径访问OK

    http://myzuul.com:9527/microservicecloud-dept/dept/get/2

    此时还有一个问题,还是可以通过服务名称进行调用。

    2、原真实服务名忽略

    可以在 YML 配置文件中进行设置,忽略真实服务名

    zuul:
      ignored-services: microservicecloud-dept
      routes:
        mydept.serviceId: microservicecloud-dept
        mydept.path: /mydept/**

    单个服务可以指定具体名称,多个服务可以用"*"

    zuul:
      ignored-services: "*"
      routes:
        mydept.serviceId: microservicecloud-dept
        mydept.path: /mydept/**

    3、设置统一公共前缀

    为了方便统一管理,还可以在配置文件中设置公共前缀

    zuul:
      prefix: /njf
      ignored-services: "*"
      routes:
        mydept.serviceId: microservicecloud-dept
        mydept.path: /mydept/**

    此时访问路径:

    http://myzuul.com:9527/njf/mydept/dept/get/1

  • 相关阅读:
    《走近心理学》第一章之行为和心理学
    《解忧杂货铺》读书笔记
    追求得到之日即其终止之时, 寻觅的过程亦即失去的过程。——村上
    简朴的生活、高贵的灵魂是人生的至高境界。——杨绛
    Laravel Seeder
    Git的使用 checkout push merge
    基于 GraphQL 构建 Laravel API —— 基本使用篇
    awk基础04-内置函数
    awk基础03-分支和循环语句
    awk基础02-变量-分隔符-数组
  • 原文地址:https://www.cnblogs.com/niujifei/p/16003271.html
Copyright © 2020-2023  润新知