引言
當(dāng)后端Java服務(wù)用Dubbo協(xié)議作為RPC方案的基礎(chǔ),但部分消費(fèi)方是前端Restful的PHP服務(wù),不能直接調(diào)用,于是在中間架設(shè)了Router服務(wù)提供統(tǒng)一的基于HTTP的后端調(diào)用入口。
而Router調(diào)用后端Java服務(wù)就應(yīng)用了Dubbo的高級(jí)特性--泛化調(diào)用
- 直接消費(fèi)方(Router服務(wù))不需要引入接口jar包
- 通過GenericService接口來處理所有服務(wù)請(qǐng)求
- 以PHP到Router的request body中的方法名和方法參數(shù)作為Router遠(yuǎn)程調(diào)用后端Java服務(wù)的入?yún)?,最后將遠(yuǎn)程調(diào)用的result返回給PHP端
本文將用一個(gè)小Demo來演示上面所述的泛化調(diào)用應(yīng)用場(chǎng)景
零.Dubbo簡(jiǎn)介
DUBBO是一個(gè)分布式服務(wù)框架,致力于提供高性能和透明化的RPC遠(yuǎn)程服務(wù)調(diào)用方案,是阿里巴巴SOA服務(wù)化治理方案的核心框架,每天為2,000+個(gè)服務(wù)提供3,000,000,000+次訪問量支持,并被廣泛應(yīng)用于阿里巴巴集團(tuán)的各成員站點(diǎn)。
-- Dubbo官方描述
Dubbo能做什么:
- 透明化的遠(yuǎn)程方法調(diào)用
- 就像調(diào)用本地方法一樣調(diào)用遠(yuǎn)程方法
- 只需簡(jiǎn)單配置,沒有任何API侵入。
- 軟負(fù)載均衡及容錯(cuò)機(jī)制
- 可在內(nèi)網(wǎng)替代F5等硬件負(fù)載均衡器
- 服務(wù)自動(dòng)注冊(cè)與發(fā)現(xiàn)
- 不再需要寫死服務(wù)提供方地址,注冊(cè)中心基于接口名查詢服務(wù)提 供者的IP地址,并且能夠平滑添加或刪除服務(wù)提供者
-- 《Dubbo功能介紹》(官方資料)
注:Dubbo的基本使用介紹不在本文范疇,如有需要請(qǐng)自行參考官方資料
泛接口調(diào)用方式主要用于客戶端沒有API接口及模型類元的情況,參數(shù)及返回值中的所有POJO均用Map表示,通常用于框架集成,比如:實(shí)現(xiàn)一個(gè)通用的服務(wù)測(cè)試框架,可通過GenericService調(diào)用所有服務(wù)實(shí)現(xiàn)。
-- Dubbo用戶指南
一.后端API
public interface UserInfoService {
public Map<String, String> getUser(String id);
public Map<String, String>[] getUsers();
}
二.Router端dubbo配置
dubboconf.properties:
application.name=router
registry.address=zookeeper://address1?buckup=address2,address3
三.前端服務(wù)post到Router的Request Body示例:
{
"interfaceName": "foo",
"methodName": "bar",
"methodParams": [
{
"id": "xxx"
}
]
}
四.處理前端參數(shù)用的Dto
RequestDto.java:
import java.util.Map;
/**
* Created by Luo
*/
public class RequestDto {
private String interfaceName;
private String methodName
private Map[] methodParams;
public String getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Map[] getMethodParams() {
return methodParams;
}
public void setMethodParam(Map[] methodParams) {
this.methodParams = methodParams;
}
}
五.Router服務(wù)入口
RouterController.java:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Luo
*/
@RestController
public class App {
@RequestMapping(value = "/router/", method = RequestMethod.POST)
public Object getUser(@ModelAttribute RequestDto dto) {
Map map = new HashMap<>();
map.put("ParamType", "java.lang.String"); //后端接口參數(shù)類型
map.put("Object", dto.getMethodParams()[0].get("id")); //用以調(diào)用后端接口的實(shí)參
List<Map<String, Object>> paramInfos= new ArrayList<>();
paramInfos.add(map);
DubboServiceFactory dubbo = DubboServiceFactory.getInstance();
return dubbo.genericInvoke(dto.getInterfaceName(), dto.getMethodName(), paramInfos);
}
}
注:本文旨在演示泛化調(diào)用的一種應(yīng)用方式,為簡(jiǎn)明起見,代碼中直接從dto中獲取了指定參數(shù),而并沒有完整實(shí)現(xiàn)其路由功能,望見諒。
六.通過GenericService進(jìn)行泛化調(diào)用
DubboServiceFactory.java
package local.demo.genericservice;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.utils.ReferenceConfigCache;
import com.alibaba.dubbo.rpc.service.GenericService;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* Created by Luo
*/
public class DubboServiceFactory {
private ApplicationConfig application;
private RegistryConfig registry;
private static class SingletonHolder {
private static DubboServiceFactory INSTANCE = new DubboServiceFactory();
}
private DubboServiceFactory(){
Properties prop = new Properties();
ClassLoader loader = DubboServiceFactory.class.getClassLoader();
try {
prop.load(loader.getResourceAsStream("dubboconf.properties"));
} catch (IOException e) {
e.printStackTrace();
}
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(prop.getProperty("application.name"));
//這里配置了dubbo的application信息*(demo只配置了name)*,因此demo沒有額外的dubbo.xml配置文件
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(prop.getProperty("registry.address"));
//這里配置dubbo的注冊(cè)中心信息,因此demo沒有額外的dubbo.xml配置文件
this.application = applicationConfig;
this.registry = registryConfig;
}
public static DubboServiceFactory getInstance() {
return SingletonHolder.INSTANCE;
}
public Object genericInvoke(String interfaceClass, String methodName, List<Map<String, Object>> parameters){
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setApplication(application);
reference.setRegistry(registry);
reference.setInterface(interfaceClass); // 接口名
reference.setGeneric(true); // 聲明為泛化接口
/*ReferenceConfig實(shí)例很重,封裝了與注冊(cè)中心的連接以及與提供者的連接,
需要緩存,否則重復(fù)生成ReferenceConfig可能造成性能問題并且會(huì)有內(nèi)存和連接泄漏。
API方式編程時(shí),容易忽略此問題。
這里使用dubbo內(nèi)置的簡(jiǎn)單緩存工具類進(jìn)行緩存*/
ReferenceConfigCache cache = ReferenceConfigCache.getCache();
GenericService genericService = cache.get(reference);
// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
int len = parameters.size();
String[] invokeParamTyeps = new String[len];
Object[] invokeParams = new Object[len];
for(int i = 0; i < len; i++){
invokeParamTyeps[i] = parameters.get(i).get("ParamType") + "";
invokeParams[i] = parameters.get(i).get("Object");
}
return genericService.$invoke(methodName, invokeParamTyeps, invokeParams);
}
}
七.部署
將Router部署到Jetty/Tomcat等容器,或者直接使用SpringBoot開發(fā),發(fā)布為內(nèi)嵌Jetty/Tomcat的獨(dú)立jar包,即可向前端服務(wù)提供服務(wù)。