您好,登錄后才能下訂單哦!
這篇文章主要介紹“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配置:
<?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>
到此,關于“Dubbo泛化如何引用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。