1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

update bus example

This commit is contained in:
fangjian0423 2019-03-15 17:09:47 +08:00
parent 1b9cbf7396
commit 69f6ad9128
2 changed files with 66 additions and 48 deletions

View File

@ -16,8 +16,6 @@
*/ */
package org.springframework.cloud.alibaba.cloud.examples.rocketmq; 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.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -30,6 +28,9 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* RocketMQ Bus Spring Application * RocketMQ Bus Spring Application
* *
@ -41,54 +42,61 @@ import org.springframework.web.bind.annotation.RestController;
@RemoteApplicationEventScan(basePackages = "org.springframework.cloud.alibaba.cloud.examples.rocketmq") @RemoteApplicationEventScan(basePackages = "org.springframework.cloud.alibaba.cloud.examples.rocketmq")
public class RocketMQBusApplication { public class RocketMQBusApplication {
public static void main(String[] args) { public static void main(String[] args) {
new SpringApplicationBuilder(RocketMQBusApplication.class) new SpringApplicationBuilder(RocketMQBusApplication.class)
.properties("server.port=0") // Random server port .properties("server.port=0") // Random server port
.properties("management.endpoints.web.exposure.include=*") // exposure includes all .properties("management.endpoints.web.exposure.include=*") // exposure
.properties("spring.cloud.bus.trace.enabled=true") // Enable trace // includes
.run(args); // all
} .properties("spring.cloud.bus.trace.enabled=true") // Enable trace
.run(args);
}
@Autowired @Autowired
private ApplicationEventPublisher publisher; private ApplicationEventPublisher publisher;
@Value("${spring.cloud.bus.id}") @Value("${spring.cloud.bus.id}")
private String originService; private String originService;
@Value("${server.port}") @Value("${server.port}")
private int localServerPort; private int localServerPort;
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
/** /**
* Publish the {@link UserRemoteApplicationEvent} * Publish the {@link UserRemoteApplicationEvent}
* *
* @param name the user name * @param name the user name
* @param destination the destination * @param destination the destination
* @return If published * @return If published
*/ */
@GetMapping("/bus/event/publish/user") @GetMapping("/bus/event/publish/user")
public boolean publish(@RequestParam String name, @RequestParam(required = false) String destination) { public boolean publish(@RequestParam String name,
User user = new User(); @RequestParam(required = false) String destination) {
user.setId(System.currentTimeMillis()); User user = new User();
user.setName(name); user.setId(System.currentTimeMillis());
publisher.publishEvent(new UserRemoteApplicationEvent(user, originService, destination)); user.setName(name);
return true; publisher.publishEvent(
} new UserRemoteApplicationEvent(this, user, originService, destination));
return true;
}
/** /**
* Listener on the {@link UserRemoteApplicationEvent} * Listener on the {@link UserRemoteApplicationEvent}
* *
* @param event {@link UserRemoteApplicationEvent} * @param event {@link UserRemoteApplicationEvent}
*/ */
@EventListener @EventListener
public void onEvent(UserRemoteApplicationEvent event) { public void onEvent(UserRemoteApplicationEvent event) {
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, event.getUser()); System.out.printf("Server [port : %d] listeners on %s\n", localServerPort,
} event.getUser());
}
@EventListener @EventListener
public void onAckEvent(AckRemoteApplicationEvent event) throws JsonProcessingException { public void onAckEvent(AckRemoteApplicationEvent event)
System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, objectMapper.writeValueAsString(event)); throws JsonProcessingException {
} System.out.printf("Server [port : %d] listeners on %s\n", localServerPort,
objectMapper.writeValueAsString(event));
}
} }

View File

@ -26,12 +26,22 @@ import org.springframework.cloud.bus.event.RemoteApplicationEvent;
*/ */
public class UserRemoteApplicationEvent extends RemoteApplicationEvent { public class UserRemoteApplicationEvent extends RemoteApplicationEvent {
public UserRemoteApplicationEvent(User user, String originService, private User user;
public UserRemoteApplicationEvent() {
}
public UserRemoteApplicationEvent(Object source, User user, String originService,
String destinationService) { String destinationService) {
super(user, originService, destinationService); super(source, originService, destinationService);
this.user = user;
}
public void setUser(User user) {
this.user = user;
} }
public User getUser() { public User getUser() {
return (User) getSource(); return user;
} }
} }