The role of SpringCloudGateway:
1. The request is first intercepted by the gateway module of the application. After the gateway module authenticates the request and restricts the current, it forwards the specific request to the corresponding module of the current application for processing. In short: the gateway is responsible for the request Route to the specific back-end service.
1. Create a new Springboot project, add the following content to the pom
<!--用于gateway,下载spring WebFlux时总是下载不好,采用去网站下载并导入maven仓库的方式,然后重新导入依赖语句即可-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--用于被consul发现-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--把consul作为配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--actuator是监控系统健康情况的工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. Modify the bootstrap file
server:
port: 8090
spring:
application:
name: gateway
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
config:
enabled: true
format: yaml
default-context: ${spring.application.name}
prefix: config
data-key: data
3. Add configuration information in consul
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: gateway-service #路由唯一id
uri: https://www.baidu.com
predicates:
- Path=/tobaidu/**
- id: gateway-service2
uri: https://www.csdn.net
predicates:
- Path=/tocsdn/**
test:
name: 1234
4. Start the service (if the terminal prompts you to remove the spring-boot-starter-web dependency, just comment out the corresponding dependency in the pom)
Then visit localhost:8090/tobaidu to jump to the Baidu page
Also visit localhost:8090/tocsdn to jump to the csdn page