任务

跑通sofa插件,研究sofa协议实现原理

sofa 原理

昨天的空指针异常,大概率是由于没有实现参数解析器导致,查看官方文档可以看到,需要实现一个参数解析的实现类继承接口。

自定义实现多参数支持:

在网关项目中,新增一个类 A,实现 org.dromara.soul.plugin.api.sofa.SofaParamResolveService

1
2
3
4
5
6
7
8
9
10
11
12
public interface SofaParamResolveService {

/**
* Build parameter pair.
* this is Resolve http body to get sofa param.
*
* @param body the body
* @param parameterTypes the parameter types
* @return the pair
*/
Pair<String[], Object[]> buildParameter(String body, String parameterTypes);
}

body为http中body传的json字符串。

parameterTypes: 匹配到的方法参数类型列表,如果有多个,则使用,分割。

Pair中,left为参数类型,right为参数值,这是sofa泛化调用的标准。

把你的类注册成Spring的bean,覆盖默认的实现。

1
2
3
4
@Bean
public SofaParamResolveService A() {
return new A();
}

Link