diff --git a/doc/docs/files/90011_原理分析之如何存储路由.md b/doc/docs/files/90011_原理分析之如何存储路由.md index 662248c7..575e5f82 100644 --- a/doc/docs/files/90011_原理分析之如何存储路由.md +++ b/doc/docs/files/90011_原理分析之如何存储路由.md @@ -1,52 +1,37 @@ # 原理分析之如何存储路由 -SOP基于spring cloud,因此会涉及到网关路由。但是开发者不用去配置文件定义路由的隐射关系,SOP自动帮你解决了这个问题。 +SOP基于spring cloud,因此会涉及到网关路由。但是开发者不用去配置文件定义路由的隐射关系,SOP帮你解决了这个问题。 ## 获取路由信息 -首先明确一点,路由信息由各微服务提供,因此网关需要从注册中心获取各微服务实例,这个通过nacos提供的`watch`来实现。 -当有新的微服务加入时,Nacos会触发一个事件推送,详见`NacosWatch.java`类 +网关启动成后会触发一个事件,代码见:`com.gitee.sop.gatewaycommon.config.AbstractConfiguration.listenEvent` -```java -this.publisher.publishEvent(new HeartbeatEvent(this, this.nacosWatchIndex.getAndIncrement())); -``` +这个事件会取拉取微服务中提供的路由信息 -因此,只要注册了`HeartbeatEvent`事件就能随时感知到微服务实例的变化。如何注册呢,config类实现`ApplicationListener`接口即可。 +下面以nacos为例,介绍拉取路由过程 -```java -public class AbstractConfiguration implements ApplicationContextAware, ApplicationListener { - @EventListener(classes = HeartbeatEvent.class) - public void listenEvent(ApplicationEvent heartbeatEvent) { - // 有服务注册进来 - } -} -``` +1.从nacos中获取微服务实例 -然后,每个微服务提供一个restful接口,接口地址为:`http://ip:port/sop/routes`,用来返回自身路由信息。网关请求这个接口用来获取路由信息。 +入口代码:`com.gitee.sop.bridge.route.NacosRegistryListener.onEvent` -加载路由伪代码如下: +2.拿到微服务信息,调用微服务提供的接口拉取路由数据 -```java -@Override -public void onAddInstance(InstanceDefinition instance) { - String serviceName = instance.getServiceId(); - String url = getRouteRequestUrl(instance); - log.info("拉取路由配置,serviceId: {}, url: {}", serviceName, url); - ResponseEntity responseEntity = getRestTemplate().getForEntity(url, String.class); - if (responseEntity.getStatusCode() == HttpStatus.OK) { - String body = responseEntity.getBody(); - ServiceRouteInfo serviceRouteInfo = JSON.parseObject(body, ServiceRouteInfo.class); - baseRouteCache.load(serviceRouteInfo, callback -> routesProcessor.saveRoutes(serviceRouteInfo, instance)); - } else { - log.error("拉取路由配置异常,url: {}, status: {}, body: {}", url, responseEntity.getStatusCodeValue(), responseEntity.getBody()); - } -} -``` +入口代码:`com.gitee.sop.gatewaycommon.route.BaseRegistryListener.pullRoutes` + +最终落实到:`com.gitee.sop.gatewaycommon.route.ServiceRouteListener.onAddInstance` + +微服务提供一个url:`http://ip:port/sop/routes`,对应Controller在:`com.gitee.sop.servercommon.route.ServiceRouteController.listRoutes` + +微服务找到被`@Open`注解的方法,然后封装成一个路由对象,放到List中,最后返回给网关。 + +3.网关拿到路由信息,经过处理,转化成网关路由配置 + +关联方法: + +`com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteCache.add` -参考代码: +`com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteCache.refresh` -- `com.gitee.sop.gatewaycommon.route.RegistryListener`以及实现类 -- `com.gitee.sop.gatewaycommon.route.ServiceRouteListener` 路由的存储方式是一个Map,key为路由id,即接口名+版本号。 @@ -57,5 +42,5 @@ public void onAddInstance(InstanceDefinition instance) { private static final Map routes = synchronizedMap(new LinkedHashMap<>()); ``` -因为客户端调用接口都会传递一个接口名和版本号,因此通过这两个字段能够很快查询出路由信息,进行路由转发操作。 +因为客户端调用接口都会传递一个接口名和版本号,因此通过这两个字段能够很快查询出路由信息,然后进行路由转发操作。