Dubbo参数回调

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


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

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


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

    }
}

你可能感兴趣的:(dubbo,zookeeper,Java)