溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Dubbo泛化如何引用

發(fā)布時(shí)間:2021-12-15 16:26:13 來(lái)源:億速云 閱讀:158 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“Dubbo泛化如何引用”,在日常操作中,相信很多人在Dubbo泛化如何引用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Dubbo泛化如何引用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

泛接口調(diào)用方式主要用于客戶端沒(méi)有API接口及模型類元的情況,參數(shù)及返回值中的所用POJO均使用Map表示,通常用于框架集成,比如:實(shí)現(xiàn)一個(gè)通用的服務(wù)測(cè)試框架,可通過(guò)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配置:

<?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服務(wù)名字 -->

    <dubbo:application name="DubboGenQ"/>

    <!-- 聲明服務(wù)注冊(cè)中心 -->

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

    <!-- 指定傳輸層通信協(xié)議 -->

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

    <!-- 暴露你的服務(wù)地址 -->

    <dubbo:service 

        ref="dubboGenQService" 

        interface="com.dubbo.service.IDubboGenQService"

        protocol="dubbo"

        cluster="failover"

    />

 </beans>

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<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());

            }

        }

        //演示用戶數(shù)據(jù)的封裝

        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服務(wù)名字 -->

    <dubbo:application name="DubboGenQ"/>

    <!-- 聲明服務(wù)注冊(cè)中心 -->

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

    <!-- 指定傳輸層通信協(xié)議  因?yàn)樵谕慌_(tái)機(jī)器測(cè)試,所以端口不一樣-->

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

    <!-- 暴露你的服務(wù)地址 -->

    <dubbo:reference

        id="dubboGenQService" 

        interface="com.dubbo.service.IDubboGenQService"

        protocol="dubbo"

        cache="lru"

        generic="true"

        />

 </beans>

到此,關(guān)于“Dubbo泛化如何引用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI