8、Spring Cloud Zuul

HOsystem發表於2020-12-12

1.Zuul簡介

  Zuul包含了對請求的路由過濾兩個最主要的功能。

  路由功能負責將外部請求轉發到具體的微服務例項上,是實現外部訪問統一入口的基礎。

  過濾器功能則負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎。

  Zuul和Eureka進行整合,將Zuul自身註冊為Eureka服務治理下的應用,同時從Eureka中獲得其他微服務的訊息,也即以後的訪問微服務都是通過Zuul跳轉後獲得。

  注:Zuul服務最終還是會註冊進Eureka

  Zuul提供代理、路由、過濾三大功能。

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

2.Zuul配置

 

(1).建立工程

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

 

 

(2).配置pom

[1].修改部分

<!-- 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>

[2].完整部分

<?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>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

    <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).applicaiton.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: hosystem-microcloud

  company.name: www.hosystem.com

  build.artifactId: $project.artifactId$

  build.version: $project.version$

(4).修改hosts

127.0.0.1  myzuul.com

 

(5).主啟動類

  建立主啟動類Zuul_9527_StartSpringCloudApp,並新增註解@EnableZuulProxy.

package com.hosystem.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);

    }

}

(6).啟動專案

[1].啟動eureka7001、eureka7002、eureka7003

  啟動microservicecloud-eureka-7001、microservicecloud-eureka-7002、microservicecloud-eureka-7003

 

[2].啟動provider8001

  啟動microservicecloud-provider-dept-8001

 

[3].啟動zuul9527

  啟動microservicecloud-zuul-gateway-9527

 

(7).測試

[1].未啟用路由

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

 

[2].啟動路由

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

 

3.Zuul路由訪問對映規則

 

(1).修改application.yml

  修改部分:

zuul:

  routes:

    mydept.serviceId: microservicecloud-dept

    mydept.path: /mydept/**

  完整部分:

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

 

zuul:

  routes:

    mydept.serviceId: microservicecloud-dept

    mydept.path: /mydept/**

 

info:

  app.name: hosystem-microcloud

  company.name: www.hosystem.com

  build.artifactId: $project.artifactId$

  build.version: $project.version$

  訪問效果

#之前訪問的時候通過'microservicecloud-dept‘

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

 

#之後訪問的時候通過'mydept'

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

  出現的問題,通過'microservicecloud-dept‘或者'mydept'都可以訪問。如何限制只允許'mydept'訪問,而'microservicecloud-dept'訪問失敗呢?具體解決方法如下,

#解決單個的時候指定具體的servideId

#如果想要解決多個的時候可以使用   "*"

zuul:

  ignored-services: microservicecloud-dept

  routes:

    mydept.serviceId: microservicecloud-dept

    mydept.path: /mydept/**

 

  統一公共字首

zuul:

  prefix: /hosystem

  ignored-services: "*"

  routes:

    mydept.serviceId: microservicecloud-dept

    mydept.path: /mydept/**

 

  完整的applicaiton.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

 

zuul:

  prefix: /hosystem

  ignored-services: "*"

  routes:

    mydept.serviceId: microservicecloud-dept

    mydept.path: /mydept/**

 

info:

  app.name: hosystem-microcloud

  company.name: www.hosystem.com

  build.artifactId: $project.artifactId$

  build.version: $project.version$

參考文件:

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

相關文章