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实体间转换
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页