zoukankan      html  css  js  c++  java
  • 【七】zuul路由网关

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



    二、zuul路由的基本配置

    1. 新建Modeul模块microservicecloud-zuul-gateway-9527
    2. pom文件
    <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>
    
    
    
    3.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: everjiankang-microcloud
      company.name: www.everjiankang.com
      build.artifactId: $project.artifactId$
      build.version: $project.version$
    
    
    
    4.修改host文件
    127.0.0.1  myzuul.com

    5. 主启动类新添加注解:
    @EnableZuulProxy
    package com.everjiankang.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);
        }
    }

    启动:
    三个eureka集群
    一个服务提供类:microservicecloud-provider-dept-8001
    一个路由
    测试:
    1.不用路由:http://localhost:8001/dept/get/1
    2.使用路由:http://myzuul.com:9527/microservicecloud-dept/get/2


    三、路由访问映射规则

    1、工程名称:
    microservicecloud-zuul-gateway-9527
    2、代理名称:YML新增:

      before:
      http://myzuul.com:9527/microservicecloud-dept/get/2
     
    zuul:
      routes: 
        mydept.serviceId: microservicecloud-dept
        mydept.path: /mydept/**
    
    
    
      after
      http://myzuul.com:9527/mydept/get/2

    3.禁止先前的非映射的url
      此时可以通过before和after 2种方式访问,这有安全缺陷,禁掉before的url

    zuul: 
        ignored-services: microservicecloud-dept #禁止掉某个微服务的原访问地址
       #ignore-services: "*" # 禁用掉所有的微服务的原访问地址
    
    

     4、添加域名映射前缀

    zuul: 
        prefix: xiaochao

    此时:

    http://myzuul.com:9527/mydept/get/2  url访问无效
    http://myzuul.com:9527/xiaochao/mydept/get/2 ok

    终极结果:

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









  • 相关阅读:
    通信信号处理的一些基本常识
    欧拉公式
    css3圆角讲解
    css3投影讲解、投影
    css3变形讲解
    浏览器兼容问题
    css3渐变详解
    css中em与px
    복 경 에 갑 니 다 去北京
    我在北京:)
  • 原文地址:https://www.cnblogs.com/guchunchao/p/10451912.html
Copyright © 2011-2022 走看看