http代理 是如何進行注冊自己的服務的
// 注解在Controller上
@SoulSpringMvcClient(path = "/order")
// 注解在方法上
@SoulSpringMvcClient(path = "/findById", desc = "Find by id")
首先被代理的服務啟動的時候會根據(jù)上面的注解加載對應的接口信息,這個注解在 soul-client-springmvc-2.2.1.jar 下面
初始化加載的時候,使用的是SpringMvcClientBeanPostProcessor,這個類實現(xiàn)了BeanPostProcessor接口,BeanPostProcessor的作用主要是在Spring 容器完成 Bean 的實例化、配置和其他的初始化前后添加一些自己的邏輯處理,然后注冊到容器中。
疑問:當admin沒有啟動的時候,注冊失敗后是如何處理的
先啟動SoulTestHttpApplication,再依次啟動SoulAdminBootstrap和SoulBootstrapApplication,執(zhí)行調(diào)用http://localhost:9195/http/order/findById?id=1
{
"code": 500,
"message": "Internal Server Error",
"data": "Did not observe any item or terminal signal within 3000ms in 'peekTerminal' (and no fallback has been configured)"
}
{
"code": -106,
"message": "Can not find url, please check your configuration!",
"data": null
}
無法訪問,等待一會后會重新調(diào)用url,訪問成功
{
"id": "1",
"name": "hello world findById"
}
是哪里發(fā)起的重試?
先看一下注冊服務調(diào)用的地方,/soul-client/springmvc-register
好像只有這里可以注冊,且其他地方?jīng)]有可以注冊的地方,也就是意味著被代理的服務如果先起的話是無法進行訪問的,目前是沒有重試注冊的。
如果是已經(jīng)啟動SoulAdminBootstrap和SoulBootstrapApplication,這個時候啟動被代理的服務,admin是否會自動同步消息到bootstrap?
第一次請求的時候:
{
"code": -107,
"message": "Can not find selector, please check your configuration!",
"data": null
}
后續(xù)請求也是沒有進行同步的,那我們手動點擊同步試試

image.png
還是未找到selector,跟上面同樣的錯誤
那如果是已注冊的服務,先起是否是沒問題的呢?
如果是已經(jīng)注冊過的,順序上面則沒什么要求,很順利地請求到了
注冊后是的同步流程是怎樣的?
注冊的代碼
被代理端注冊在SpringMvcClientBeanPostProcessor
// 初始化結(jié)束后要做的操作
@Override
public Object postProcessAfterInitialization(@NonNull final Object bean, @NonNull final String beanName) throws BeansException {
// 如果isFull是true,則提供所有服務的代理
if (soulSpringMvcConfig.isFull()) {
return bean;
}
// @Controller
Controller controller = AnnotationUtils.findAnnotation(bean.getClass(), Controller.class);
// @RestController
RestController restController = AnnotationUtils.findAnnotation(bean.getClass(), RestController.class);
// @RequestMapping("/order")
RequestMapping requestMapping = AnnotationUtils.findAnnotation(bean.getClass(), RequestMapping.class);
if (controller != null || restController != null || requestMapping != null) {
// @SoulSpringMvcClient(path = "/order") Controller上的soul注解
SoulSpringMvcClient clazzAnnotation = AnnotationUtils.findAnnotation(bean.getClass(), SoulSpringMvcClient.class);
String prePath = "";
if (Objects.nonNull(clazzAnnotation)) {
if (clazzAnnotation.path().indexOf("*") > 1) {
String finalPrePath = prePath;
// 注冊Controller
executorService.execute(() -> RegisterUtils.doRegister(buildJsonParams(clazzAnnotation, finalPrePath), url,
RpcTypeEnum.HTTP));
return bean;
}
prePath = clazzAnnotation.path();
}
final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(bean.getClass());
for (Method method : methods) {
// @SoulSpringMvcClient(path = "/findById", desc = "Find by id") Method上的soul注解
SoulSpringMvcClient soulSpringMvcClient = AnnotationUtils.findAnnotation(method, SoulSpringMvcClient.class);
if (Objects.nonNull(soulSpringMvcClient)) {
String finalPrePath = prePath;
// 注冊Method
executorService.execute(() -> RegisterUtils.doRegister(buildJsonParams(soulSpringMvcClient, finalPrePath), url,
RpcTypeEnum.HTTP));
}
}
}
return bean;
}
上面這段代碼我們可以看到,注冊請求的是/soul-client/springmvc-register
@PostMapping("/springmvc-register")
public String registerSpringMvc(@RequestBody final SpringMvcRegisterDTO springMvcRegisterDTO) {
return soulClientRegisterService.registerSpringMvc(springMvcRegisterDTO);
}
@Override
@Transactional
public String registerSpringMvc(final SpringMvcRegisterDTO dto) {
// 默認非元數(shù)據(jù),這個有可能是元數(shù)據(jù)類型嗎? cutie 20200120
if (dto.isRegisterMetaData()) {
MetaDataDO exist = metaDataMapper.findByPath(dto.getPath());
if (Objects.isNull(exist)) {
saveSpringMvcMetaData(dto);
}
}
// 注冊選擇器
String selectorId = handlerSpringMvcSelector(dto);
// 注冊規(guī)則
handlerSpringMvcRule(selectorId, dto);
return SoulResultMessage.SUCCESS;
}
注冊選擇器
// 注冊選擇器
private String handlerSpringMvcSelector(final SpringMvcRegisterDTO dto) {
// 獲取訪問前綴
String contextPath = dto.getContext();
// 根據(jù)訪問前綴獲取選擇器
SelectorDO selectorDO = selectorService.findByName(contextPath);
// 選擇器id
String selectorId;
// 拼uri 主機ip:端口
String uri = String.join(":", dto.getHost(), String.valueOf(dto.getPort()));
if (Objects.isNull(selectorDO)) {
// 選擇器不存在的話則進行注冊
selectorId = registerSelector(contextPath, dto.getRpcType(), dto.getAppName(), uri);
} else {
// 選擇器存在的話則獲取信息并進行分發(fā)
selectorId = selectorDO.getId();
//update upstream
String handle = selectorDO.getHandle();
String handleAdd;
// 根據(jù)uri創(chuàng)建DivideUpstream對象
DivideUpstream addDivideUpstream = buildDivideUpstream(uri);
// 根據(jù)訪問前綴創(chuàng)建選擇器對象
SelectorData selectorData = selectorService.buildByName(contextPath);
// handle字段即DivideUpstream對象的json串
if (StringUtils.isBlank(handle)) {
handleAdd = GsonUtils.getInstance().toJson(Collections.singletonList(addDivideUpstream));
} else {
// 如果handle字段存在,就遍歷看看是否已經(jīng)保存過了
List<DivideUpstream> exist = GsonUtils.getInstance().fromList(handle, DivideUpstream.class);
for (DivideUpstream upstream : exist) {
if (upstream.getUpstreamUrl().equals(addDivideUpstream.getUpstreamUrl())) {
// 找到了的話就返回選擇器id
return selectorId;
}
}
// 添加到存在列表中
exist.add(addDivideUpstream);
// 轉(zhuǎn)成json串
handleAdd = GsonUtils.getInstance().toJson(exist);
}
// 更新用
selectorDO.setHandle(handleAdd);
// 發(fā)布通知用
selectorData.setHandle(handleAdd);
// update db
selectorMapper.updateSelective(selectorDO);
// submit upstreamCheck 定時更新提交
upstreamCheckService.submit(contextPath, addDivideUpstream);
// publish change event.
// ApplicationEventPublisher是ApplicationContext的父接口之一。這接口的作用是:Interface that encapsulates event publication functionality. 功能就是發(fā)布事件,也就是把某個事件告訴的所有與這個事件相關(guān)的監(jiān)聽器。
// 把當前選擇器發(fā)布給監(jiān)聽選擇器發(fā)布更新的服務,解耦用不錯
eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.UPDATE,
Collections.singletonList(selectorData)));
}
return selectorId;
}
注冊規(guī)則
// 注冊規(guī)則
private void handlerSpringMvcRule(final String selectorId, final SpringMvcRegisterDTO dto) {
// 查詢下規(guī)則是否存在
RuleDO ruleDO = ruleMapper.findByName(dto.getRuleName());
if (Objects.isNull(ruleDO)) {
// 不存在的話就注冊規(guī)則
registerRule(selectorId, dto.getPath(), dto.getRpcType(), dto.getRuleName());
}
}
不存在的話就注冊規(guī)則
// 不存在的話就注冊規(guī)則
private void registerRule(final String selectorId, final String path, final String rpcType, final String ruleName) {
// 包裝規(guī)則
RuleHandle ruleHandle = RuleHandleFactory.ruleHandle(RpcTypeEnum.acquireByName(rpcType), path);
RuleDTO ruleDTO = RuleDTO.builder()
.selectorId(selectorId)
.name(ruleName)
.matchMode(MatchModeEnum.AND.getCode())
.enabled(Boolean.TRUE)
.loged(Boolean.TRUE)
.sort(1)
.handle(ruleHandle.toJson())
.build();
RuleConditionDTO ruleConditionDTO = RuleConditionDTO.builder()
.paramType(ParamTypeEnum.URI.getName())
.paramName("/")
.paramValue(path)
.build();
if (path.indexOf("*") > 1) {
ruleConditionDTO.setOperator(OperatorEnum.MATCH.getAlias());
} else {
ruleConditionDTO.setOperator(OperatorEnum.EQ.getAlias());
}
ruleDTO.setRuleConditions(Collections.singletonList(ruleConditionDTO));
// 注冊規(guī)則
ruleService.register(ruleDTO);
}