Message bus - SpringCloud Bus

Bus Introduction

Spring Cloud Bus is the message bus within the Spring Cloud system and supports two message middlewares: RabbitMQ and Kafka. The so-called message bus, simply understood, is a message center. Many microservice instances can be connected to the bus. The instances can send or receive information to the message center. For example: instance A sends a message to the bus, and instance B on the bus can receive it. to information (instance B subscribes to instance A), the message bus acts as an intermediary, decoupling instance A and instance B


Spring Cloud Bus in practice

Spring Cloud Bus can combine Spring event mechanism and Stream. The specific mechanism is as follows:

  • @RemoteApplicationEventScanAdd annotations to applications that need to publish or listen to events . Through this annotation,
    you can start the binding of message channels in Stream.
  • For event publishing, you need to inherit ApplicationEventthe extended class RemoteApplicationEvent. When ApplicationContext.publishEvent()publishing an event through
  • For event listeners, there is no need to make any changes, and message monitoring is still implemented in the Spring way.

Install and start ZooKeeper and Kafka, create an event publisher project, and introduce dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>

Define the user event class UserEvent and implement RemoteApplicationEvent

@Data
@Slf4j
@EqualsAndHashCode(callSuper = true)
public class UserEvent extends RemoteApplicationEvent {
    
    

    public UserEvent(Object source, String originService, String destination) {
    
    
        super(source, originService, destination);
    }
}
  • originService: For the event publisher, originService is itself
  • destinationService: Which microservice instances the event is published to. The configuration format is. {serviceld):{appContextId)Serviceld and appContextld can use wildcards during configuration. For example, userservice:**the event will be published to the userservice microservice.

The code to publish the message is as follows

@Slf4j
@RestController
public class TestCon {
    
    

    @Autowired
    private ApplicationContextHolder holder;

    @GetMapping("/test/userEvent")
    public void userAdd() {
    
    
        User user = new User();
        user.setId("2");
        user.setName("tom");
        user.setAge(50);
        ApplicationContext ctx = ApplicationContextHolder.getApplicationContext();
        UserEvent event = new UserEvent(user, ctx.getId(), "*:**");
        ctx.publishEvent(event);
    }
}

Add the following configuration in the configuration file:

spring:
  cloud:
    stream:
      default-binder: kafka
      kafka:
        binder:
          brokers: localhost:9092

Add @RemoteApplicationEventScanannotations to the startup class

@SpringBootApplication
@RemoteApplicationEventScan
public class Server01Application {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(Server01Application.class, args);
    }
}

Create an event receiver project, introduce the same dependencies as the event publisher, copy the UserEvent class to this module, and implement the event listening class UserEventListener

@Slf4j
@Component
public class UserEventListener implements ApplicationListener<UserEvent> {
    
    

    @Override
    public void onApplicationEvent(UserEvent event) {
    
    
        log.info("收到用户事件: {}", event);
    }
}

Add the same configuration and startup class annotations as the event publisher

Start two projects and request the event publisher's /test/userEventinterface to publish and receive events.

Guess you like

Origin blog.csdn.net/CSDN_handsome/article/details/133548618