SpringCloud(Finchley版)4 - Zuul

一, 簡介

    Zuul 路由轉發和過濾器, zuul默認和Ribbon結合實現了負載均衡的功能. java

二, 新建 Zuul 服務 cloud-e

    1,  cloud-e pom.xmlgit

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.gy.cloud</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>cloud-e</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <description>Demo project for Service Zuul</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.cloud</groupId>-->
            <!--<artifactId>spring-cloud-starter-gateway</artifactId>-->
        <!--</dependency>-->
    </dependencies>

</project>

    2,  cloud-e application.ymlspring

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

server:
  port: 8765

spring:
  application:
    name: service-e

#  cloud:
#    gateway:
#      discovery:
#        locator:
#          enabled: true
#      routes:
#      - id: api-b
#        uri: http://localhost:8762/
#        predicates:
#        - Path=/api-b/**
#        filters:
#        - StripPrefix=1
#      - id: api-c
#        uri: lb://service-c
#        predicates:
#        - Path=/api-c/**
#        filters:
#        - StripPrefix=1

zuul:
  routes:
    api-b:
      path: /api-b/**
      serviceId: service-b
    api-c:
      path: /api-c/**
      serviceId: service-c

    3, cloud-e CloudEApplicationapache

package com.gy.cloud.cloude;

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

@EnableZuulProxy
@SpringBootApplication
public class CloudEApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudEApplication.class, args);
        System.out.println("=== 路由網關服務E啓動成功 ===");
    }

}

    4, 啓動服務 cloud-e , 訪問 :  http://localhost:8765/api-b/hi   ,    http://localhost:8765/api-c/hiapi

三, 總結

    Zuul 主要負責服務接口轉發, 這樣整個服務只須要請求 Zuul 服務接口便可;app

    spring boot 的版本必定是 2.0.3.RELEASE , 最新版的 spring boot 集成 Zuul 啓動就報錯;負載均衡

學習文檔

方誌朋的博客 :  https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f5-zuul/maven

項目源碼:  https://gitee.com/ge.yang/spring-demo/tree/master/cloud學習

相關文章
相關標籤/搜索