本文主要講解實現Spring Cloud Gateway
路由轉發功能java
Spring Cloud Gateway是Spring官方基於Spring 5.0,Spring Boot 2.0和Project Reactor等技術開發的網關,Spring Cloud Gateway
旨在爲微服務架構提供一種簡單而有效的統一的API路由管理方式。Spring Cloud Gateway做爲Spring Cloud生態系中的網關,目標是替代Netflix
ZUUL,其不只提供統一的路由方式,而且基於Filter鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等
複製代碼
一、新建springBoot
項目
依賴包引入:服務註冊包nacos、服務調用包Feign、Gateway包spring
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.miniwan</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
複製代碼
二、將網關服務註冊到nacos
a、編寫application.yml文件,配置服務註冊中心地址apache
server:
port: 9001
spring:
application:
name: service-agateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
複製代碼
b、主程序客戶端標記@EnableDiscoveryClient
安全
@EnableDiscoveryClient
@SpringBootApplication
public class AgatewayApplication {
public static void main(String[] args) {
SpringApplication.run(AgatewayApplication.class, args);
}
}
複製代碼
三、實現路由轉發
a、application.yml開啓網關功能並配置路由轉發bash
server:
port: 9001
spring:
application:
name: service-agateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
#將此服務設置爲網關
gateway:
discovery:
locator:
enabled: true
routes:
#路由名稱
- id: consumer_route
#跳轉路由
uri: http://www.scnunanshen.online/
#斷言,設置攔截條件,
predicates:
- Path=/discovery
#注意事項一
#上面-Path=/discovery 設置方法生效路由只有http://localhost:9001/discovery
#http://localhost:9001/discovery/xxx... 不能被攔截
#http://localhost:9001/service-consumer/discovery 不能被攔截
#固然也能夠將路由設置爲-Path=/discovery/**,此時http://localhost:9001/discovery/xxx...也能被攔截
#注意事項二
#此處spring.cloud.gateway.locator.enabled須要設置爲true 網關轉發功能才能生效
複製代碼
上面設置:http://localhost:9001/discovery跳轉到http://www.scnunanshen.online/
架構