Dubbo参数回调

24 篇文章 0 订阅
21 篇文章 0 订阅

dubbo参数回调,参数回调方式与调用本地的callback或listener相同,只需要在spring的配置文件中声明哪个参数时callback类型即可,dubbo将基于长连接生成反向代理,这样就可以从服务器端调用客户端逻辑。
消费者提供者:

package com.yncp.dubbo.entity;

import java.io.Serializable;

public class Computer implements Serializable{ 
    private static final long serialVersionUID = -8956826694546044434L;
    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.yncp.dubbo.entity;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 7318984091634598278L;
    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.yncp.dubbo.service;

import java.io.Serializable;

import com.yncp.dubbo.entity.User;

public interface CallbackListener extends Serializable{
    public User callBack(User v);
}
package com.yncp.dubbo.service;
import com.yncp.dubbo.entity.Computer;
import com.yncp.dubbo.service.CallbackListener;

public interface IDubboCallParamService {

    public Computer methodInvoke(String value,CallbackListener callbackListener);
}
package com.yncp.dubbo.service.impl;

import com.yncp.dubbo.entity.Computer;
import com.yncp.dubbo.entity.User;
import com.yncp.dubbo.service.CallbackListener;
import com.yncp.dubbo.service.IDubboCallParamService;

public class DubboCallParamServiceImpl implements IDubboCallParamService {
    public Computer methodInvoke(String value, CallbackListener callbackListener) {
        User user=new User();
        user.setName(value);
        System.out.println("-----------");
        User callUser = callbackListener.callBack(user);
        callUser.getComputer().setId(1);
        System.out.println(callUser.getComputer().getName());

        System.out.println(callUser.getComputer());

        return callUser.getComputer();
    }

}

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="DubboCallParam"/>
    <!-- 声明服务注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!-- 指定传输层通信协议 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 暴露你的服务地址 -->
    <dubbo:service 
        ref="dubboCallParamService" 
        interface="com.yncp.dubbo.service.IDubboCallParamService"
        protocol="dubbo"
    >
     <dubbo:method name="methodInvoke" >
            <dubbo:argument index="1" callback="true" type="com.yncp.dubbo.service.CallbackListener"/>
       </dubbo:method>      
    </dubbo:service>
 </beans>

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

    <!-- 配置Bean -->
    <bean id="dubboCallParamService" class="com.yncp.dubbo.service.impl.DubboCallParamServiceImpl"/>
    <!-- 引入配置文件 -->
    <import resource="classpath:dubbo.xml"/>
 </beans>

消费者代码测试:

package com.yncp.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.yncp.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.yncp.dubbo.service;

import java.io.Serializable;

import com.yncp.dubbo.entity.User;

public interface CallbackListener extends Serializable{
    public User callBack(User v);
}
package com.yncp.dubbo.service;

import com.yncp.dubbo.entity.Computer;

public interface IDubboCallParamService {
    /**
     * 
     * @param value
     * @param callbackListener
     * @return
     */
    public Computer methodInvoke(String value,
                  CallbackListener callbackListener);
}
import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yncp.dubbo.entity.Computer;
import com.yncp.dubbo.entity.User;
import com.yncp.dubbo.service.CallbackListener;
import com.yncp.dubbo.service.IDubboCallParamService;


public class DubboCallStart {
    public static void main(String[] args) throws IOException {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        IDubboCallParamService demoService=(IDubboCallParamService) ctx.getBean("dubboCallParamService");       
        Computer res=demoService.methodInvoke("zhangsan", new CallbackListener() {
                public User callBack(User v) { 
                    Computer computer=new Computer();
                    computer.setName("客户端设置的Computer");
                    v.setComputer(computer);
                    return v;
                }

        });
        System.out.println(res.getName()+" "+res.getId());

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值