首页 » Web前端 » php挪用javadubbo技巧_Dubbo 高级特点实践泛化调用

php挪用javadubbo技巧_Dubbo 高级特点实践泛化调用

访客 2024-12-11 0

扫一扫用手机浏览

文章目录 [+]

而Router调用后端Java做事就运用了Dubbo的高等特性–泛化调用

直接消费方(Router做事)不须要引入接口jar包通过GenericService接口来处理所有做事要求以PHP到Router的request body中的方法名和方法参数作为Router远程调用后端Java做事的入参,末了将远程调用的result返回给PHP端

本文将用一个小Demo来演示上面所述的泛化调用运用处景

php挪用javadubbo技巧_Dubbo 高级特点实践泛化调用

零.Dubbo简介

DUBBO是一个分布式做事框架,致力于供应高性能和透明化的RPC远程做事调用方案,是阿里巴巴SOA做事化管理方案的核心框架,每天为2,000+个做事供应3,000,000,000+次访问量支持,并被广泛运用于阿里巴巴集团的各成员站点。

php挪用javadubbo技巧_Dubbo 高级特点实践泛化调用
(图片来自网络侵删)

– Dubbo官方描述

Dubbo能做什么:

①、透明化的远程方法调用

就像调用本地方法一样调用远程方法

只需大略配置,没有任何API侵入。

②、软负载均衡及容错机制

可在内网替代F5等硬件负载均衡器

③、做事自动注册与创造

不再须要写去世做事供应方地址,注册中央基于接口名查询做事提 供者的IP地址,并且能够平滑添加或删除做事供应者

– 《Dubbo功能先容》(官方资料)

注:Dubbo的基本利用先容不在本文范畴,如有须要请自行参考官方资料

泛接口调用办法紧张用于客户端没有API接口及模型类元的情形,参数及返回值中的所有POJO均用Map表示,常日用于框架集成,比如:实现一个通用的做事测试框架,可通过GenericService调用所有做事实现。

– Dubbo用户指南

一、后端API

public interface UserInfoService { public Map<String, String> getUser(String id); public Map<String, String>[] getUsers();}二、Router端dubbo配置

dubboconf.properties:application.name=routerregistry.address=zookeeper://address1?buckup=address2,address3三、前端做事post到Router的Request Body示例:

{ \"大众interfaceName\公众: \"大众foo\"大众, \"大众methodName\"大众: \公众bar\公众, \"大众methodParams\公众: [ { \"大众id\"大众: \公众xxx\公众 } ]}四、处理前端参数用的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做事入口

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 /@RestControllerpublic class App { @RequestMapping(value = \"大众/router/\"大众, method = RequestMethod.POST) public Object getUser(@ModelAttribute RequestDto dto) { Map map = new HashMap<>(); map.put(\"大众ParamType\公众, \公众java.lang.String\公众); //后端接口参数类型 map.put(\"大众Object\公众, dto.getMethodParams()[0].get(\"大众id\公众)); //用以调用后端接口的实参 List<Map<String, Object>> paramInfos= new ArrayList<>(); paramInfos.add(map); DubboServiceFactory dubbo = DubboServiceFactory.getInstance(); return dubbo.genericInvoke(dto.getInterfaceName(), dto.getMethodName(), paramInfos); }}

注:本文旨在演示泛化调用的一种运用办法,为简明起见,代码中直接从dto中获取了指定参数,而并没有完全实现其路由功能,看见谅。

六、通过GenericService进行泛化调用

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的注册中央信息,因此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实例很重,封装了与注册中央的连接以及与供应者的连接, //须要缓存,否则重复天生ReferenceConfig可能造成性能问题并且会有内存和连接泄露。
//API办法编程时,随意马虎忽略此问题。
//这里利用dubbo内置的大略缓存工具类进行缓存 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开拓,发布为内嵌Jetty/Tomcat的独立jar包,即可向前端做事供应做事。

写在末了:欢迎留言谈论,私信“Java”或“架构资料”有惊喜!
加关注,持续更新!


标签:

相关文章

phplumen安装技巧_灯具穿上透视装

这一系列灯具包括壁灯、吊灯和台灯,统称为「回顾录」系列,是 Plumen 事情室从上世纪 50 年代的事情灯得来的灵感而设计,带有...

Web前端 2024-12-13 阅读0 评论0