- 创建第一个SpringBoot应用作为网关服务
# 服务器端口
server.port=8080
# Spring Cloud Gateway 配置
spring.cloud.gateway.routes[0].id=demo1
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/demo1
spring.cloud.gateway.routes[1].id=demo2
spring.cloud.gateway.routes[1].uri=http://localhost:8082
spring.cloud.gateway.routes[1].predicates[0]=Path=/demo2
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
- 创建第二个SpringBoot应用作为后端应用
server.port=8081
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
@RestController
@RequestMapping("/demo1")
public class Demo1Controller {
@GetMapping()
public String getInfo() {
return "demo1 service is running";
}
}
- Demo2同理,只是端口为8082
- 访问http://localhost:8080/demo1或者http://localhost:8080/demo2即可完成网关转发