mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-18 17:58:38 +08:00
2.0
This commit is contained in:
63
doc/docs/files/90011_原理分析之如何存储路由.md
Normal file
63
doc/docs/files/90011_原理分析之如何存储路由.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# 原理分析之如何存储路由
|
||||
|
||||
SOP基于spring cloud,因此会涉及到网关路由。但是开发者不用去配置文件定义路由的隐射关系,SOP自动帮你解决了这个问题。
|
||||
|
||||
## 获取路由信息
|
||||
|
||||
首先明确一点,路由信息由各微服务提供,因此网关需要从注册中心获取各微服务实例,这个通过nacos提供的`watch`来实现。
|
||||
当有新的微服务加入时,Nacos会触发一个事件推送,详见`NacosWatch.java`类
|
||||
|
||||
```java
|
||||
this.publisher.publishEvent(new HeartbeatEvent(this, this.nacosWatchIndex.getAndIncrement()));
|
||||
```
|
||||
|
||||
因此,只要注册了`HeartbeatEvent`事件就能随时感知到微服务实例的变化。如何注册呢,config类实现`ApplicationListener`接口即可。
|
||||
|
||||
```java
|
||||
public class AbstractConfiguration implements ApplicationContextAware, ApplicationListener<HeartbeatEvent> {
|
||||
/**
|
||||
* nacos事件监听,每次微服务变化会触发这个方法
|
||||
* @see org.springframework.cloud.alibaba.nacos.discovery.NacosWatch NacosWatch
|
||||
* @param heartbeatEvent
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(HeartbeatEvent heartbeatEvent) {
|
||||
...这里加载路由信息
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
然后,每个微服务提供一个restful接口,接口地址为:`http://ip:port/sop/routes`,用来返回自身路由信息。网关请求这个接口用来获取路由信息。
|
||||
|
||||
加载路由伪代码如下:
|
||||
|
||||
```java
|
||||
public void onApplicationEvent(HeartbeatEvent heartbeatEvent) {
|
||||
// 获取nacos中的服务实例列表
|
||||
List<Instance> allInstances = namingService.getAllInstances(serviceName);
|
||||
for(Instance instance : allInstances) {
|
||||
// 微服务提供的接口
|
||||
String url = "http://" + instance.getIp + ":" + instance.getPort + "/sop/routes";
|
||||
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
|
||||
if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
||||
// 返回来的路由信息
|
||||
String body = responseEntity.getBody();
|
||||
...加载路由到本地
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
完整代码可参看`com.gitee.sop.gatewaycommon.manager.ServiceRoutesLoader.java`
|
||||
|
||||
路由的存储方式是一个Map,key为路由id,即接口名+版本号。
|
||||
|
||||
```java
|
||||
/**
|
||||
* key:nameVersion
|
||||
*/
|
||||
private Map<String, ZuulTargetRoute> nameVersionTargetRouteMap;
|
||||
```
|
||||
|
||||
因为客户端调用接口都会传递一个接口名和版本号,因此通过这两个字段能够很快查询出路由信息,进行路由转发操作。
|
||||
|
Reference in New Issue
Block a user