Exercise 3 - Deploy Service to OpenShift
In this exercise you will deploy your 'My-Web-API' service to OpenShift.

Step 1: Change the Endpoint URL

Once the service is running on Kubernetes or OpenShift, it can access other services easily via DNS, for example in our case via http://articles-reactive:8080/. In that case the traffic only occurs
Re-create the class ArticlesDataAccess.java.
1
cd ~/cloud-native-starter/reactive/rest-json-quickstart/src/main/java/org/acme/rest/json/
2
rm ArticlesDataAccess.java
3
touch ArticlesDataAccess.java
4
nano ArticlesDataAccess.java
Copied!
1
package org.acme.rest.json;
2
3
import org.eclipse.microprofile.config.inject.ConfigProperty;
4
import org.eclipse.microprofile.rest.client.RestClientBuilder;
5
import javax.annotation.PostConstruct;
6
import javax.enterprise.context.ApplicationScoped;
7
import javax.ws.rs.core.UriBuilder;
8
import java.net.URI;
9
import java.util.List;
10
import java.util.concurrent.CompletionStage;
11
import java.util.concurrent.TimeUnit;
12
13
@ApplicationScoped
14
public class ArticlesDataAccess {
15
16
private static final int MAXIMAL_DURATION = 5000;
17
18
// this configuration needs to be used when running the code in OpenShift
19
private static String urlArticlesServiceLocal = "http://articles-reactive:8080/v2/articles?amount=10";
20
21
private ArticlesService articlesService;
22
23
@PostConstruct
24
void initialize() {
25
URI apiUrl = UriBuilder.fromUri(urlArticlesServiceLocal).build();
26
articlesService = RestClientBuilder.newBuilder()
27
.baseUri(apiUrl)
28
.register(ExceptionMapperArticles.class)
29
.build(ArticlesService.class);
30
}
31
32
public CompletionStage<List<Article>> getArticlesReactive(int amount) {
33
return articlesService.getArticlesFromService(amount);
34
}
35
}
Copied!
Exit the Editor via 'Ctrl-X', 'y' and 'Enter'.

Step 2: Build the Image

Invoke the following commands to build the image.
1
cd ~/cloud-native-starter/reactive/rest-json-quickstart/
2
oc project cloud-native-starter
3
mvn package
4
mv src/main/docker/Dockerfile.jvm ./Dockerfile
5
oc new-build --name my-web-api-reactive --binary --strategy docker
6
oc start-build my-web-api-reactive --from-dir=.
Copied!
deploy-my-web-api
The build will take some time. In the meantime you can check out the Dockerfile, for example to see how the ubi-minimal image is used.
1
cd ~/cloud-native-starter/reactive/rest-json-quickstart/
2
cat Dockerfile
Copied!
Open the OpenShift Web Console and wait for the build to be completed.
deploy2
The output of the build will be a new image stream.
deploy3

Step 3: Deploy and test the Image

In the 'Developer' view, click on 'Add' and then 'Container Image'.
deploy4
On the next page choose 'Image from internal registry' and define the project, stream name and tag.
deploy5
Leave all other defaults including 'Create a route' and click 'Create'.
deploy6
On the next page click on 'my-web-api-reactive'.
deploy7
Wait until the pod is running. After this click on the route at the bottom.
deploy8
This will display the home page of the generated service. To invoke your endpoint append 'articles' to the URL.
deploy9