gateway
路由 由id 目标URI 一系列断言和过滤器组成
断言 根据请求头和请求参数来确定是否连接
过滤器 可以在请求在路由前或者路由后进行修改,添加自己定义的操作
客户端向gateway发送请求,gateway中找到匹配的路由,找到后发送到gateway web handler,handler在通过指定的过滤器来讲i请求发送到我们实际的服务执行业务逻辑,然后返回。
流程为,在路由中包含相关的信息,断言中有这些信息是否为true,若全为true,则看filter中执行自己添加的动作,执行后发往后端
我的理解在真实端口上套了一层马甲
那么开始一系列操作
1建moudle,gateway-test 。
加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2写yaml配置
server:
port: 8514
spring:
application:
name: service-user-gateway-test
main:
web-application-type: reactive
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true #为true代表 我们可以通过微服务名称调用,不推荐
enabled: true #是否开启网关
3主启动类
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
启动
已经入住nacos
现在试一试如何做网关映射,即不想暴露真实地址,套一层网关
同样的建moudle 加依赖,写 写主启动类,写服务进行测试
依赖没啥需要额外添加的
主启动
@EnableDiscoveryClient
@SpringBootApplication
public class GateApplication {
public static void main(String[] args) {
SpringApplication.run(GateApplication.class,args);
}
}
服务层
@RestController
public class TestController {
@GetMapping("little/gateway/test")
public String abb(){
return "success";
}
@GetMapping("little/gateway/test1")
public String aee(){
return "success1";
}
@GetMapping("little/gateway/test2")
public String aff(){
return "success2";
}
}
开始写application.yaml
server:
port: 8901
spring:
application:
name: nacos-gateway-pro-test
main:
web-application-type: reactive
cloud:
nacos:
discovery:
server-addr: localhost:8848
改gateway-test的yaml配置
server:
port: 8900
spring:
application:
name: service-user-gateway-test
main:
web-application-type: reactive
cloud:
nacos:
discovery:
server-addr: http://192.168.150.1:8848
gateway:
discovery:
locator:
enabled: true #为true代表 我们可以通过微服务名称调用,不推荐
enabled: true #是否开启网关
routes: #因为controller中有两个方法所以配两套 id uri predicates
#在server-user.controller.UserInfoController中有两个方法,且端口为8512,现在我们要求暴露的端口是8514,所以进行以下操作
- id: test_route1 #路由ID,全局唯一,没有固定要求
#uri: http://localhost:8901 #目标微服务的请求地址和端口 我们服务交给了nacos 如果有集群,直接指定IP加端口是不可取的 上面的8514是真是地址这里的8020是马甲,是对外暴露的地址
uri: lb:// nacos-gateway-pro-test
predicates:
- Path=/little/gateway/test/** #断言,路径相匹配的进行路由,就是你的@GetMapping("little/gateway/test"),下面同理
# - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问
- id: test_route2 #路由ID,全局唯一
#uri: http://localhost: 8901 #目标微服务的请求地址和端口 我们服务交给了nacos 如果有集群,直接指定IP加端口是不可取的
uri: lb:// nacos-gateway-pro-test
predicates:
- Path=/little/gateway/test2/**
# - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问
- id: test_route3 #路由ID,全局唯一
#uri: http://localhost: 8901 #目标微服务的请求地址和端口 我们服务交给了nacos 如果有集群,直接指定IP加端口是不可取的
uri: lb:// nacos-gateway-pro-test
predicates:
- Path=/little/gateway/test3/**
# - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问
可以试一下localhost:8900/little/gateway/test 和 localhost:8901/little/gateway/test 效果一致,等于是下访问的8900 然后找,找到8901的URI进行访问
这里如果用到openfeign则1可以将其中的
@FeignClient(value = "service-user-test")
//改为@FeignClient(value = "service-user-gateway-test")
类内的方法改为
TestController类内的方法
public interface TestFeign {
@PostMapping("register")
public String register(@RequestBody String userRegisterDto);
}
这样相当于先访问openfeign的端口,这个端口展gateway的马甲端口,马甲端口找真实端口,
端口写死不适合后面的更改,所以改为微服务名uri: lb:// 微服务名
断言 两种shortcut ,fully expand 取比较常见的
predicates:
- Path=/little/gateway/test/** #断言,路径相匹配的进行路由,就是你的@GetMapping("little/gateway/test"),下面同理
# - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问,之前不可访问
#- Before=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间之前才可以访问,超过不可访问
# - Between=2022-05-22T16:35:05.794+08:00[Asia/Shanghai],2022-05-23T16:35:05.794+08:00[Asia/Shanghai] #指定时间之间才可以访问
- Cookie=username,zzbds #两个参数 Cookie name,正则表达式,匹配上执行,否则不执行,username 要等于zzbds
- Header=X-Request-Id,\d+ #X-Request-Id为整数才能访问
- Host=**.some.org,**.someHost.org #可以有多个**为模糊匹配,写清楚的必须符合,即符合前面两个的主机可以访问本服务
- Query=green,\d+ #带请求参数才能访问,参数名为green 且是整数才能访问,不确定是否是必须呆在浏览器中
- RemoteAddr=192,168.3.150/24 #外部访问的IP限制
自定义断言和过滤器后面会讲