Dubbo事件通知

来源:互联网 发布:她来听我的演唱会知乎 编辑:程序博客网 时间:2024/06/04 18:14

dubbo在调用之前,之后,出现异常时,会触发oninvoke,onreturn,onthrow三个事件,可配置当事件发生时,通知那个类的那个方法。
如以下实现:
服务提供者:

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 com.yncp.dubbo.entity.User;public interface IDubboEvenService {    public User methodInvoke(Integer id,String value);}
package com.yncp.dubbo.service.impl;import com.yncp.dubbo.entity.User;import com.yncp.dubbo.service.IDubboEvenService;public class DubboEvenServiceImpl implements IDubboEvenService {    public User methodInvoke(Integer id,String name) {         User user=new User();        user.setId(id);        user.setName(name);        System.out.println("抛出异常");        if(id<0) throw new RuntimeException("用户参数不合法");        return user;    }}

服务消费者:

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 com.yncp.dubbo.entity.User;public interface IDubboEvenService {    public User methodInvoke(Integer id,String value);}
package com.yncp.dubbo.service;public interface INotify {    public void onreturn(Object res,Object... args);    public void onthrow(Throwable ex,Object... args);}
package com.yncp.dubbo.service.impl;import com.yncp.dubbo.service.INotify;public class Notify implements INotify {    public void onreturn(Object res, Object... args) {        System.out.println("返回值:"+res);        for (Object object : args) {            System.out.println("参数:"+object);        }    }    public void onthrow(Throwable ex, Object... args) {        System.out.println("异常:"+ex.getMessage());        for (Object object : args) {            System.out.println("异常参数:"+object);        }    }}

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 id="notify" class="com.yncp.dubbo.service.impl.Notify"></bean>    <import resource="classpath:dubbo.xml"/> </beans>```.dubbo.xml配置<div class="se-preview-section-delimiter"></div>

这里写代码片
“`

<?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="DubboEven_ref"/>    <!-- 声明服务注册中心 -->    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>    <dubbo:protocol name="dubbo" port="20881"/>    <!-- 暴露你的服务地址 -->    <dubbo:reference        id="dubboEvenService"         interface="com.yncp.dubbo.service.IDubboEvenService"        protocol="dubbo"     >        <dubbo:method name="methodInvoke"           onreturn="notify.onreturn"          onthrow="notify.onthrow"         />     </dubbo:reference> </beans>```.测试:<div class="se-preview-section-delimiter"></div>

这里写代码片
“`

import java.io.IOException;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yncp.dubbo.entity.User;import com.yncp.dubbo.service.IDubboEvenService;public class DubboStart {    public static void main(String[] args) throws IOException {        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        IDubboEvenService dubboEvenService=(IDubboEvenService) ctx.getBean("dubboEvenService");        try {            User user = dubboEvenService.methodInvoke(1, "jiangzz");            System.out.println(user.getName()+"  "+user.getId());        } catch (Exception e) {         }    }}
1 0