一,引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.1.3</version>
</dependency>
注意:在使用gateway时需要排除MVC的依赖文件,否则会运行报错。
二,排除数据源
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//排除数据源
public class GulimallGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallGatewayApplication.class, args);
}
}
三,application.yaml
server:
port: 88
spring:
application:
name: gulimall-gateway
cloud:
nacos:
config:
server-addr: localhost:8848
gateway:
routes:
# 路由,添加多个
- id: test_route
uri: https://www.baidu.com
predicates: # 断言,可添加多个
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq
四,测试
输入http://localhost:88/hello?url=baidu
成功!!!下图是因为没有www.baidu.com/hello这个资源