天天看點

從0到1學SpringCloud第八篇:API網關

文章内容輸出來源:拉勾教育Java高薪訓練營

1. 前言

1.1 說明

現在我們是有三個服務:郵箱、驗證碼、使用者。每個服務都有自己的請求端口。本文我們将引入GateWay網關,做為整個項目的通路入口,然後将流量轉發至對應的服務。

1.2 什麼是GateWay

GateWay是Spring Cloud的⼀個全新項⽬,⽬标是取代Netflix Zuul,它基于Spring5.0+SpringBoot2.0+WebFlux等技術開發。GateWay天⽣就是異步⾮阻塞的,基于Reactor模型。

當使用者發送一個請求,首先會經過網關,網關根據路由的規則進行比對,然後将請求轉發到指定的服務位址。在這一個過程中,我們還可以做一些像限流、日志、黑白名單等功能。

它主要包括了三大部分:

  • 路由(route): ⽹關最基礎的部分,也是⽹關⽐較基礎的⼯作單元

    路由由⼀個ID、⼀個⽬标URL(最終路由到的位址)、⼀系列的斷⾔(比對條件判斷)和Filter過濾器(精細化控制)組成

  • 斷⾔(predicates):通過斷言可以比對Http請求中的所有内容(包括請求頭、請求參數等),如果斷⾔與請求相比對則路由
  • 過濾器(filter):⼀個标準的Spring webFilter,使⽤過濾器,可以在請求之前或者之後執⾏業務邏輯

2. 建立網關項目

2.1 建立網關項目

  • 建立一個maven的子產品

    lagou-cloud-gateway-9002

2.2 在pom.xml中引入依賴

  • 引入網關的依賴以及webflux。因為要把網關項目也在Nacos注冊中心中注冊,是以也引入Nacos的相關依賴
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<!--引入webflux-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
           

2.3 建立配置檔案application.yml

  • 配置端口為9002
  • 配置應用名稱為

    lagou-cloud-gateway

  • 配置Nacos注冊中心
  • 配置網關的路由規則,包括了驗證碼、使用者、郵箱三部分的規則。三者都是後端API,是以在Path通路位址上多加了一個 /api 的字首。然後就要把StripPrefix設定為1
server:
  port: 9002
spring:
  application:
    name: lagou-cloud-gateway
  cloud:
    # nacos配置
    nacos:
      discovery:
        # Nacos 注冊中心配置位址(無需配置 HTTP 協定部分),多個用,隔開
        server-addr: 127.0.0.1:8848
    gateway:
      routes:
        - id: service-code-router # 我們自定義的路由 ID,保持唯一
          uri: lb://lagou-service-code
          # 斷言:路由條件,Predicate 接受一個輸入參數,傳回一個布爾值結果。該接口包含多種默 認方法來将 Predicate 組合成其他複雜的邏輯(比如:與,或,非)。
          predicates:
            - Path=/api/codes/**
          filters:
            - StripPrefix=1
        - id: service-user-router
          uri: lb://lagou-service-user
          predicates:
            - Path=/api/users/**
          filters:
            - StripPrefix=1
        - id: service-email-router
          uri: lb://lagou-service-email
          predicates:
            - Path=/api/emails/**
          filters:
            - StripPrefix=1

           

2.4 建立啟動類

GateWayApplication9002

@SpringBootApplication
@EnableDiscoveryClient // 開啟注冊中心用戶端 (通用型注解,比如注冊到Eureka、Nacos等)
public class GateWayApplication9002 {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication9002.class, args);
    }

}
           

2.5 啟動服務,則可以在nacos注冊中心看到這個服務

從0到1學SpringCloud第八篇:API網關

3. 修改nginx的配置

3.1 修改nginx的配置檔案

  • 将原先的三個服務的反向代理配置修改為網關服務的反向代理配置
  • 去掉原先三個服務的比對規則,增加 /api 的比對規則
http {
    include       mime.types;
    default_type  application/octet-stream;
    
    upstream cloud-server {
        server 127.0.0.1:9002;
    }

    server {
        listen       80;
        server_name  www.test.com;
        access_log  /logs/nginx/cloud.access.log;
        location ^~ /api {
            proxy_pass http://cloud-server; 
        }

        location ^~ /static {
            root /上層檔案夾的絕對路徑/v2.0/user-cloud/lagou-static-web;
        }
    }
}
           

3.2 重新開機nginx

4. 修改靜态頁面的ajax請求

  • 修改登入、注冊、歡迎頁面中的ajax請求,在url請求位址增加上 /api 的字首

5. 參考代碼

  • user-cloud

繼續閱讀