soul数据上传-HTTP

    @PostMapping("/springmvc-register")

    public String registerSpringMvc(@RequestBody final SpringMvcRegisterDTO springMvcRegisterDTO) {

        return soulClientRegisterService.registerSpringMvc(springMvcRegisterDTO);

    }

    @Override

    @Transactional

    public String registerSpringMvc(final SpringMvcRegisterDTO dto) {

        if (dto.isRegisterMetaData()) {

            MetaDataDO exist = metaDataMapper.findByPath(dto.getPath());

            if (Objects.isNull(exist)) {

                saveSpringMvcMetaData(dto);

            }

        }

        String selectorId = handlerSpringMvcSelector(dto);

        handlerSpringMvcRule(selectorId, dto);

        return SoulResultMessage.SUCCESS;

}
private String handlerSpringMvcSelector(final SpringMvcRegisterDTO dto) {

    String contextPath = dto.getContext();

    SelectorDO selectorDO = selectorService.findByName(contextPath);

    String selectorId;

    String uri = String.join(":", dto.getHost(), String.valueOf(dto.getPort()));

    if (Objects.isNull(selectorDO)) {

        // 没有注册过,进行注册

        selectorId = registerSelector(contextPath, dto.getRpcType(), dto.getAppName(), uri);

    } else {

        // 已经注册过进行更新

        selectorId = selectorDO.getId();

        //update upstream

        String handle = selectorDO.getHandle();

        String handleAdd;

        DivideUpstream addDivideUpstream = buildDivideUpstream(uri);

        SelectorData selectorData = selectorService.buildByName(contextPath); // 通过 context-path 解析 selectorData

        if (StringUtils.isBlank(handle)) { // 本地服务为空,添加本地服务

            handleAdd = GsonUtils.getInstance().toJson(Collections.singletonList(addDivideUpstream));

        } else {

            // 本地服务不为空,添加本地服务

            List<DivideUpstream> exist = GsonUtils.getInstance().fromList(handle, DivideUpstream.class);

            for (DivideUpstream upstream : exist) {

                if (upstream.getUpstreamUrl().equals(addDivideUpstream.getUpstreamUrl())) {

                    return selectorId;

                }

            }

            exist.add(addDivideUpstream);

            handleAdd = GsonUtils.getInstance().toJson(exist);

        }

        selectorDO.setHandle(handleAdd);
 
        selectorData.setHandle(handleAdd);

        // update db

        selectorMapper.updateSelective(selectorDO); // 对数据库进行更新

        // submit upstreamCheck

        upstreamCheckService.submit(contextPath, addDivideUpstream);

        // publish change event.

        eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.UPDATE,

        Collections.singletonList(selectorData))); // 发布数据变更事件

    }

    return selectorId;

}

而在客户端,通过Spring的BeanPostProcessor机制,对bean的方法进行扫描,扫描到SoulSpringMvcClient的信息并注对应的API

public class SpringMvcClientBeanPostProcessor implements BeanPostProcessor {

    private final ThreadPoolExecutor executorService;

    private final String url;

    private final SoulSpringMvcConfig soulSpringMvcConfig;

    /**

    * Instantiates a new Soul client bean post processor.

    *

    * @param soulSpringMvcConfig the soul spring mvc config

    */

    public SpringMvcClientBeanPostProcessor(final SoulSpringMvcConfig soulSpringMvcConfig) {

        ValidateUtils.validate(soulSpringMvcConfig);

        this.soulSpringMvcConfig = soulSpringMvcConfig;

        url = soulSpringMvcConfig.getAdminUrl() + "/soul-client/springmvc-register"; // 【soul-admin】请求地址

        executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

    }

    @Override

    public Object postProcessAfterInitialization(@NonNull final Object bean, @NonNull final String beanName) throws BeansException {

        if (soulSpringMvcConfig.isFull()) {

            return bean; // 全局代理,直接返回

        }

        Controller controller = AnnotationUtils.findAnnotation(bean.getClass(), Controller.class);

        RestController restController = AnnotationUtils.findAnnotation(bean.getClass(), RestController.class);

        RequestMapping requestMapping = AnnotationUtils.findAnnotation(bean.getClass(), RequestMapping.class);

        if (controller != null || restController != null || requestMapping != null) { // 被SpringMVC管理的API

            SoulSpringMvcClient clazzAnnotation = AnnotationUtils.findAnnotation(bean.getClass(), SoulSpringMvcClient.class); // 扫描 SoulSpringMvcClient 类相关信息

            String prePath = "";

            if (Objects.nonNull(clazzAnnotation)) {

                if (clazzAnnotation.path().indexOf("*") > 1) {

                    String finalPrePath = prePath;

                    executorService.execute(() -> RegisterUtils.doRegister(buildJsonParams(clazzAnnotation, finalPrePath), url, RpcTypeEnum.HTTP)); // 执行注册HTTP请求

                    return bean;

                }

                prePath = clazzAnnotation.path();

            }

            final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(bean.getClass());

            for (Method method : methods) {
    
                SoulSpringMvcClient soulSpringMvcClient = AnnotationUtils.findAnnotation(method, SoulSpringMvcClient.class); // 扫描 SoulSpringMvcClient 方法相关信息

                if (Objects.nonNull(soulSpringMvcClient)) {

                    String finalPrePath = prePath;

                    executorService.execute(() -> RegisterUtils.doRegister(buildJsonParams(soulSpringMvcClient, finalPrePath), url, RpcTypeEnum.HTTP)); // 执行注册HTTP请求

                }

            }

        }

        return bean;

    }

}
@Slf4j

public final class RegisterUtils {

    private RegisterUtils() {

    }

    /**

    * call register api.

    *

    * @param json request body

    * @param url url

    * @param rpcTypeEnum rcp type

    */

    public static void doRegister(final String json, final String url, final RpcTypeEnum rpcTypeEnum) {

        try {

            String result = OkHttpTools.getInstance().post(url, json); // 发送注册请求

            if (AdminConstants.SUCCESS.equals(result)) {

                log.info("{} client register success: {} ", rpcTypeEnum.getName(), json);

            } else {

                log.error("{} client register error: {} ", rpcTypeEnum.getName(), json);

            }

        } catch (IOException e) {

            log.error("cannot register soul admin param, url: {}, request body: {}", url, json, e);

        }

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值