前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dubbo泛化引用

Dubbo泛化引用

作者头像
BUG弄潮儿
发布2022-06-30 14:53:31
1460
发布2022-06-30 14:53:31
举报
文章被收录于专栏:JAVA乐园

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配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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">

<!-- 指定web服务名字 -->

<dubbo:application name="DubboGenQ"/>

<!-- 声明服务注册中心 -->

<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

<!-- 指定传输层通信协议 -->

<dubbo:protocol name="dubbo" port="20880"/>

<!-- 暴露你的服务地址 -->

<dubbo:service

ref="dubboGenQService"

interface="com.dubbo.service.IDubboGenQService"

protocol="dubbo"

cluster="failover"

/>

</beans>

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<String, Object> userMap=(HashMap<String, Object>) resultUser;

for(Map.Entry<String, Object> entry1: userMap.entrySet()){

if(entry1.getValue() instanceof HashMap){

HashMap<String, Object> computerMap=(HashMap<String, Object>) entry1.getValue();

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

for(Map.Entry<String, Object> entry: computerMap.entrySet()){

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

}

}else{

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

}

}

//演示用户数据的封装

Map<String, Object> user = new HashMap<String, Object>();

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

HashMap<String, String> value = new HashMap<String, String>();

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

user.put("computer", value);

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

new Object[]{user});

}

}

dubbo.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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">

<!-- 指定web服务名字 -->

<dubbo:application name="DubboGenQ"/>

<!-- 声明服务注册中心 -->

<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

<!-- 指定传输层通信协议 因为在同一台机器测试,所以端口不一样-->

<dubbo:protocol name="dubbo" port="20881"/>

<!-- 暴露你的服务地址 -->

<dubbo:reference

id="dubboGenQService"

interface="com.dubbo.service.IDubboGenQService"

protocol="dubbo"

cache="lru"

generic="true"

/>

</beans>

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-01-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档