soul网关案例学习(2):http代理和divide插件

前文疑惑

1. ConditionTransfer无法初始化是啥回事?
2. 代理的服务具体是如何注册上去的?

本篇先解答下上述两个问题。
1. ConditionTransfer无法初始化
ConditionTransfer无法初始化在后台还发现如下日志:

Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for org.dromara.soul.admin.transfer.ConditionTransfer
	at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61) ~[mapstruct-1.3.1.Final.jar:na]
	at org.dromara.soul.admin.transfer.ConditionTransfer.<clinit>(ConditionTransfer.java:39) ~[classes/:na]
	... 75 common frames omitted
Caused by: java.lang.ClassNotFoundException: Cannot find implementation for org.dromara.soul.admin.transfer.ConditionTransfer
	at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75) ~[mapstruct-1.3.1.Final.jar:na]
	at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58) ~[mapstruct-1.3.1.Final.jar:na]
	... 76 common frames omitted

看异常信息,应该是跟mapstruct的自动生成有关,由于mapstruct之前没接触过,在网上大致搜了下,发现一个解决方案,在eclipse中对soul-admin工程做了如下处理:

在项目上右键 – > properties – > Java Compiler – > Annotation Processing (Enable 所有项) – > Factory Path (Enable) – > Add External JARS – > 选择mapstruct-processor-*.jar(大概在计算机的目录.m2\repository\org\mapstruct\mapstruct-processor) – > OK

重启本地eclipse环境下的admin, bootstrap和http test,对应的选择器和规则可以自动注册上去。不过具体原因暂未细究。

2. 代理的服务具体是如何注册上去
也就是对应的选择器和规则如何注册到admin中,探索过程如下:
搜索SoulSpringMvcClient,找到对应注解处理函数org.dromara.soul.client.springmvc.init.SpringMvcClientBeanPostProcessor.postProcessAfterInitialization(Object, String),关键代码如下:

String contextPath = soulSpringMvcConfig.getContextPath();
            SoulSpringMvcClient clazzAnnotation = AnnotationUtils.findAnnotation(bean.getClass(), SoulSpringMvcClient.class);
            String prePath = "";
            if (Objects.nonNull(clazzAnnotation)) {
                if (clazzAnnotation.path().indexOf("*") > 1) {
                    String finalPrePath = prePath;
                    executorService.execute(() -> post(buildJsonParams(clazzAnnotation, contextPath, finalPrePath)));
                    return bean;
                }
                prePath = clazzAnnotation.path();
            }

其中post函数就是像admin的注册过程,post函数如下:

private void post(final String json) {
        try {
            String result = OkHttpTools.getInstance().post(url, json);
            if (Objects.equals(result, "success")) {
                log.info("http client register success :{} ", json);
            } else {
                log.error("http client register error :{} ", json);
            }
        } catch (IOException e) {
            log.error("cannot register soul admin param :{}", url + ":" + json);
        }
    }

断点调试,捕捉到其中一次注册的url和传递参数如下:

http://localhost:9095/soul-client/springmvc-register

{
	"appName": "http",
	"context": "/http",
	"path": "/http/test/**",
	"pathDesc": "",
	"rpcType": "http",
	"host": "172.30.82.27",
	"port": 8187,
	"ruleName": "/http/test/**",
	"enabled": true,
	"registerMetaData": false
}

在admin这边,接收注册信息后走到函数org.dromara.soul.admin.service.impl.SoulClientRegisterServiceImpl.registerSpringMvc(SpringMvcRegisterDTO),函数具体如下:

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 "success";
    }

天色已晚,暂且搁笔。

参考内容

  1. MapStruct实体间转换
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值