SpringCloud --- Zuul路由网关

一、Zuul概述

1、什么是服务网关

       服务网关是在微服务前边设置一道屏障,请求先到服务网关,网关会对请求进行过滤、校验、路由等处理。有了服务网关可以提高微服务的安全性,校验不通过的请求将被拒绝访问。
       前边介绍的Ribbon客户端负载均衡技术可以不用经过网关,因为通常使用Ribbon完成微服务与微服务之间的内部调用,而对那些对外提供服务的微服务,比如:用户登录、提交订单等,则必须经过网关来保证微服务的安全。

2、Zuul是什么

SpringCloud Zuul是整合Netflix公司的Zuul开源项目实现的微服务网关,它实现了请求路由、负载均衡、校验过滤等功能。
在这里插入图片描述
Zuul包含了对请求的路由和过滤两个最主要的功能:

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

       Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka,提供=代理+路由+过滤三大功能。

3、Zuul作用
  • 提供统一服务入口,微服务对前台透明
  • 聚合后台服务,节省流量,提升性能
  • 安全,过滤,流控等API管理功能
  • 提供统一服务出口,解耦
4、具体实战应用

在这里插入图片描述
(1)部署用户信息服务A、单点登录服务B,每个服务部署至少两台机器。
(2)将用户信息服务A、单点登录服务B注册到EurekaServer中。
(3)开发并部署zuul。
在这里插入图片描述(4)在zuul中配置路由
在这里插入图片描述(5)可以定义filter,需要集成zuul提供filter类,进行校验拦截。
(6)在spring boot的启动类中配置注解
在这里插入图片描述(7)根据上边的路由配置访问微服务
在这里插入图片描述
凡是以/sso/打头的请求,路由到 microservice-sso微服务
在这里插入图片描述

5、官网资料

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

二、路由基本配置

1、新建zuul-gateway-9527
(1)pom.xml
    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-zuul</artifactId>
   </dependency>

全部内容

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>springcloud-demo</artifactId>
        <groupId>com.lin</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-zuul-gateway-9527</artifactId>

    <name>springcloud-zuul-gateway-9527</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- zuul路由网关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <!-- Eureka客户端 -->
        <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>
        <!-- jetty服务器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <!-- springboot起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot测试 -->
        <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>
        <!-- 日常标配 -->
        <dependency>
            <groupId>com.lin</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>

(2)application.yml
server:
  port: 9527

spring:
  application:
    name: 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: microservice-dept
  company.name: www.nari.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

(3)修改hosts文件(c:\windows\System32\drivers\etc\hosts)

在这里插入图片描述

(4)主启动类添加注解@EnableZuulProxy
package com.lin;

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

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableZuulProxy //路由网关
public class Zuul_9527_StartSpringCloudApp {
    public static void main( String[] args )
    {
        SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
    }
}

(5)测试

启动3个eureka集群
启动服务提供者provider-8001
启动路由 zuul-gateway-9527

不用路由:
http://localhost:8001/dept/get?dpno=1
在这里插入图片描述
启动路由:
http://myzuul.com:9527/springcloud-demo/dept/get?dpno=1
解释:网关/真实微服务地址/rest风格访问细节
在这里插入图片描述

三、路由访问映射规则

访问时不暴露微服务的名称
基于工程zuul-gateway-9527进行改造演示。

1、代理名称

修改yml文件
在这里插入图片描述
修改前:
http://myzuul.com:9527/springcloud-demo/dept/get?dpno=1
在这里插入图片描述修改后:
http://myzuul.com:9527/mydept/dept/get?dpno=1
在这里插入图片描述但此时有个问题,代理名称可以访问,原来微服务名字访问也可以。需要将原真是服务名忽略

2、原真实服务名忽略
(1)修改yml

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

(2)如果要忽略多个微服务名,可批处理:

在这里插入图片描述

(3)设置统一公共前缀

在这里插入图片描述http://myzuul.com:9527/test/mydept/dept/get?dpno=1
在这里插入图片描述

(4)最终yml
server: 
  port: 9527
 
spring: 
  application:
    name: 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 
 
zuul:
  prefix: /test
  ignored-services: "*"
  routes:
    mydept.serviceId: dept-provider
    mydept.path: /mydept/**
 
info:
  app.name: microservice-dept
  company.name: www.nari.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

猜你喜欢

转载自blog.csdn.net/weixin_43240792/article/details/89404457