spring-cloud / spring-cloud-bus

Spring Cloud event bus
http://cloud.spring.io/spring-cloud-bus/
Apache License 2.0
409 stars 242 forks source link

Cannot consume custom remote events #262

Closed skylarkn closed 1 year ago

skylarkn commented 2 years ago
  1. custom remote events

    package org.example.provider.event;
    
    import org.springframework.cloud.bus.event.RemoteApplicationEvent;
    
    public class CustomerEvent extends RemoteApplicationEvent {
    
        /**
         * 用户名
         */
        private String userName;
    
        private CustomerEvent() {
        }
    
        public CustomerEvent(Object source, String originService, String destinationService, String username) {
            super(source, originService);
            this.userName = username;
        }
    
        public String getUserName() {
            return userName;
        }
    
    }
  2. local listener

    package org.example.consumer.service;
    
    import lombok.extern.slf4j.Slf4j;
    import org.example.provider.event.CustomerEvent;
    import org.springframework.cloud.bus.event.SentApplicationEvent;
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class MessageService {
    
        @EventListener
        public void sendMessage(CustomerEvent event) {
            // dident work
        }
    
    //    @EventListener
    //    public void sendMessage2(RemoteApplicationEvent event) {
    //        log.info("通过事件 调用方法2 : {}", event.getDestinationService());
    //    }
    //
    //    @EventListener
    //    public void sendMessage3(UnknownRemoteApplicationEvent event) {
    //        log.info("通过事件 调用方法3 : {}", event.getDestinationService());
    //    }
    //
          @EventListener
          public void sendMessage4(SentApplicationEvent event) {
              // work event.getDestinationService() was unknow
          }
    }
  3. Remote Application Event Scan

    package org.example.consumer.config;
    
    import org.example.provider.event.CustomerEvent;
    import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @RemoteApplicationEventScan(basePackageClasses = CustomerEvent.class)
    public class BusConfiguration {
    }

add break points in org.springframework.cloud.bus.BusConsumer.accept(RemoteApplicationEvent event), param event are instanceof UnknownRemoteApplicationEvent

dependency version:

<spring-boot.version>2.6.9</spring-boot.version>
<spring.cloud.version>2021.0.3</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
charleshuangcai commented 2 years ago

me too, and depgrade to 2.x.x version is ok.