八、Spring Cloud Zuul

1.Zuul簡介

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

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

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

  Zuul和Eureka進行整合,將Zuul自身註冊爲Eureka服務治理下的應用,同時從Eureka中得到其餘微服務的消息,也即之後的訪問微服務都是經過Zuul跳轉後得到。spring

  注:Zuul服務最終仍是會註冊進Eurekaapache

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

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

2.Zuul配置

 

(1).建立工程

  新建Module模塊microservicecloud-zuul-gateway-9527maven

 

 

(2).配置pom

[1].修改部分

<!-- zuul路由網關 -->ide

<dependency>spring-boot

    <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].啓動eureka700一、eureka700二、eureka7003

  啓動microservicecloud-eureka-700一、microservicecloud-eureka-700二、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

相關文章
相關標籤/搜索