springcloud gateway基础
# gateway基础
# 集成nacos
# 引入starter
<!-- nacos starter -->
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--客户端负载均衡loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
从starter的依赖中可以看到,从springcloud2020弃用了Ribbon,因此需要再引入Ribbon的jar包,否则无法通过lb路由到指定微服务,出现了503情况。
# 写配置
spring:
cloud:
gateway:
discovery:
locator:
enabled: true # 根据服务名称从注册中心获取服务请求地址
lower-case-service-id: true #使用小写服务名,默认是大写
routes:
- id: baidu-proxy
uri: https://www.163.com/
predicates:
- Path=/163
- id: url-proxy-1 #保持唯一
uri: https://blog.csdn.net
predicates:
- Path=/csdn
- id: oauth2AuthServer # 路由 ID,唯一
uri: lb://oauth2-auth-server # 根据服务名称从注册中心获取服务请求地址
predicates: # 断言(判断条件)
- Path=/auth/** # 匹配对应 URL 的请求,将匹配到的请求追加在目标 URI 之后
filters:
- StripPrefix=1
- id: oauth2ResouceServer
uri: lb://oauth2-resource-server
predicates:
- Path=/client/**
filters:
#- StripPrefix=1
- RewritePath=/client/(?<segment>/?.*), /$\{segment}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
参考文章: