• SpringCloud与微服务Ⅸ --- Zuul路由网关


    一.Zool是什么

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

    其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。

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

    Zuul为我们提供了代理、路由、过滤等三大功能。

    https://github.com/Netflix/zuul/wiki

     

    二.路由的基本配置

    新建模块microservice-zuul-gateway-9527项目。

    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.wang.springcloud</groupId>
            <artifactId>microservice</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>microservice-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.wang.springcloud</groupId>
                <artifactId>microservice-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>

    yml配置文件

    server:
      port: 9527
    spring:
      application:
        name: microservice-zuul-gateway
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
      instance:
        instance-id: gateway-9527.com
        prefer-ip-address: true
    info:
      app.name: microservice
      company.name: www.gateway.com
      build.artifactId: microservice-zuul-gateway-9527
      build.version: 1.0-SNAPSHOT

    修改host文件

    修改域名映射,找到系统的host文件(C:WindowsSystem32Driversetc ),新增如下映射关系:

    127.0.0.1 myzuul.com

    启动类注解:

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

    启动测试:

    启动3个集群,一个服务提供者,一个路由项目。

    使用路由访问:http://myzuul.com:9527/microservice-dept/dept/get/2

     

    三.路由访问的映射规则

    修改路由的访问路径,隐藏真实的路由,对外暴露一个虚拟的路由,防止泄露微服务的名称等信息。

    修改microservice-zuul-gateway-9527项目。

    yml修改

    zuul:
      ignored-services: microservice-dept #隐藏该微服务名称
      routes:
        mydept.serviceId: microservice-dept
        mydept.path: /mydept/**

    修改完毕后访问:http://myzuul.com:9527/mydept/dept/get/2

    若要禁止掉所有真实微服务名称:

    zuul:
      ignored-services: "*"    #隐藏所有微服务名称

    设置统一公共前缀

    zuul:
      prefix: /wang

    配置完成后重启编译然后访问:http://myzuul.com:9527/wang/mydept/dept/get/2

    完整的yml配置

    server:
      port: 9527
    spring:
      application:
        name: microservice-zuul-gateway
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
      instance:
        instance-id: gateway-9527.com
        prefer-ip-address: true
    
    
    zuul:
      prefix: /wang
      ignored-services: "*" #忽略真实服务名
      routes:
        mydept.serviceId: microservice-dept
        mydept.path: /mydept/**
    
    
    info:
      app.name: microservice
      company.name: www.gateway.com
      build.artifactId: microservice-zuul-gateway-9527
      build.version: 1.0-SNAPSHOT
  • 相关阅读:
    JVM
    Java并发II
    Nlpir大数据知识图谱的落地指南
    灵玖软件:NLPIR大数据提供智能挖掘技术方案
    灵玖软件:NLPIR大数据中文挖掘平台为各个行业赋能
    NLPIR大数据从分词到知识图谱展现智能实现
    纠文网为毕业论文智能修改内容格式
    纠文网论文智能核查融合人工智能和规则技术
    纠文网智能论文修改融合人工智能和规则知识技术
    纠文网智能语义自动解决毕业论文内容格式难题
  • 原文地址:https://www.cnblogs.com/wangxiayun/p/10427979.html
Copyright © 2020-2023  润新知