這篇文章主要介紹“Dubbo泛化如何引用”,在日常操作中,相信很多人在Dubbo泛化如何引用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Dubbo泛化如何引用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、洛浦網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、洛浦網(wǎng)絡(luò)營銷、洛浦企業(yè)策劃、洛浦品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供洛浦建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
泛接口調(diào)用方式主要用于客戶端沒有API接口及模型類元的情況,參數(shù)及返回值中的所用POJO均使用Map表示,通常用于框架集成,比如:實(shí)現(xiàn)一個(gè)通用的服務(wù)測試框架,可通過GenericService調(diào)用所有服務(wù)實(shí)現(xiàn)。
實(shí)現(xiàn)代碼如下:
服務(wù)器端代碼:
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 提供者啟動(dòng)
public class dubboServerStart {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo.xml");
context.start();
System.in.read();
}
}
客戶端實(shí)現(xiàn):
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
for(Map.Entry
if(entry1.getValue() instanceof HashMap){
HashMap
System.out.println(entry1.getKey());
for(Map.Entry
System.out.println("\t"+entry.getValue());
}
}else{
System.out.println(entry1.getKey()+" "+entry1.getValue());
}
}
//演示用戶數(shù)據(jù)的封裝
Map
user.put("name", "張三");
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" />
到此,關(guān)于“Dubbo泛化如何引用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!