码迷,mamicode.com
首页 > 编程语言 > 详细

第十三章 SpringCloud之Gateway 路由

时间:2020-01-11 16:58:34      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:event   conf   style   relative   parent   cal   star   route   class   

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

第十三章 SpringCloud之Gateway 路由

标签:event   conf   style   relative   parent   cal   star   route   class   

原文地址:https://www.cnblogs.com/ywjfx/p/12180254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!