Try WSO2 Cloud for Free
Sign in

All docs This doc
Skip to end of metadata
Go to start of metadata

You can use WSO2 Integration Cloud to host the backend applications for the APIs that you create in WSO2 API Cloud. WSO2 API Cloud is a platform that enables you to expose business capabilities as APIs and manage their lifecycles, from creation to publication and retirement.

In this tutorial, you create a sample Java Microservices application that works as the backend implementation of an API that you create in WSO2 API Cloud.

The sample application allows you to insert any buzzword of your choice to a database that you create in the Integration Cloud. If the same buzzword is added several times, its popularity increases. You then write an API that uses this application in the backend. You can invoke the API to insert buzzwords to the database and retrieve buzzwords in different ways such as all at once, the 10 most popular buzzwords, or the ones that are similar to a word that you give.

Let's get started!

Creating the database

Let's create a MySQL database in the Integration Cloud to insert our buzzwords.

  1. Log in to WSO2 Integration Cloud by going to https://wso2.com/cloud/, clicking the Sign In link and then selecting WSO2 Integration Cloud.

  2. The Integration Cloud opens. Click CREATE DATABASE.

  3. Enter the following information in the form that opens, scroll down and click Create Database. 

    • Database Name: TestDB
    • Default User Name: user1
    • Give a suitable password 

  4. The database that you just created appears in the console. Copy the database name, URL and username. 

    Tip: You see additional characters in the database name and user name because the Cloud adds them to uniquely identify the tenant of the database and the user.

  5. In the command-line or the terminal, run the following command to log in to the MySQL database that you just created. Note that some MySQL clients provide GUIs to connect to the databases.

    mysql -h <mysql_hostname> -u <username> -p<password>

    For example:

    mysql -h mysql.storage.cloud.wso2.com -u user1_U5HcQFOR -p<the password>
  6. Run the following command to create a table by the name Buzzwords in the database.

    USE <your database name>;
    Example: USE TestDB_companyn;
     
    CREATE TABLE Buzzwords (Word VARCHAR(255) NOT NULL, Popularity INT,  PRIMARY KEY (Word));
  7. Run the following command to insert some test data to the table:

    INSERT INTO Buzzwords
    VALUES ('C#',1),
           ('Java',1),
           ('Perl',1),
           ('Python',1);

You now have a database created.

Creating the backend application

Let's create an application in the Integration Cloud that connects to the database that you created earlier.

  1. Sign in to WSO2 Cloud and go to the Integration Cloud tab. Then, click Java Microservices.
  2. Select the Local File System option and click Continue.

  3. Enter the following details in the form, scroll down and click Create.

    • Application Name: API Producer
    • JAR file: Download this sample Java Microservices application and upload it.
    • Under Advanced Settings...

      FieldValue
      Runtime

      OracleJDK 8 + WSO2 MSF4J 1.0.0 - Deprecated

      This sample was written for WSO2 MSF4J version 1.0.0.

      Tip: Be sure to always select the same deployment environment that you used to develop the sample. If not, the sample might not work properly.

      Environment Variables

      Scroll down to the Environment Variables section and add the following to read and write data from/to the database that you created earlier:

      KeyValue
      DB_URLType " database: " -> select the database -> select  URL  and click  Add.
      DB_USERNAMEType " database: " -> select the database -> select  Users -> Select the username and click Add.
      DB_PASSWORD <Give your database password>

  4. Once the application is created, the application's homepage opens. Copy the application's URL.
     

You have now created an application that connects to the database in the Integration Cloud.

Creating an API to consume the backend application

Let's create an API in WSO2 API Cloud with the backend application as its endpoint. This way, users who subscribe to your API after it is published can insert or retrieve buzzwords without writing the application code themselves.

  1. Log in to WSO2 API Cloud by going to https://wso2.com/cloud/, clicking the Sign In link and then selecting WSO2 API Cloud.
  2. Click ADD NEW API.
     
  3. Select the Design new REST API option and then click Start Creating.
  4. Add the following information under the Design tab, scroll down and click Next: Implement to go to the next tab:

    FieldValue
    Namebuzzwordapi
    Context/buzzwordservice
    Version1.0.0
    API Definition

    Add two API definitions as follows:

    • URL pattern: all and HTTP method: GET
    • URL pattern: mostPopular and HTTP method: GET

  5. The Implement tab opens. Click the Managed API section to expand it, enter the following information, scroll down, and click Next: Manage to go to the next tab.

    FieldValue
    Endpoint TypeHTTP/REST Endpoint
    Production Endpoint<DEPLOYMENT_URL>/buzzword
    You got the < DEPLOYMENT_URL > in step 4 of the previous section. In this example, the production endpoint is https://companyn-api-producer-1-0-0.wso2apps.com/buzzword.

  6. The Manage tab opens. Enter the following information, scroll down, and click Save and Publish.

    FieldValue
    Make this the Default VersionCheck
    TransportsHTTP/HTTPS
    Subscription TiersGold

  7. Choose to go the API Store when prompted.
  8. The API opens in the API Store. 

Next, let's invoke the API that you just created.

Invoking the API

WSO2 API Store has an integrated Swagger console called the API Console, which allows you to invoke the APIs that are published in the API Store through a simplified graphical interface. Swagger also facilitates simple, interactive API documentation.

Before you invoke an API, you must subscribe to it using an application and a throttling tier. An application is a logical collection of APIs. It is primarily used to decouple the consumer from the APIs by allowing you to generate a single key for multiple APIs and subscribe to a single API  multiple times with different SLA levels.

throttling tier allows you to limit the number of successful hits to an API during a given period of time, typically to protect your APIs from security attacks, to regulate traffic according to infrastructure availability, and for monetization.

  1. In the previous step, you opened the newly created API in the API Store. From the API's subscription options that open on the right-hand side of the screen, select DefaultApplication, the Gold tier, and click Subscribe.
  2. Click View Subscriptions when prompted.
  3. Go to the Production Keys tab and click Generate Keys to create an access token.

    You need an 
    access token  to be authenticated to invoke the API. An access token is a simple string that is passed as an HTTP header of a request to authenticate API users and applications. Access tokens expire after 1 hour by default. You can either extend the expiration period before it expires or click Regenerate  to renew an access token.
     

    Next, let's invoke the API.

  4. Click the APIs link in the API Store and then click the API you want to invoke.
  5. Go to the API Console tab of the API, expand one of the methods (e.g., /all), and click Try it out.
  6. Note the results that appear in the API Console.

You have now invoked the backend application in the Integration Cloud using the API that you created in the API Cloud.

What's next

See Consume the APIs in WSO2 API Cloud from your Applications or see Integration Solutions.

  • No labels