alibaba / nacos

an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
https://nacos.io
Apache License 2.0
30.07k stars 12.81k forks source link

使用@ComponentScan导致ApplicationReadyEvent监听失效,导致配置变更后无法刷新,ApplicationListener ApplicationReadyEvent not working,when use @componentscan. #2725

Closed suqun closed 4 years ago

suqun commented 4 years ago

Issue Description

Type: bug report

Describe what happened (or what feature you want)

ApplicationListener ApplicationReadyEvent not working,when use @ComponentScan.

springcloud启动类上使用@ComponentScan注解,导致NacosContextRefresher中的ApplicationReadyEvent事件监听失效

Describe what you expected to happen

NacosContextRefresher 监听失效会导致配置中心修改配置,无法刷新,期望可以刷新

How to reproduce it (as minimally and precisely as possible)

  1. config nacos
  2. add @ComponentScan ,指定扫描的包
    package com.wise.boot.proxy;
    @ComponentScan(value = "com.wise")
    @SpringBootApplication
    @EnableDiscoveryClient
    public class MicroServiceProxyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MicroServiceProxyApplication.class, args);
    }
    }
  3. add@RefreshScope @Value使用的类上添加@RefreshScope
  4. start test,service register success, config change not listen 启动测试,服务注册到nacos上成功,但是监听配置变化的日志不打印
2020-05-01 21:40:04  INFO  [main] com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register nacos registry, DEFAULT_GROUP por-b-core-proxy 192.168.0.103:8808 register finished
2020-05-01 21:40:04  INFO  [main] com.wise.boot.proxy.MicroServiceProxyApplication.logStarted Started MicroServiceProxyApplication in 21.194 seconds (JVM running for 23.075)
2020-05-01 21:40:04  INFO  [main] io.lettuce.core.EpollProvider.info Starting without optional epoll library
2020-05-01 21:40:04  INFO  [main] io.lettuce.core.KqueueProvider.info Starting without optional kqueue library
  1. 注释掉@ComponentScan ,启动测试,服务注册成功后打印监听日志,配置可以正常刷新

    2020-05-01 21:22:30  INFO  [main] com.wise.boot.proxy.MicroServiceProxyApplication.logStarted Started MicroServiceProxyApplication in 12.818 seconds (JVM running for 14.59)
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.ClientWorker.addCacheDataIfAbsent [fixed-localhost_8848] [subscribe] por-b-core-proxy.properties+DEFAULT_GROUP
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.CacheData.addListener [fixed-localhost_8848] [add-listener] ok, tenant=, dataId=por-b-core-proxy.properties, group=DEFAULT_GROUP, cnt=1
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.ClientWorker.addCacheDataIfAbsent [fixed-localhost_8848] [subscribe] por-b-core-proxy+DEFAULT_GROUP
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.CacheData.addListener [fixed-localhost_8848] [add-listener] ok, tenant=, dataId=por-b-core-proxy, group=DEFAULT_GROUP, cnt=1
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.ClientWorker.addCacheDataIfAbsent [fixed-localhost_8848] [subscribe] por-common.properties+DEFAULT_GROUP
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.CacheData.addListener [fixed-localhost_8848] [add-listener] ok, tenant=, dataId=por-common.properties, group=DEFAULT_GROUP, cnt=1
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.ClientWorker.addCacheDataIfAbsent [fixed-localhost_8848] [subscribe] por-b-core-proxy-local.properties+DEFAULT_GROUP
    2020-05-01 21:22:30  INFO  [main] com.alibaba.nacos.client.config.impl.CacheData.addListener [fixed-localhost_8848] [add-listener] ok, tenant=, dataId=por-b-core-proxy-local.properties, group=DEFAULT_GROUP, cnt=1
    2020-05-01 21:22:30  INFO  [com.alibaba.nacos.client.Worker.longPolling.fixed-localhost_8848] com.alibaba.nacos.client.config.impl.ClientWorker.run get changedGroupKeys:[]
    2020-05-01 21:22:30  INFO  [RMI TCP Connection(3)-192.168.0.103] com.zaxxer.hikari.HikariDataSource.getConnection HikariPool-1 - Starting...
    2020-05-01 21:22:31  INFO  [RMI TCP Connection(3)-192.168.0.103] com.zaxxer.hikari.pool.PoolBase.getAndSetNetworkTimeout HikariPool-1 - Driver does not support get/set network timeout for connections. (com.mysql.jdbc.JDBC4Connection.getNetworkTimeout()I)
    2020-05-01 21:22:31  INFO  [RMI TCP Connection(3)-192.168.0.103] com.zaxxer.hikari.HikariDataSource.getConnection HikariPool-1 - Start completed.
    2020-05-01 21:22:31  INFO  [RMI TCP Connection(3)-192.168.0.103] io.lettuce.core.EpollProvider.info Starting without optional epoll library
    2020-05-01 21:22:31  INFO  [RMI TCP Connection(3)-192.168.0.103] io.lettuce.core.KqueueProvider.info Starting without optional kqueue library
    2020-05-01 21:23:00  INFO  [com.alibaba.nacos.client.Worker.longPolling.fixed-localhost_8848] com.alibaba.nacos.client.config.impl.ClientWorker.run get changedGroupKeys:[]

Tell us your environment

spring cloud version : Finchley.SR2 spring Cloud Alibaba Version : 2.0.2.RELEASE nacos version :1.2.1

Anything else we need to know?

suqun commented 4 years ago

程序里有个这段代码,注释掉,就可以正常监听,不是注解和Nacos的问题。具体原因不是很了解,有懂的麻烦留个言,多谢

@Component
public class RedisTestService implements ApplicationRunner {
    private static final Logger logger = Logger.getLogger(RedisTestService.class);

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        test();
    }

    public void test(){
        int i = 0;
        while (i==0){
            redisTemplate.opsForValue().set("redis-test", "1");
            try {
                Thread.sleep(1000);
            }catch (Exception e){}
        }
    }
}