千家信息网

Dubbo泛化如何引用

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章主要介绍"Dubbo泛化如何引用",在日常操作中,相信很多人在Dubbo泛化如何引用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Dubbo泛化如何引用"的疑惑
千家信息网最后更新 2025年02月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泛化如何引用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0