# 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](https://github.com/nheidloff/workshop-quarkus-openshift-reactive-endpoints/blob/master/finish/rest-json-quickstart/src/main/java/org/acme/rest/json/ArticlesDataAccess.java).

```bash
cd ~/cloud-native-starter/reactive/rest-json-quickstart/src/main/java/org/acme/rest/json/
rm ArticlesDataAccess.java
touch ArticlesDataAccess.java
nano ArticlesDataAccess.java
```

```java
package org.acme.rest.json;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

@ApplicationScoped
public class ArticlesDataAccess {

    private static final int MAXIMAL_DURATION = 5000;

    // this configuration needs to be used when running the code in OpenShift
    private static String urlArticlesServiceLocal = "http://articles-reactive:8080/v2/articles?amount=10";

    private ArticlesService articlesService;

    @PostConstruct
    void initialize() {
        URI apiUrl = UriBuilder.fromUri(urlArticlesServiceLocal).build();
        articlesService = RestClientBuilder.newBuilder()
                .baseUri(apiUrl)
                .register(ExceptionMapperArticles.class)
                .build(ArticlesService.class);
    }

    public CompletionStage<List<Article>> getArticlesReactive(int amount) {
        return articlesService.getArticlesFromService(amount);
    }
}
```

Exit the Editor via 'Ctrl-X', 'y' and 'Enter'.

## Step 2: Build the Image

Invoke the following commands to build the image.

```bash
cd ~/cloud-native-starter/reactive/rest-json-quickstart/
oc project cloud-native-starter
mvn package
mv src/main/docker/Dockerfile.jvm ./Dockerfile
oc new-build --name my-web-api-reactive --binary --strategy docker
oc start-build my-web-api-reactive --from-dir=.
```

![deploy-my-web-api](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-dd6f49248d2bbf52fff495e91f0d640b393b7878%2Fdeploy-my-web-api.png?alt=media)

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.

```bash
cd ~/cloud-native-starter/reactive/rest-json-quickstart/
cat Dockerfile
```

Open the OpenShift Web Console and wait for the build to be completed.

![deploy2](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-3431330fab17c94ef76399d19ab6e4c4ba508176%2Fdeploy2.png?alt=media)

The output of the build will be a new image stream.

![deploy3](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-7e8faeba8b199db91628d96508db8e44a67eea09%2Fdeploy3.png?alt=media)

## Step 3: Deploy and test the Image

In the 'Developer' view, click on 'Add' and then 'Container Image'.

![deploy4](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-4b124f5d2745902312adcfac4df9b8063ebee439%2Fdeploy4.png?alt=media)

On the next page choose 'Image from internal registry' and define the project, stream name and tag.

![deploy5](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-d81b860d5ed4d66563222ab02fdbf658f175f4a0%2Fdeploy5.png?alt=media)

Leave all other defaults including 'Create a route' and click 'Create'.

![deploy6](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-99be1b2893138f773270650ad1dea81a2875ec23%2Fdeploy6.png?alt=media)

On the next page click on 'my-web-api-reactive'.

![deploy7](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-06cb4c372d5d1dd230dc67ec98df2948fc9b24c3%2Fdeploy7.png?alt=media)

Wait until the pod is running. After this click on the route at the bottom.

![deploy8](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-187cee4fb5a5951422ecd17c923f0f72cb3a9f14%2Fdeploy8.png?alt=media)

This will display the home page of the generated service. To invoke your endpoint append 'articles' to the URL.

![deploy9](https://230114421-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8FkCR0HY2sWktByd2R%2Fuploads%2Fgit-blob-cd9bffde266847f5c5bb157e9a19aa86753eef4f%2Fdeploy10.png?alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ibm-developer.gitbook.io/reactive-endpoints-with-quarkus-on-openshift/build-a-new-version-of-the-microservice/exercise-04.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
