https://www.bilibili.com/video/BV1np4y1C7Yf?p=26
1.创建项目gulimall-gateway引入pom
<dependencies>
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--nacos-->
<!--服务注册/发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--配置管理可选-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<!--依赖管理,在dependencies中添加spring-cloud-alibaba相关依赖就不需要指定版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.启动类添加注解,将网关注册进入naco
@EnableDiscoveryClient
3.启动nacos
4.创建配置文件bootstrap.properties并写入属性
spring.application.name=gulimall-gateway
spring.cloud.nacos.config.server-addr=192.168.112.250:8848
5.如果报错创建application.yml并写入属性(就很奇怪不知道是不是nacos的bug)
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.112.250:8848
server:
port: 10003
6.配置yaml
spring:
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu #访问http://localhost:10004/?url=baidu
- id: gulimall-member
uri: lb://gulimall-member
predicates:
- Path=/api/member/** #http://localhost:88/api/member/test
filters:
- RewritePath=/api/(?<segment>.*),/$\{segment}
- id: gulimall-coupon
uri: lb://gulimall-coupon #lb负载均衡到nacos的服务名称
predicates:
- Path=/coupon/** # coupon开头的请求 http://localhost:88/coupon/test