目录
二 引入 gulimall-common 依赖和 spring-cloud-starter-gateway 依赖
四 创建 application.properties 文件,指定服务名和注册中心地址
六 创建 bootstrap.properties 文件,添加如下配置,指明配置中心地址和所属命名空间
一 创建 gulimall-gateway 微服务
二 引入 gulimall-common 依赖和 spring-cloud-starter-gateway 依赖
<dependency>
<groupId>com.atguigu.gulimall</groupId>
<artifactId>gulimall-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入 Spring Cloud gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
三 开启网关的服务注册和发现
@EnableDiscoveryClient
// 排除和数据源相关的配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GulimallGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallGatewayApplication.class, args);
}
}
四 创建 application.properties 文件,指定服务名和注册中心地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-gateway
server.port=88
五 在nacos创建配置中心的命名空间和配置
六 创建 bootstrap.properties 文件,添加如下配置,指明配置中心地址和所属命名空间
spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=b3298fbf-2c21-4886-93ba-1154efb68130
七 启动服务 gulimall-gateway
八 测试
现在想要实现针对于“http://localhost:88/hello?url=baidu”,转发到“https://www.baidu.com”,针对于“http://localhost:88/hello?url=qq”的请求,转发到“https://www.qq.com/”
1 创建 application.yml
spring:
cloud:
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
2 启动 gulimall-gateway
3 访问:http://localhost:88/hello?url=baidu
访问:http://localhost:88/hello?url=qq