Gateway簡(jiǎn)介
SpringCloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目憾赁,該項(xiàng)目是基于 Spring 5.0构挤,Spring Boot 2.0 和 Project Reactor 等技術(shù)開發(fā)的網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的統(tǒng)一的 API 路由管理方式凯砍。
SpringCloud Gateway 作為 Spring Cloud 生態(tài)系統(tǒng)中的網(wǎng)關(guān)箱硕,目標(biāo)是替代 Zuul,在Spring Cloud 2.0以上版本中悟衩,沒(méi)有對(duì)新版本的Zuul 2.0以上最新高性能版本進(jìn)行集成剧罩,仍然還是使用的Zuul 2.0之前的非Reactor模式的老版本。而為了提升網(wǎng)關(guān)的性能座泳,SpringCloud Gateway是基于WebFlux框架實(shí)現(xiàn)的惠昔,而WebFlux框架底層則使用了高性能的Reactor模式通信框架Netty幕与。
Spring Cloud Gateway 的目標(biāo),不僅提供統(tǒng)一的路由方式舰罚,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能纽门,例如:安全,監(jiān)控/指標(biāo)营罢,和限流赏陵。
其主要特征分為以下五點(diǎn):
(1)基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
(2)集成 Hystrix 斷路器
(3)集成 Spring Cloud DiscoveryClient
(4)Predicates 和 Filters 作用于特定路由饲漾,易于編寫的 Predicates 和 Filters
(5)具備一些網(wǎng)關(guān)的高級(jí)功能:動(dòng)態(tài)路由蝙搔、限流、路徑重寫
引入依賴
在 sca-gateway 模塊的pom中引入Gateway的maven依賴
!-- SpringCloud Gateway 網(wǎng)關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
resources目錄下新增bootstrap.yml文件
server:
port: 88
servlet:
context-path: /gateway
spring:
cloud:
nacos:
##服務(wù)的注冊(cè)
discovery:
server-addr: localhost:8848
##服務(wù)配置中心
config:
server-addr: localhost:8848
file-extension: yaml
gateway:
discovery:
locator:
# 是否與服務(wù)發(fā)現(xiàn)組件進(jìn)行結(jié)合考传,通過(guò)serviceId轉(zhuǎn)發(fā)到具體的服務(wù)實(shí)例吃型。默認(rèn)false,
# 為true代表開啟基于服務(wù)發(fā)現(xiàn)的路由規(guī)則僚楞。
enabled: true
# 配置之后訪問(wèn)時(shí)無(wú)需大寫
lower-case-service-id: true
routes:
- id: normal-rest
uri: lb://normal-rest
predicates:
# 路徑匹配勤晚,以 api 開頭,直接配置是不生效的泉褐,看 filters 配置
- Path=/normal/**
filters:
# 前綴過(guò)濾赐写,默認(rèn)配置下,我們的請(qǐng)求路徑是 http://localhost:9000/myshop-service-consumer-item/** 這時(shí)會(huì)路由到指定的服務(wù)
# 此處配置去掉 1 個(gè)路徑前綴膜赃,再配置上面的 Path=/api/**挺邀,就能按照 http://localhost:9000/api/** 的方式訪問(wèn)了
- StripPrefix=1
application:
name: sca-gateway
profiles:
active: dev
創(chuàng)建啟動(dòng)類 GatewayApp.java
package com.ldh.sca;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan("com.ldh.sca.gateway")
public class GatewayApp {
public static void main(String[] args) {
SpringApplication.run(GatewayApp.class, args);
}
@Bean
LoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalance) {
return new LoadBalancerInterceptor(loadBalance);
}
}
創(chuàng)建 GatewayService.java
package com.ldh.sca.gateway.service;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
@Component
public class GatewayService implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
}
啟動(dòng)sca-gateway項(xiàng)目,此時(shí)Nacos管理頁(yè)面的服務(wù)列表會(huì)注冊(cè)sca-gateway服務(wù)
瀏覽器訪問(wèn) http://localhost:88/normal-rest/order/getName 跳座,如果能正常返回ldh.name的值端铛,則Gateway路由配置成功