天天看點

SpringCloud全家桶 (第四期:Zuul網關)

文章目錄

    • 一:什麼是SpringCloud
    • 二:zuul是什麼
    • 三:搭建zuul
    • 五:通路

一:什麼是SpringCloud

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.

翻譯如下:

Spring Cloud為開發人員提供了快速建構分布式系統中的一些常見模式的工具(例如配置管理、服務發現、斷路器、智能路由、微代理、控制總線、一次性令牌、全局鎖、上司層選舉、分布式會話、叢集狀态)。分布式系統的協調導緻了鍋爐闆模式,使用Spring Cloud開發人員可以快速建立實作這些模式的服務和應用程式。它們在任何分布式環境中都能很好地工作,包括開發人員自己的筆記本電腦、裸機資料中心和雲計算等托管平台。

SpringCloud主要架構:

  • 服務發現——Netflix Eureka
  • 服務調用——Netflix Feign
  • 熔斷器——Netflix Hystrix
  • 服務網關——Netflix Zuul
  • 分布式配置——Spring Cloud Config
  • 消息總線 —— Spring Cloud Bus

二:zuul是什麼

zuul 是netflix開源的一個API Gateway 伺服器, 本質上是一個web servlet應用。

Zuul 在雲平台上提供動态路由,監控,彈性,安全等邊緣服務的架構。Zuul 相當于是裝置和 Netflix 流應用的 Web 網站後端所有請求的前門。

SpringCloud全家桶 (第四期:Zuul網關)

zuul的主要功能:

  • 路由
  • 過濾

三:搭建zuul

1,建立

ml_gateway

子子產品

SpringCloud全家桶 (第四期:Zuul網關)
SpringCloud全家桶 (第四期:Zuul網關)
SpringCloud全家桶 (第四期:Zuul網關)

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tt</groupId>
    <artifactId>ml_gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ml_gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

3,建立一個

application.yml

檔案,加入以下配置

server:
  port: 7979

spring:
  application:
    name: ml-gateway
  profiles:
    active: dev


zuul:
  routes:
    api-a:                            #自定義服務(随便寫)
      path: /gateway/ss/**            #自定義路徑(将要攔截的),切記不能以關鍵字開頭如:/zuul/**
      serviceid: ml-student           #将要通路的微服務名
    api-b:
      path: /gateway/tt/**
      serviceid: ml-teacher


  host:
    connect-timeout-millis: 15000 #HTTP連接配接逾時要比Hystrix的大
    socket-timeout-millis: 60000   #socket逾時
ribbon: #設定ribbon的逾時時間小于zuul的逾時時間
  ReadTimeout: 10000
  ConnectTimeout: 10000

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
           

4,在啟動類

MlGatewayApplication

中加入注解

@EnableZuulProxy

,表示開啟網關

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

@SpringBootApplication
@EnableZuulProxy//開啟網關
@EnableEurekaClient
public class MlGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(MlGatewayApplication.class, args);
    }

}
           

到這一步,就搭建zull完成了,接下來啟動測試下

5,服務提供者(測試)

ml-student

微服務中,建立

controller

如下:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StringController {


    @RequestMapping("provider")
    public String getString(){
        return "我是服務提供者!";
    }
}
           

五:通路

SpringCloud全家桶 (第四期:Zuul網關)

開心一刻

老師在講掩耳盜鈴,講完後讓小明講一下他的讀後感。

小明:“有些東西,并不是你不知道或者假裝不知道就會變得不存在,比如下課鈴!”

老師:“滾出去!”

SpringCloud全家桶 (第四期:Zuul網關)

如果覺得不錯,幫忙點個贊,您的點贊将是我的動力!

上一篇:SpringCloud Hystrix之Hystrix Dashboard儀表盤

下一篇:SpringCloud Zuul網關之ZuulFilter過濾器

繼續閱讀