Spring Cloud Gateway 入门案例

前言


本文主要讲解实现Spring Cloud Gateway路由转发功能java

概念


什么是Spring Cloud Gateway?

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/架构

相关文章
相关标签/搜索