apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.44k stars 26.42k forks source link

NullPointException when requesting MetadataService on a injected Spring bean custom filter #12122

Closed nikotung closed 1 year ago

nikotung commented 1 year ago

Environment

Steps to reproduce this issue

I created a demo to reproduce the issue.

While I tried to fix this issue #12078 , I found a NullPointException when the consumer start up and request to org.apache.dubbo.registry.client.metadata.MetadataServiceDelegation#getMetadataInfo service in the custom filter which injected a Spring bean successfully. This NullPointException only happen when request to the MetadataService, the other requests don't affect. It since likes there are two custom filter instances created for the same type.

And I also found the consumer doesn't register to nacos with the following config, can't find the consumer instance on the nacos console.

  dubbo:
  application:
    name: service-consumer-dubbo
    register-mode: instance
    service-discovery:
      migration: FORCE_APPLICATION
    parameters:
      register-consumer-url: true
    qos-enable: false
    register-consumer: true

Expected Behavior

Actual Behavior

AlbumenJ commented 1 year ago

Do you mean that you want to inject MetadataService into your bean?

nikotung commented 1 year ago

No, I didn't want to do anything with MetadataService. I write a custom filter injected a Spring bean on provider side and the provider starts up normally, the bean was successfully injected to the filter (not null). Also I have a consumer, during start-up this consumer will make a org.apache.dubbo.registry.client.metadata.MetadataServiceDelegation#getMetadataInfo request to the provider, I think this is by-design to get provider meta data. When the request come to the provider, it will first invoke the custom filter. The problem is that the Spring bean in the invoked custom filter is null, it only be null when the consumer request org.apache.dubbo.registry.client.metadata.MetadataServiceDelegation#getMetadataInfo. When I try to request our business interface this custom filter work as expected, no NullPointException. Very time the consumer start up, it can reproduce. This is why I guest that the custom filter instanced twice.

@AlbumenJ You may check my demo here.

AlbumenJ commented 1 year ago

There are two Filter instance for MetadataService (Internal Services) and user-define services. Filter for internal serivces is useless and you can ignore it.

nikotung commented 1 year ago

Thanks, now I know that the user-define Filter would be instanced twice, for Internal Services and User-define Services.

But I think the Filter should work exactly in both Internal Service and User-define Services, in my case, the Filter instance for Internal Service(MetadataService) was not injected a Spring bean via public void setBeanToInject(BeanToInject beanToInject) method which caused a NullPointException.

AlbumenJ commented 1 year ago

The reason is that spring context is on Dubbo module model level.

nikotung commented 1 year ago

Ok. So now my question is how can I avoid or fix this NullPointException? Or it's not a good idea to inject Spring bean in Dubbo3 via a setter method?

AlbumenJ commented 1 year ago

Or it's not a good idea to inject Spring bean in Dubbo3 via a setter method?

It would be better to create a Factory class for yourself.

nikotung commented 1 year ago

Thanks. I think it would be great to write this in docs,like:

nikotung commented 1 year ago

For the question "Dubbo consumer not register to Nacos", can you @AlbumenJ help to check. Here is the configuration:

 dubbo:
  application:
    name: service-consumer-dubbo
    register-mode: instance
    service-discovery:
      migration: FORCE_APPLICATION
    parameters:
      register-consumer-url: true
    qos-enable: false
    register-consumer: true
  registry:
    address: nacos://127.0.0.1:8848
    parameters:
      register-consumer-url: true

Dubbo3.0.0版本以后,增加了是否注册消费者的参数,如果需要将消费者注册到nacos注册中心上,需要将参数(register-consumer-url)设置为true,默认是false。

From the docs. I specified the register-consumer-url to true, but still can't find the consumer in Nacos console.

AlbumenJ commented 1 year ago

In application service discovery mode, consumer node is no longer supported.