千家信息网

Dubbo泛化如何引用

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍"Dubbo泛化如何引用",在日常操作中,相信很多人在Dubbo泛化如何引用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Dubbo泛化如何引用"的疑惑
千家信息网最后更新 2026年11月03日Dubbo泛化如何引用

这篇文章主要介绍"Dubbo泛化如何引用",在日常操作中,相信很多人在Dubbo泛化如何引用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Dubbo泛化如何引用"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值中的所用POJO均使用Map表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过GenericService调用所有服务实现。

实现代码如下:

服务器端代码:

package com.dubbo.entity;

import java.io.Serializable;

public class Computer implements Serializable{

private static final long serialVersionUID = 1L;

private Integer id;

private String name;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

package com.dubbo.entity;

import java.io.Serializable;

public class User implements Serializable {

private static final long serialVersionUID = 1L;

private Integer id;

private String name;

private Computer computer;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Computer getComputer() {

return computer;

}

public void setComputer(Computer computer) {

this.computer = computer;

}

}

package com.dubbo.service;

import com.dubbo.entity.User;

public interface IDubboGenQService {

public User queryUser(Integer id);

public void saveUser(User user);

}

package com.dubbo.service.impl;

import com.dubbo.entity.Computer;

import com.dubbo.entity.User;

import com.dubbo.service.IDubboGenQService;

public class DubboGenQServiceImpl implements IDubboGenQService {

public User queryUser(Integer id) {

User user=new User();

user.setId(id);

user.setName("张三"+id);

Computer computer=new Computer();

computer.setId(id);

computer.setName("张三的电脑");

user.setComputer(computer);

return user;

}

public void saveUser(User user) {

System.out.println("用户保存了");

System.out.println(user.getName());

System.out.println(user.getComputer().getName());

}

dubbo.xml配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

ref="dubboGenQService"

interface="com.dubbo.service.IDubboGenQService"

protocol="dubbo"

cluster="failover"

/>

dubbo 提供者启动

public class dubboServerStart {

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo.xml");

context.start();

System.in.read();

}

}

客户端实现:

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.rpc.service.GenericService;

public class DubboClientStart {

public static void main(String[] args) throws IOException {

ApplicationContext ctx=new ClassPathXmlApplicationContext("dubbo.xml");

GenericService genericService=(GenericService) ctx.getBean("dubboGenQService");

Object resultUser = genericService.$invoke("queryUser",

new String[] { "java.lang.Integer"},

new Object[] { 1 });

HashMap userMap=(HashMap) resultUser;

for(Map.Entry entry1: userMap.entrySet()){

if(entry1.getValue() instanceof HashMap){

HashMap computerMap=(HashMap) entry1.getValue();

System.out.println(entry1.getKey());

for(Map.Entry entry: computerMap.entrySet()){

System.out.println("\t"+entry.getValue());

}

}else{

System.out.println(entry1.getKey()+" "+entry1.getValue());

}

}

//演示用户数据的封装

Map user = new HashMap();

user.put("name", "张三");

HashMap value = new HashMap();

value.put("name", "张三的电脑");

user.put("computer", value);

genericService.$invoke("saveUser", new String[]{"com.dubbo.entity.User"},

new Object[]{user});

}

}

dubbo.xml配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

id="dubboGenQService"

interface="com.dubbo.service.IDubboGenQService"

protocol="dubbo"

cache="lru"

generic="true"

/>

到此,关于"Dubbo泛化如何引用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

张三 学习 服务 代码 客户 客户端 接口 更多 框架 用户 电脑 帮助 配置 实用 接下来 参数 情况 所用 提供者 数据 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 湖南永感互联网科技有限公司 江苏根荣互联网科技 互联网科技未来 深圳互联网科技公司 易通互联网络科技有限公司 融势互联网科技 互联网 科技服务平台 科技与互联网的利与弊 深圳前海聚择互联网科技有限公司 中国的互联网科技企业的崛起 互联网企业和科技型企业 阿大互联网科技有限公司 互联网金融科技展 本届互联网博览会上的黑科技 互联网科技公司营业执照怎么办理 互联网科技资金流入 富国互联网金融科技混合基金 从那以后互联网科技有限公司 广州有谷有贝互联网科技有限公司 中国互联网公司的科技投入 互联网科技宣传片 成都狮子座互联网科技 中国互联网金融科技基金 互联网软件和科技 互联网科技革命什么开始 广州市恰铎互联网科技有限公司 元气玛特互联网科技公司是外包吗 互联网金融科技元年 山东星卡互联网科技有限公司官网 江苏互联网科技小贷
0