第十三章 SpringCloud之Gateway 路由

#######gateway  路由案例#######

1、pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../</relativePath> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>nacos-client-consumer-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-client-consumer-gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-web</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

注意:必须要  spring-boot-starter-web 的依赖去掉,否则启动有问题

2、application.yml文件

#端口
server:
  port: 8007
#虚拟应用名
spring:
  application:
    name: springcloud-gateway

3、路由启动类

package com.test.nacosclientconsumergateway;

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;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController  //需要加上
public class NacosClientConsumerGatewayApplication {

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

    /**
     * gateway 路由功能
     * @param builder
     * @return
     */
    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                // 凡是请求了/gateway/get 路径的请求,都转发到http://localhost:8001//gateway/get
                .route(p -> p
                        .path("/gateway/get")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri("http://localhost:8001/"))
                .build();
    }
}

4、在localhost:8001项目中添加这个请求的路径

package com.test.nacosserver.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/gateway")
@RestController
public class GatewayController {

    @Value("${server.port}")
    private String port;

    @RequestMapping("/get")
    public String get() {
        return "get gateway "+port;
    }


    @RequestMapping("/set")
    public String set() {
        return "set gateway "+port;
    }

}

5、url请求

http://localhost:8007/gateway/get

 

#############gateway 路由案例2##############

1、pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../</relativePath> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>nacos-client-consumer-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-client-consumer-gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <!--nacos组件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
View Code

2、application.xml

spring:
  cloud:
    gateway:
      routes:  #方法二:路由功能
        - id: nameRoot
          uri: http://localhost:8001  #路由地址
          predicates:
            - Path=/gateway/**   # 由 http://localhost:8080/gateway/xxx 路由到   http://localhost:8001/xxx
          filters:
            - StripPrefix=1  # 截断路由地址

3、再另一个工程(8001端口)创建一个控制类

package com.test.nacosserver.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GatewayController {

    @Value("${server.port}")
    private String port;

    @RequestMapping("/get")
    public String get() {
        return "get gateway "+port;
    }


    @RequestMapping("/set")
    public String set() {
        return "set gateway "+port;
    }

}

4、请求

http://localhost:8080/gateway/get  你会发现,实际的请求地址为: http://localhost:8001/get
因为
配置文件中的
- StripPrefix=1 将gateway过滤了

##################完毕!!!##############

posted @ 2020-01-11 16:21  小白啊小白,Fighting  阅读(545)  评论(0编辑  收藏  举报