第十三章 SpringCloud之Gateway 路由
Posted 小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十三章 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>
注意:必须要 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 路由的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud - Spring Cloud 之 Gateway网关,Route路由,Predicate 断言,Filter 过滤器(十三)