spring cloud gateway 入门

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号: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链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

spring cloud gateway 案例

新建一个spring boot项目,添加spring cloud相关依赖的jar包

    <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">
        <parent>
            <artifactId>ch18-1</artifactId>
            <groupId>cn.springcloud.book</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>

        <modelVersion>4.0.0</modelVersion>

        <artifactId>ch18-1-gateway</artifactId>
        <packaging>jar</packaging>

        <name>ch18-1-gateway</name>
        <url>http://springcloud.cn</url>

        <properties>
            <spring-cloud.version>Finchley.M8</spring-cloud.version>
        </properties>

        <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>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

        <repositories>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>

        <pluginRepositories>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>

    </project>

spring cloud gateway 网关路由方式

新的gateway网关路由配置有两种方式:

1.通过@Bean自定义RouteLocator,在启动主类Application中配置

2.在配置文件yml中配置

这两种方式都可以实现网关路由是等价的,但是通常项目开发中会使用配置文件yml方式。

启动类Application配置网关路由:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.gateway.route.RouteLocator;
    import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
    import org.springframework.context.annotation.Bean;

    @SpringBootApplication
    public class SpringCloudGatewayApplication {

        @Bean
        public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
            return builder.routes()
                    //basic proxy
                    .route(r -> r.path("/baidu")
                            .uri("http://baidu.com:80/")
                    ).build();
        }

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

配置文件application.yml配置网关路由:

    server:
      port: 8080
    spring:
      application:
        name: spring-cloud-gateway

    spring:
      cloud:
        gateway:
          routes:
          - id: 163_route
            uri: http://www.163.com/
            predicates:
            - Path=/163

    logging:
      level:
        org.springframework.cloud.gateway: TRACE
        org.springframework.http.server.reactive: DEBUG
        org.springframework.web.reactive: DEBUG
        reactor.ipc.netty: DEBUG

运行测试

配置好了,启动服务

访问 http://localhost:8080/baidu,路由转发到 http://www.baidu.com
访问http://localhost:8080/163, 路由转发到 http://www.163.com

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » spring cloud gateway 入门

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏