You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "MLeft (GitHub)" <gi...@apache.org> on 2018/10/16 13:50:04 UTC

[GitHub] [incubator-dubbo] MLeft opened issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

- [x] I have searched the [issues](https://github.com/apache/incubator-dubbo/issues) of this repository and believe that this is not a duplicate.
- [x] I have checked the [FAQ](https://github.com/apache/incubator-dubbo/blob/master/FAQ.md) of this repository and believe that this is not a duplicate.

### Environment

* Dubbo version: 2.6.2
* Operating System version: win 10, spring-boot 1.5.9.RELEASE
* Java version: 1.8

### Steps to reproduce this issue

1. Java config
```java
@Configuration
public class HttpConfig {

    @Bean
    RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        return  restTemplate;
    }
}
```
2. Dubbo Service config
```java
@Configuration
public class CanaryConfig {

    private Logger logger = LoggerFactory.getLogger(CanaryConfig.class);

    @Autowired
    private ApplicationConfig application;
    @Autowired
    private RegistryConfig registry;
    @Autowired
    private ProtocolConfig protocol;

    @PostConstruct
    public void init() {
        logger.info("CanaryConfig init ...");
        // 服务提供者暴露服务配置

        // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
        ServiceConfig<CanaryService> service = new ServiceConfig<>();
        service.setApplication(application);
        // 多个注册中心可以用setRegistries()
        service.setRegistry(registry);
        // 多个协议可以用setProtocols()
        service.setProtocol(protocol);
        service.setInterface(CanaryService.class);
        service.setRef(new CanaryServiceImpl());
        service.setFilter("tracing,dubboInterfaceAuthorityFilter,dubboLogFilter");
        // 如果不延迟暴露, 则filter无法进入setter来获取spring实例
        // service.setDelay(5000);

        // 暴露及注册服务
        service.export();
    }

}
```
3. Dubbo filter
```java
public class DubboInterfaceAuthorityFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(DubboInterfaceAuthorityFilter.class);

    public void setRestTemplate(RestTemplate restTemplate) {
        // get restTemplate instance from spring
        logger.info(" into setter");
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        if(restTemplate!=null){
             // my code
        }
        // ...
    }
}
```

Pls. provide [GitHub address] to reproduce this issue.

### Expected Result

DubboInterfaceAuthorityFilter can invoke its setRestTemplate method, to get restTemplate instance from spring

### Actual Result

DubboInterfaceAuthorityFilter do not invoke setRestTemplate method, so that restTemplate is always null in my filter.


### By the way
If i `service.setDelay(5000);` or export this service by dubbo xml:
```xml
<dubbo:service interface="com.canary.service.CanaryService" ref="canaryServiceImpl" version="1.0.3"/>
<bean id="canaryServiceImpl" class="com.canary.service.CanaryServiceImpl"/>
```
DubboInterfaceAuthorityFilter will invoke its setRestTemplate method.

**Why ?**

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org

[GitHub] [incubator-dubbo] diecui1202 commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "diecui1202 (GitHub)" <gi...@apache.org>.
@MLeft Great. Feel free to reopen if necessary.

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] diecui1202 commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "diecui1202 (GitHub)" <gi...@apache.org>.
@MLeft Great. Feel free to reopen if necessary.

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] diecui1202 commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "diecui1202 (GitHub)" <gi...@apache.org>.
The service is exported in init() method, which is before the ApplicationContext finishing initialization. Maybe this is the reason that RestTemplate can not be autowired.

Could you show us the reason why you need to export service so early ?

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] MLeft commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "MLeft (GitHub)" <gi...@apache.org>.
@diecui1202 Thanks for answer. After reading dubbo source code, i found the reason:
1. I export a service in init method, before the ApplicationContext finishing initialization
2. Dubbo will call `ExtensionLoader.getActivateExtension` to create all filters that having @Active annotation immediately, since applicationContext not finishing initialization, my filter's setter method will not invoke

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] diecui1202 commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "diecui1202 (GitHub)" <gi...@apache.org>.
@MLeft Great. Feel free to reopen if necessary.

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] diecui1202 commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "diecui1202 (GitHub)" <gi...@apache.org>.
@MLeft Great. Feel free to reopen if necessary.

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] MLeft commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "MLeft (GitHub)" <gi...@apache.org>.
@diecui1202 Thanks for answer. After reading dubbo source code, i found the reason:
1. I export a service in init method, before the ApplicationContext finishing initialization
2. dubbo will init call `ExtensionLoader.getActivateExtension` to create all filters that having @Active annotation, since ApplicationContext not finishing initialization, my filter's setter method will not invoke

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] diecui1202 closed issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "diecui1202 (GitHub)" <gi...@apache.org>.
[ issue closed by diecui1202 ]

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org


[GitHub] [incubator-dubbo] diecui1202 commented on issue #2651: 使用Java api 提前暴露服务, 导致后续filter 的setter方法未执行

Posted by "diecui1202 (GitHub)" <gi...@apache.org>.
@MLeft Great. Feel free to reopen if necessary.

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2651 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org