|
|
|
@ -16,15 +16,19 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.springframework.cloud.alibaba.cloud.examples.rocketmq;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.boot.ApplicationRunner;
|
|
|
|
|
import org.springframework.boot.WebApplicationType;
|
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
|
|
|
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
|
|
|
|
|
import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
|
|
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RocketMQ Bus Spring Application
|
|
|
|
@ -32,33 +36,45 @@ import org.springframework.context.event.EventListener;
|
|
|
|
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
|
|
|
|
* @since 0.2.1
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@EnableAutoConfiguration
|
|
|
|
|
@RemoteApplicationEventScan(basePackages = "org.springframework.cloud.alibaba.cloud.examples.rocketmq")
|
|
|
|
|
public class RocketMQBusApplication {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
new SpringApplicationBuilder(RocketMQBusApplication.class)
|
|
|
|
|
.properties("server.port=0") // Random server port
|
|
|
|
|
.properties("management.endpoints.web.exposure.include=*") // exposure includes all
|
|
|
|
|
.properties("spring.cloud.bus.trace.enabled=true") // Enable trace
|
|
|
|
|
.run(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
|
|
|
|
@Value("${spring.cloud.bus.id}")
|
|
|
|
|
private String originService;
|
|
|
|
|
|
|
|
|
|
@Value("${server.port}")
|
|
|
|
|
private int localServerPort;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Publish the {@link UserRemoteApplicationEvent} to all instances of currentService.
|
|
|
|
|
* Publish the {@link UserRemoteApplicationEvent}
|
|
|
|
|
*
|
|
|
|
|
* @param publisher {@link ApplicationEventPublisher}
|
|
|
|
|
* @param currentService Current application Name
|
|
|
|
|
* @return {@link ApplicationRunner} instance
|
|
|
|
|
* @param name the user name
|
|
|
|
|
* @param destination the destination
|
|
|
|
|
* @return If published
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
public ApplicationRunner publishEventRunner(ApplicationEventPublisher publisher,
|
|
|
|
|
@Value("${spring.application.name}") String currentService) {
|
|
|
|
|
return args -> {
|
|
|
|
|
User user = new User();
|
|
|
|
|
user.setName("Mercy Ma");
|
|
|
|
|
for (int i = 1; i < 10; i++) {
|
|
|
|
|
user.setId(Long.valueOf(i));
|
|
|
|
|
publisher.publishEvent(new UserRemoteApplicationEvent(user, currentService, currentService + ":**"));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
@GetMapping("/bus/event/publish/user")
|
|
|
|
|
public boolean publish(@RequestParam String name, @RequestParam(required = false) String destination) {
|
|
|
|
|
User user = new User();
|
|
|
|
|
user.setId(System.currentTimeMillis());
|
|
|
|
|
user.setName(name);
|
|
|
|
|
publisher.publishEvent(new UserRemoteApplicationEvent(user, originService, destination));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -68,6 +84,11 @@ public class RocketMQBusApplication {
|
|
|
|
|
*/
|
|
|
|
|
@EventListener
|
|
|
|
|
public void onEvent(UserRemoteApplicationEvent event) {
|
|
|
|
|
System.out.println("Listener on User : " + event.getUser());
|
|
|
|
|
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, event.getUser());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventListener
|
|
|
|
|
public void onAckEvent(AckRemoteApplicationEvent event) throws JsonProcessingException {
|
|
|
|
|
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, objectMapper.writeValueAsString(event));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|