1、接口定义
public interface Service2 {
String sayHello(String name);
String sayHello(ClientInfo clientInfo, String name);
default CompletableFuture<String> sayHelloAsync(String name){
return CompletableFuture.completedFuture(sayHello(name));
}
default CompletableFuture<String> sayHelloAsync(ClientInfo clientInfo, String name){
return CompletableFuture.completedFuture(sayHello(clientInfo, name));
}
}
2、
@Configuration public class DubboConsumerConfiguration {
@Bean
@DubboReference(
check = false,
methods = {
// 如果删除这一行,则不报错
//仅仅配置@Method(name = "sayHelloAsync"),报错
//换成xml配置不报错
//@DubboReference直接在filed上注入不报错
@Method(name = "sayHelloAsync", onreturn = "service2_sayHelloAsync.onreturn")
}
)
public ReferenceBean<Service2> service2() {
return new ReferenceBean<>();
}
}
3、回调接口
public interface Notify {
void onreturn(String ret, String req); void onthrow(Throwable ex, String req);
}
@Component("service2_sayHelloAsync") public class NotifyImpl implements Notify{ @Override public void onreturn(String ret, String name) { System.out.println("callback调用"); var clientAtt = RpcContext.getClientAttachment().getObjectAttachments(); var serverAtt = RpcContext.getServerAttachment().getObjectAttachments(); var serverCtx = RpcContext.getServerContext(); var serviceCtx = RpcContext.getServiceContext(); System.out.println("clientAtt" + JSON.toJSONString(clientAtt)); System.out.println("serverAtt" + JSON.toJSONString(serverAtt)); System.out.println("serverCtx" + JSON.toJSONString(serverCtx.getObjectAttachments())); System.out.println("serviceCtx" + JSON.toJSONString(serviceCtx.getObjectAttachments())); System.out.println("callback调用2"); }
@Override
public void onthrow(Throwable ex, String name) {
}
}
报错
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service2' defined in class path resource [org/chm/dubboexample/application1/DubboConsumerConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1708) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ... 19 more Caused by: java.lang.NullPointerException at org.apache.dubbo.config.spring.reference.ReferenceBeanSupport.convertToString(ReferenceBeanSupport.java:174) at org.apache.dubbo.config.spring.reference.ReferenceBeanSupport.convertToString(ReferenceBeanSupport.java:186) at org.apache.dubbo.config.spring.reference.ReferenceBeanSupport.convertToString(ReferenceBeanSupport.java:178) at org.apache.dubbo.config.spring.reference.ReferenceBeanSupport.generateReferenceKey(ReferenceBeanSupport.java:129) at org.apache.dubbo.config.spring.reference.ReferenceBeanSupport.generateReferenceKey(ReferenceBeanSupport.java:243) at org.apache.dubbo.config.spring.reference.ReferenceBeanManager.addReference(ReferenceBeanManager.java:70) at org.apache.dubbo.config.spring.ReferenceBean.afterPropertiesSet(ReferenceBean.java:242) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1767) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704) ... 29 more 2022-02-02 00:28:14.033 WARN 11163 --- [ Thread-5] c.a.nacos.common.notify.NotifyCenter : [NotifyCenter] Start destroying Publisher 2022-02-02 00:28:14.033 WARN 11163 --- [ Thread-5] c.a.nacos.common.notify.NotifyCenter : [NotifyCenter] Destruction of the end
原提问者GitHub用户lifes
这种方式可以启动。
@DubboReference( check = false, methods = { @Method(name = "sayHelloAsync", onreturn = "service2_sayHelloAsync.onreturn") } ) private Service2 service2;
原回答者GitHub用户lifes
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。