SoulClientController- registerSpringMvc
-
启动http-example的服务时(Started SoulTestHttpApplication) ,会来调用这个方法,注册服务到soul-admin。
@PostMapping("/springmvc-register") public String registerSpringMvc(@RequestBody final SpringMvcRegisterDTO springMvcRegisterDTO) { return soulClientRegisterService.registerSpringMvc(springMvcRegisterDTO); }
-
注册soul-admin的url 是:http://localhost:9095/soul-client/springmvc-register
-
服务的所有control接口都会去注册调用一遍。(如:/http/order/save 会调用1次,/http/order/path/**/name会调用1次。。。)
-
注册接口超过10秒,会读取超时:
o.d.s.client.common.utils.RegisterUtils : cannot register soul admin param, url: http://localhost:9095/soul-client/springmvc-register, request body: {"appName":"http","context":"/http","path":"/http/test/**","pathDesc":"","rpcType":"http","host":"192.168.101.146","port":8188,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false} java.net.SocketTimeoutException: Read timed out
soulClientRegisterService.registerSpringMvc
handlerSpringMvcSelector(dto)
selectorService.findByName(contextPath);
查询选择器是否存在,根据上下文(“/http”)查询数据库是否已经存在,
不存在则调用 注册选择器到数据库的方法。
registerSelector(final String contextPath, final String rpcType, final String appName, final String uri);
存在则更新数据:
selectorId = selectorDO.getId();
//update upstream 接口上游信息 //[{"upstreamHost":"localhost","protocol":"http://","upstreamUrl":"1...46:8188","weight":50,"status":true,"timestamp":0,"warmup":0}]
String handle = selectorDO.getHandle();
// 更新URI
DivideUpstream addDivideUpstream = buildDivideUpstream(uri);
// update db
selectorMapper.updateSelective(selectorDO);
// submit upstreamCheck
upstreamCheckService.submit(final String selectorName, final DivideUpstream divideUpstream);
缓存在一个 Map<String, List> UPSTREAM_MAP里,有定时任务每10秒查缓存UPSTREAM_MAP是否有效。
new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), SoulThreadFactory.create("scheduled-upstream-task", false))
.scheduleWithFixedDelay(this::scheduled, 10, scheduledTime, TimeUnit.SECONDS);
发布事件后通过监听器 监听数据(SelectorData )改变
// publish change event.
eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.UPDATE,
Collections.singletonList(selectorData)));
handlerSpringMvcRule(selectorId, dto);
先查询规则是否已经存在,不存在则注册规则(registerRule)。
private void handlerSpringMvcRule(final String selectorId, final SpringMvcRegisterDTO dto) {
RuleDO ruleDO = ruleMapper.findByName(dto.getRuleName());
if (Objects.isNull(ruleDO)) {
registerRule(selectorId, dto.getPath(), dto.getRpcType(), dto.getRuleName());
}
}
根据rpcType获得RuleHandle,http->DivideRuleHandle.class, DUBBO -> DubboRuleHandle.class,SOFA -> SofaRuleHandle.class
RuleHandle ruleHandle = RuleHandleFactory.ruleHandle(RpcTypeEnum.acquireByName(rpcType), path);
保存规则和规则数据细表。
ruleMapper.insertSelective(ruleDO);
ruleConditions.forEach(ruleConditionDTO -> {
ruleConditionDTO.setRuleId(ruleDO.getId());
ruleConditionMapper.insertSelective(RuleConditionDO.buildRuleConditionDO(ruleConditionDTO));
});
推送数据改变的事件
publishEvent(final RuleDO ruleDO, final List<RuleConditionDTO> ruleConditions)