Load simulation module
This commit is contained in:
@@ -3,8 +3,10 @@ package demo;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@@ -13,4 +15,9 @@ public class OrderWorker {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OrderWorker.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import demo.order.event.OrderEvent;
|
||||
import demo.order.event.OrderEventType;
|
||||
import demo.order.event.OrderEvents;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.MediaTypes;
|
||||
import org.springframework.hateoas.client.Traverson;
|
||||
@@ -14,31 +16,57 @@ import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class StateFactory {
|
||||
|
||||
final private Logger log = Logger.getLogger(StateFactory.class);
|
||||
final private StateService stateService;
|
||||
final private RestTemplate restTemplate;
|
||||
final private DiscoveryClient discoveryClient;
|
||||
|
||||
public StateFactory(StateService stateService) {
|
||||
public StateFactory(StateService stateService, RestTemplate restTemplate, DiscoveryClient discoveryClient) {
|
||||
this.stateService = stateService;
|
||||
this.restTemplate = restTemplate;
|
||||
this.discoveryClient = discoveryClient;
|
||||
}
|
||||
|
||||
public Order apply(OrderEvent orderEvent) {
|
||||
Assert.notNull(orderEvent, "Cannot apply a null event");
|
||||
Assert.notNull(orderEvent.getId(), "The event payload's identity link was not found");
|
||||
|
||||
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("order-web");
|
||||
Assert.notEmpty(serviceInstances, "No instances available for order-web");
|
||||
Integer instanceNum = new Random().nextInt(serviceInstances.size());
|
||||
ServiceInstance orderService = serviceInstances.get(instanceNum);
|
||||
|
||||
URI orderHref = getLoadBalanceUri(orderService, URI.create(orderEvent.getLink("order").getHref()));
|
||||
URI selfHref = getLoadBalanceUri(orderService, URI.create(orderEvent.getLink("self").getHref()));
|
||||
|
||||
orderEvent.getLinks()
|
||||
.replaceAll(a -> Objects.equals(a.getRel(), "order") ? new Link(orderHref
|
||||
.toString(), "order") : a);
|
||||
|
||||
orderEvent.getLinks()
|
||||
.replaceAll(a -> Objects.equals(a.getRel(), "self") ? new Link(selfHref
|
||||
.toString(), "self") : a);
|
||||
|
||||
StateMachine<OrderStatus, OrderEventType> stateMachine = getStateMachine(orderEvent);
|
||||
stateMachine.stop();
|
||||
|
||||
return stateMachine.getExtendedState().get("order", Order.class);
|
||||
}
|
||||
|
||||
private URI getLoadBalanceUri(ServiceInstance serviceInstance, URI uri) {
|
||||
return URI.create(uri.toString()
|
||||
.replace(uri.getHost(), serviceInstance.getHost())
|
||||
.replace(":" + uri.getPort(), ":" + String.valueOf(serviceInstance.getPort())));
|
||||
}
|
||||
|
||||
private StateMachine<OrderStatus, OrderEventType> getStateMachine(OrderEvent orderEvent) {
|
||||
Link eventId = orderEvent.getId();
|
||||
log.info(String.format("Order event received: %s", eventId));
|
||||
@@ -67,13 +95,18 @@ public class StateFactory {
|
||||
}
|
||||
|
||||
private OrderEvents getEventLog(OrderEvent event) {
|
||||
|
||||
// Replace the host and port of the link with an available instance
|
||||
URI orderHref = URI.create(event.getLink("order")
|
||||
.getHref());
|
||||
|
||||
// Follow the hypermedia link to fetch the attached order
|
||||
Traverson traverson = new Traverson(
|
||||
URI.create(event.getLink("order")
|
||||
.getHref()),
|
||||
Traverson traverson = new Traverson(orderHref,
|
||||
MediaTypes.HAL_JSON
|
||||
);
|
||||
|
||||
traverson.setRestOperations(restTemplate);
|
||||
|
||||
// Get the event log for the attached order resource
|
||||
return traverson.follow("events")
|
||||
.toEntity(OrderEvents.class)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package demo.order.event;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import demo.domain.AbstractEntity;
|
||||
import org.springframework.hateoas.Link;
|
||||
|
||||
public class OrderEvent extends AbstractEntity {
|
||||
|
||||
@@ -21,6 +23,12 @@ public class OrderEvent extends AbstractEntity {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
@Override
|
||||
public Link getId() {
|
||||
return super.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderEvent{" +
|
||||
|
||||
Reference in New Issue
Block a user