Lab - Microservice Integration Patterns

Return to Workshop

Microservice Integration Patterns

Option 1: Client Aggregation

Option 2: Chaining

Option 3: API Gateway

Step 1

First, deploy the API gateway:

$ cd ~/coolstore
$ git clone -b app-partner https://github.com/epe105/gateway
$ cd gateway

Please update <USERNAME> below with your assigned username

$ oc project coolstore-<USERNAME>
$ mvn clean fabric8:deploy -Popenshift -DskipTests

Step 2

Take a look at the code for the Gateway in your browser : https://github.com/epe105/gateway/blob/master/src/main/java/com/redhat/coolstore/api_gateway/ProductGateway.java

59restConfiguration()
60		.contextPath("/services").apiContextPath("/services-docs")
61		.apiProperty("host", "")
62		.apiProperty("api.title", "CoolStore Gateway API")
63		.apiProperty("api.version", "1.0")
64		.component("servlet")
65		.bindingMode(RestBindingMode.json);
67rest("/products").description("Access the CoolStore products and their availability")
68            .produces(MediaType.APPLICATION_JSON_VALUE)
70.get("/").description("Retrieves the product catalog, including inventory availability").outType(Product.class)
71    .route().id("productRoute")
72          .setBody(simple("null"))
73          .removeHeaders("CamelHttp*")
74          .recipientList(simple("http4://{{env:CATALOG_ENDPOINT:catalog:8080}}/api/catalog")).end()
75          .unmarshal(productFormatter)
76          .split(body()).parallelProcessing()
77          .enrich("direct:inventory", new InventoryEnricher())
78      .end()	            
79.endRest();
 94from("direct:inventory")
 95          .id("inventoryRoute")
 96          .setHeader("itemId", simple("${body.itemId}"))            
 97    .setBody(simple("null"))
 98    .removeHeaders("CamelHttp*")
 99          .recipientList(simple("http4://{{env:INVENTORY_ENDPOINT:inventory:8080}}/api/inventory/${header.itemId}")).end()
100          .setHeader("CamelJacksonUnmarshalType", simple(Inventory.class.getName()))
101          .unmarshal().json(JsonLibrary.Jackson, Inventory.class);
108@Override
109public Exchange aggregate(Exchange original, Exchange resource) {
110
111	// Add the discovered availability to the product and set it back
112	Product p = original.getIn().getBody(Product.class);
113	Inventory i = resource.getIn().getBody(Inventory.class);
114	p.setQuantity(i.getQuantity());
115	p.setLocation(i.getLocation());
116	p.setLink(i.getLink());
117	original.getOut().setBody(p);
118
119	return original;
120
121}

Step 3

Navigate to Applications → Routes to list the current routes.
Notice the www route is the primary route for our monolith:

Step 4

  • Click Create Route to begin creating a new route with the following values:

Step 5

Step 6

Open the gateway source code file and un-comment the lines that implement a filter based on product ID (around line 80). The highlighted code shows you the predicate used for the filter

Use vi to make changes in the code. If you need help in using vi, please click here

$ vi ~/coolstore/gateway/src/main/java/com/redhat/coolstore/api_gateway/ProductGateway.java
77//
78// Uncomment the below lines to filter out products
79//
80//		.process(exchange -> {
81//                    List<Product> originalProductList = (List<Product>)exchange.getIn().getBody(List.class);
82//                    List<Product> newProductList = originalProductList.stream().filter(product ->
83//							!("329299".equals(product.itemId)))
84//						.collect(Collectors.toList());
85//                    exchange.getIn().setBody(newProductList);
86//                })

Save your changes and quit

$ shift + Z
$ shift + Z

Step 7

$ cd ~/coolstore/gateway
$ mvn clean fabric8:deploy -Popenshift -DskipTests
$ oc logs -f dc/gateway
...
--> Success       # --> wait for it!

Step 8

Congratulations!!!

Return to Workshop