|
| 1 | +## MyToDoReact version 1.0. |
| 2 | +Copyright (c) 2021 Oracle, Inc. |
| 3 | + |
| 4 | +Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 5 | + |
| 6 | +# Part II -- Backend (Java/Helidon) |
| 7 | + |
| 8 | +## **Summary** |
| 9 | + |
| 10 | +In this Part, you will build and deploy the Helidon Java backend Docker image to OKE, then configure the API Gateway |
| 11 | + |
| 12 | +As with most React applications (https://reactjs.org/), this application uses remote APIs to handle data persistence. The backend implements 5 REST APIs including: |
| 13 | +- 1) retrieving the current list of todo items |
| 14 | +- 2) Adding a new todo item |
| 15 | +- 3) finding a todo item by its id |
| 16 | +- 4) updating an existing todo item |
| 17 | +- 5) deleting a todo item. |
| 18 | + |
| 19 | +The APIs are documented using Swagger @ http://130.61.67.158:8780/swagger-ui/#/ |
| 20 | + |
| 21 | +The backend is implemented using the following Java classes (under ./backend/src/...): |
| 22 | +- Main.java: starts and configure the main entry points. |
| 23 | +- ToDoItem.java: maps a Todo Item instance to/from JSON document |
| 24 | +- ToDoItemStorage.java: stores the Todo item in a persistent store i.e., the Oracle Autonomous database |
| 25 | +- ToDoListAppService.java: implements the Helidon service and exposes the REST APIs |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +## **Objectives** |
| 30 | + |
| 31 | +* Set values for environment variables |
| 32 | +* Build and deploy the Docker image of the application |
| 33 | +* Deploy the image on the Oracle Kubernetes Engine (OKE) |
| 34 | +* Describe the steps for Undeploying |
| 35 | +* Configure the API Gateway |
| 36 | +* Test the backend application |
| 37 | + |
| 38 | + |
| 39 | +## **STEP 1**: Set values for workshop environment variables |
| 40 | + |
| 41 | +1. Set the root directory of the workshop |
| 42 | +``` |
| 43 | +<copy>export MTDRWORKSHOP_LOCATION=~/mtdrworkshop</copy> |
| 44 | +``` |
| 45 | +2. Run source addAndSourcePropertiesInBashrc.sh |
| 46 | + |
| 47 | +The following command will set the values of environment variables in mtdrworkshop.properties and source ~/.bashrc |
| 48 | + |
| 49 | + ``` |
| 50 | + <copy>cd $MTDRWORKSHOP_LOCATION; source addAndSourcePropertiesInBashrc.sh |
| 51 | + </copy> |
| 52 | +``` |
| 53 | + |
| 54 | +## **STEP 2**: Build and push the Docker images to the OCI Registry |
| 55 | + |
| 56 | +1. Ensure that the "DOCKER_REGISTRY" variable is set |
| 57 | + |
| 58 | + Example: <region-key>.ocir.io/<object-storage-namespace>/<firstname.lastname>/<repo-name>" |
| 59 | + If the variable is not set or is an empty string, the push will fail (but the docker image will be built). |
| 60 | + |
| 61 | +2. Make sure to be in backend/target/classes/wallet directory then execute |
| 62 | + ``` |
| 63 | + <copy>unzip ~/mtdrworkshop/setup-dev-environment/wallet.zip</copy> |
| 64 | + ``` |
| 65 | + |
| 66 | +3. Pick mtdrb_tp service alias (see the list of aliases in |
| 67 | + ./backend/target/classes/wallet/tnsnames.ora) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +4. Edit ./backend/target/classes/application.yaml to set the database service and user password |
| 72 | + |
| 73 | + |
| 74 | +5. Copy the edited ./backend/target/classes/application.yaml to backend./src/main/resources/application.yaml |
| 75 | + |
| 76 | +6. Edit ./backend/src/main/java/com/oracle/todoapp/Main.java |
| 77 | + - Locate the following code fragment |
| 78 | +  |
| 79 | + - Replace `eu-frankfurt-1` in `"https://objectstorage.eu-frankfurt-1.oraclecloud.com"` by your region |
| 80 | + |
| 81 | + - Save the file |
| 82 | + |
| 83 | +7. Run the `build.sh` script to build and push the |
| 84 | + microservices images into the repository |
| 85 | + |
| 86 | + ``` |
| 87 | + <copy>cd $MTDRWORKSHOP_LOCATION/backend; ./build.sh</copy> |
| 88 | + ``` |
| 89 | + In a couple of minutes, you should have successfully built and pushed the images into the OCIR repository. |
| 90 | +
|
| 91 | +8. Check your container registry from the root compartment |
| 92 | + - Go to the Console, click the hamburger menu in the top-left corner and open |
| 93 | + **Developer Services > Container Registry**. |
| 94 | +
|
| 95 | +  |
| 96 | +
|
| 97 | +9. Mark Access as Public (if Private) (**Actions** > **Change to Public**): |
| 98 | +
|
| 99 | +  |
| 100 | +
|
| 101 | +## **STEP 3**: Deploy on Kubernetes and Check the Status |
| 102 | +
|
| 103 | +1. Run the `deploy.sh` script |
| 104 | +
|
| 105 | +``` |
| 106 | + <copy>cd $MTDRWORKSHOP_LOCATION/backend; ./deploy.sh</copy> |
| 107 | +``` |
| 108 | +
|
| 109 | +--> service/todolistapp-helidon-se-service created |
| 110 | +--> deployment.apps/todolistapp-helidon-se-deployment created |
| 111 | +
|
| 112 | +2. Check the status using the following commands |
| 113 | +$ kubectl get services |
| 114 | +
|
| 115 | +The following command returns the Kubernetes service of MyToDo application with a load balancer exposed through an external API |
| 116 | +``` |
| 117 | + <copy>kubectl get services</copy> |
| 118 | +``` |
| 119 | +
|
| 120 | + |
| 121 | +
|
| 122 | +4. $ kubectl get pods |
| 123 | +``` |
| 124 | + <copy>kubectl get pods</copy> |
| 125 | +``` |
| 126 | +
|
| 127 | + |
| 128 | +
|
| 129 | +5. Continuously tailing the log of one of the pods |
| 130 | +
|
| 131 | +$ kubectl logs -f <pod name> |
| 132 | + Examaple kubectl lgs -f todolistapp-helidon-se-deployment-7fd6dcb778-c9dbv |
| 133 | +
|
| 134 | + Returns: |
| 135 | + http://130.61.66.27/todolist |
| 136 | +
|
| 137 | +## **STEP 4**: UnDeploy (optional) |
| 138 | +
|
| 139 | + If you make changes to the image, you need to delete the service and the pods by running undeploy.sh then redo Steps 2 & 3. |
| 140 | +
|
| 141 | + 1. Run the `undeploy.sh` script |
| 142 | + ``` |
| 143 | + <copy>cd $MTDRWORKSHOP_LOCATION/backend; ./undeploy.sh</copy> |
| 144 | + ``` |
| 145 | + 2. Rebuild the image + Deploy + (Re)Configure the API Gateway |
| 146 | +
|
| 147 | +
|
| 148 | +## **STEP 5**: Configure the API Gateway |
| 149 | +
|
| 150 | +The API Gateway protects any RESTful service running on Container Engine for Kubernetes, Compute, or other endpoints through policy enforcement, metrics and logging. |
| 151 | +Rather than exposing the Helidon service directly, we will use the API Gateway to define cross-origin resource sharing (CORS). |
| 152 | +
|
| 153 | +1. From the hamburger menu navigate **Developer Services** > **API Management > Create Gateway** |
| 154 | +  |
| 155 | +
|
| 156 | +2. Configure the basic info: name, compartment, VCN and Subnet |
| 157 | + - VCN: pick on of the vitual circuit network |
| 158 | + - Subnet pick the public subnet |
| 159 | + The click "Create" |
| 160 | +  |
| 161 | +
|
| 162 | +3. Click on Todolist gateway |
| 163 | +  |
| 164 | +
|
| 165 | +4. Click on Deployments |
| 166 | +  |
| 167 | +
|
| 168 | +5. Create a todolist deployment |
| 169 | +  |
| 170 | +
|
| 171 | +
|
| 172 | +6. Configure Cross-origin resource sharing (CORS) policies |
| 173 | + CORS is a security mechanism that will prevent running application loaded from origin A from using resources from another origin B. |
| 174 | + Allowed Origins: is the list of all servers (origins) that are allowed to access the API deployment typically your Kubernetes cluster IP. |
| 175 | + Allowed methods: GET, PUT, DELETE, POST, OPTIONS are all needed. |
| 176 | +  |
| 177 | +
|
| 178 | + 7. Configure the Headers |
| 179 | +  |
| 180 | +
|
| 181 | + 8. Configure the routes: we will define two routes: |
| 182 | + - /todolist for the first two APIs: GET, POST and OPTIONS |
| 183 | +  |
| 184 | +
|
| 185 | + - /todolist/{id} for the remaining three APIs: (GET, PUT and DELETE) |
| 186 | +  |
| 187 | +
|
| 188 | +
|
| 189 | +## **STEP 6**: Testing the backend application through the API Gateway |
| 190 | +
|
| 191 | +1. Navigate to the newly create Gateway Deployment Detail an copy the endpoint |
| 192 | +  |
| 193 | +
|
| 194 | +2. Testing through the API Gateway endpoint |
| 195 | + postfix the gateway endpoint with "/todolist" as shown in the image below |
| 196 | +  |
| 197 | +
|
| 198 | + It should display the Todo Item(s) in the TodoItem table. At least the row you have created in Part I. |
| 199 | +
|
| 200 | +Congratulations, you have completed Part II; you may now proceed to the final part (i.e., Part III). |
| 201 | +
|
| 202 | +## Acknowledgements |
| 203 | +* **Workshop by** - Kuassi Mensah, Dir. Product Management, Java Database Access |
| 204 | +* **Application by** - Jean de Lavarene, Sr. Director of Development, JDBC/UCP |
| 205 | +* **Original scripts by** - Paul Parkinson, Developer Evangelist, Microservices |
| 206 | +
|
| 207 | +## Need Help? |
| 208 | +Please submit feedback or ask for help using this [LiveLabs Support Forum](https://community.oracle.com/tech/developers/categories/building-microservices-with-oracle-converged-database). Please login using your Oracle Sign On and click the **Ask A Question** button to the left. You can include screenshots and attach files. Communicate directly with the authors and support contacts. Include the *lab* and *step* in your request. |
0 commit comments