• (07)SpringCloud实战之zuul路由网关


      一、概述

      1、什么是zuul(官方资料

      Zuul包含了对请求的路由过滤两个最主要的功能

      (1)路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。

      (2)过滤功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。

      Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。

      二、路由基本配置 

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

      pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>microservicecloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>microservicecloud-zuul-gateway-9527</artifactId>
      <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.atguigu.springcloud</groupId>
         <artifactId>microservicecloud-api</artifactId>
         <version>${project.version}</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>
    </project>

      application.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: atguigu-microcloud
      company.name: www.atguigu.com
      build.artifactId: $project.artifactId$
      build.version: $project.version$

      hosts 修改:添加 127.0.0.1  myzuul.com

      新建启动类 Zuul_9527_StartSpringCloudApp.java

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

      2、启动 eureka、microservicecloud-provider-dept-8001、microservicecloud-zuul-gateway-9527 测试

      输入 http://localhost:7001/ 路由网关微服务已经注册到 eureka

       输入 http://localhost:8001/dept/get/2 不用路由,测试成功

      输入http://myzuul.com:9527/microservicecloud-dept/dept/get/2 使用路由,测试成功

      三、路由访问映射规则

      1、代理微服务名称

      修改microservicecloud-zuul-gateway-9527的application.yml,添加

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

      启动 eureka、microservicecloud-provider-dept-8001、microservicecloud-zuul-gateway-9527 测试

      输入:http://myzuul.com:9527/microservicecloud-dept/dept/get/2 测试成功

      输入:http://myzuul.com:9527/mydept/dept/get/1 测试成功

      2、忽略真实的微服务名称

      application.yml 做如下修改,使只能访问代理后的路径

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

      输入:http://myzuul.com:9527/microservicecloud-dept/dept/get/2 测试失败

      输入:http://myzuul.com:9527/mydept/dept/get/1 测试成功

      3、忽略所有的真实的微服务名称

      忽略单个的微服务名称可以用具体名称,如上,忽略所有微服务名称可以使用 “*”,修改 application.yml 如下:

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

      4、设置统一公共前缀

      如果想在访问微服务之前添加统一的前缀,可以修改 application.yml 如下:

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

      输入:http://myzuul.com:9527/atguigu/mydept/dept/get/1 测试成功

      

  • 相关阅读:
    jQuery源码 support
    jQuery 源码: 延迟对象补充。
    web FG interview all
    Img load
    浅谈js中this指向问题
    浅谈ES6原生Promise
    BootStrap的两种模态框方式
    让div盒子相对父盒子垂直居中的几种方法
    normalize与reset
    JS实现继承的方式
  • 原文地址:https://www.cnblogs.com/javasl/p/12524867.html
Copyright © 2020-2023  润新知