Go back to Read free chapters

Download Connecting API

Download Connecting application microservice component and run it as Docker container with MySQL database

In the end of this chapter you can download the Connecting API Java source code, connect it to MySQL database, build and run it as Docker container.

The Connecting API microservice is intended to work as a Beta Test version as a backend service. You can call the Connecting API inside a secured application server where your application calls the REST service.

The Connecting API works similarly as the Messaging API except that the database client has been changed to use MySQL or MariaDB database server to create the database tables. This means that there is a persistent database where the data is written to the filesystem, opposed to the H2 database that is stored only in memory.

So to run the Connecting API you need the following

  • Install MySQL or MariaDB database

  • Create new database for your application and create application user inside the database

  • Update the Connecting API application.yml properties file with your database application user

Install MySQL or MariaDB database

When you are doing local development one of the best ways is to install a new database with Docker. You can follow these instructions from the official MariaDB site

https://mariadb.com/kb/en/mariadb/installing-and-using-mariadb-via-docker/

How to create a new database for the Connecting application

Copy this file to your home directory location from the Connecting API source code

connecting\src\main\resources\create-database.sql

Open the file and update your database user name and password to the file.

CREATE DATABASE IF NOT EXISTS socialnetworkingpatterns CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

GRANT ALL PRIVILEGES ON socialnetworkingpatterns.connectable TO '[USERNAME]'@'[LOCALHOST_IP]' IDENTIFIED BY '[PASSWORD]';

GRANT ALL PRIVILEGES ON socialnetworkingpatterns.connection  TO '[USERNAME]'@'[LOCALHOST_IP]' IDENTIFIED BY '[PASSWORD]';

FLUSH PRIVILEGES;

- Update new username for the application here [USERNAME]

- Update your Connecting Application host IP address here [LOCALHOST_IP]

For example if you run it inside a Docker container update it with your Docker IP address. In MySQL you can use % to mark that the access is allowed from all hosts, but do not use this unless you have bloced the database connection in your host. 

- Update the application username's password here [PASSWORD]

Below is an example of the settings.

CREATE DATABASE IF NOT EXISTS socialnetworkingpatterns CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

GRANT ALL PRIVILEGES ON socialnetworkingpatterns.connectable TO 'testuser'@'162.0.0.0' IDENTIFIED BY 'testpass12345';

GRANT ALL PRIVILEGES ON socialnetworkingpatterns.connection  TO 'testuser'@'162.0.0.0' IDENTIFIED BY 'testpass12345';

FLUSH PRIVILEGES;

Then run the create-database.sql with the following command for example in Linux systems. This means that the MySQL client runs the commands that are locate in the file create-database.sql that is located in your home folder in Linux the ~ directory. The command uses MySQL root user and asks you to input the root user password to run the command.

mysql -u root -p < ~/create-database.sql

And delete the temporary file create-database.sql from your local directory so the password will not be stored anywhere locally. Always keep your credentials in a safe place other than your client so that only an administrator can access them. Preferrably in your memory. 

Update the database connection properties to the Connecting application

After that you have created the database open up the application properties file application.yml in the folder

connecting\src\main\resources\application.yml

And update the following properties

- Update the location where you installed the database to the DATABASE_IP and DATABASE_PORT. If you installed as a Docker container update the Docker IP address here. You can see the docker IP address with the command docker-machine ls . The database port is the port number that you set in the docker run command, by default in MySQL it is 3306.

- Update the USERNAME and PASSWORD to the ones from the previous section where you created the database username and password

Here are the original application properties to override

spring.datasource.driverclassName: "org.mariadb.jdbc.Driver"

spring.datasource.url: "jdbc:mysql://[DATABASE_IP]:[DATABASE_PORT]/socialnetworkingpatterns"

spring.datasource.username: "[USERNAME]"

spring.datasource.password: "[PASSWORD]"

Here is an example of the overridden properties.

spring.datasource.driverclassName: "org.mariadb.jdbc.Driver"

spring.datasource.url: "jdbc:mysql://162.0.0.0:3306/socialnetworkingpatterns"

spring.datasource.username: "testuser"

spring.datasource.password: "testpass12345"

There's other properties in the property file as well and you can examine these. Spring Boot has default properties that these settings override and you can see all the Spring Boot default properties from this list. 

Spring Boot Common application properties

Build the Spring Boot application with Maven

You can run the Connecting API with either Docker compose that we will show in the following chapter or by hand by launching the application as a Java process shown below.

mvn spring-boot:run

After that test the Connecting application by compiling and running the application inside a Docker container with maven command.

mvn clean package docker:build -Pdocker

Run the Connecting API Docker container with Database access that creates the database tables

In this chapter we will pass the database location and access credentials using environment variables. Here is an example command to launch the Connecting API Docker container. From the command replace the fields as shown below.

docker run -p 5001:5001 --name=connecting -t weallcom/connecting -e "SPRING_DATASOURCE_URL=jdbc:mysql://[DATABASE_IP]:[DATABASE_PORT]/socialnetworkingpatterns" -e "SPRING_DATASOURCE_USERNAME=[USERNAME]" -e "SPRING_DATASOURCE_PASSWORD=[DB_PASSWORD]" -e "SPRING_JPA_HIBERNATE_DLL_AUTO=create" -e "SPRING_PROFILES_ACTIVE=docker"

Replace the followig parameters from the precending command.

- Update [DATABASE_IP] to your installed database IP address, Docker IP address if you use one.

- Update [DATABASE_PORT] to the portnumber where you are running MySQL, the default port is 3306.

- Update [USERNAME] to the username that you created for the Connecting API. 

- Update [DB_PASSWORD] to the password that you created for the username. 

Eureka Service registry and Zuul discovery client

This version of the Connecting API is configured to use Zuul service discovery automatically so you will see this error when the application starts. This is because the application tries to contact a Zuul discovery service that is not yet installed. You can turn off this setting by commenting the annotation @EnableDiscoveryClient in the ConnectingApplication class.

The in next chapters we will discuss about Eureka service registry and Zuul service discovery and how to connect an application to a registry and make it discoverable.

Start creating connections with the application

After that you can start connecting connectables together with the Connecting API. Here's an example use case for the current version. 

curl --data "ownerName=@john" [DOCKER_IP]:5001/v0.1/connections/@mary

curl --data "ownerName=@mary" [DOCKER_IP]:5001/v0.1/connections/@john

curl [DOCKER_IP]:5001/v0.1/connections?ownerName=@mary

curl -X "DELETE" [DOCKER_IP]:5001/v0.1/connections/@john?ownerName=@mary

curl [DOCKER_IP]:5001/v0.1/connections?ownerName=@mary

Below is the Download section for the Connection API Docker microservice. Please login to access the Download section. 

Below you can find the link to download the full Connecting API Spring Boot microservice source code. 

The link is downloadable only when you have enough share points.

LOGIN TO DOWNLOAD

Here is also a fast and easy 1 2 3 to setup install the service. Note that you have to change the username and password to your own.

The Requirement is that you have Docker installed in your host. If you have an older machine use Docker Toolbox from 

LOGIN TO DOWNLOAD

See what your Docker machine IP address is from, we are going to use this as the parameters below. For example the Docker IP address is 192.168.99.100

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.2

Install a local database according to these instructions

LOGIN TO DOWNLOAD

Download the Connecting API, unzip the file, open Docker terminal and change directory to unzipped folder. 

Copy this to file connecting\src\main\resources\create-database.sql

CREATE DATABASE IF NOT EXISTS socialnetworkingpatterns CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

GRANT ALL PRIVILEGES ON socialnetworkingpatterns.connectable TO 'testuser'@'192.168.99.100' IDENTIFIED BY 'testpass12345';

GRANT ALL PRIVILEGES ON socialnetworkingpatterns.connection  TO 'testuser'@'192.168.99.100' IDENTIFIED BY 'testpass12345';

FLUSH PRIVILEGES;

Paste the database creation after you login to the database installed with Docker. 

Login to the database.

docker exec -i -t localtestdb mysql -u root -p

Enter your database password. Then paste the script to the terminal and run it. Verify that the database was created by running the command

show databases;

An example log for the commands:

MariaDB [(none)]> show databases;
+--------------------------+
| Database                 |
+--------------------------+
| information_schema       |
| mysql                    |
| performance_schema       |
| socialnetworkingpatterns |
+--------------------------+
4 rows in set (0.00 sec)

Replace these rows in file connecting\src\main\resources\application.yml

spring.datasource.driverclassName: "org.mariadb.jdbc.Driver"

spring.datasource.url: "jdbc:mysql://192.168.99.100:3306/socialnetworkingpatterns"

spring.datasource.username: "testuser"

spring.datasource.password: "testpass12345"

Run command to build a new Docker container with the Connecting API

mvn clean package docker:build -Pdocker -Dmaven.test.skip=true

Run the fresh Docker container in a port 5001. Verify that the database connection IP address and port are correct below, and username and password also.

docker run -p 5001:5001 --name=connecting -t weallcom/connecting -e "SPRING_DATASOURCE_URL=jdbc:mysql://192.168.99.100:3306/socialnetworkingpatterns" -e "SPRING_DATASOURCE_USERNAME=testuser" -e "SPRING_DATASOURCE_PASSWORD=testpass12345" -e "SPRING_JPA_HIBERNATE_DLL_AUTO=create" -e "SPRING_PROFILES_ACTIVE=docker"

There is going to be an Exception because the application tries to connect to Eureka, but you have not yet installed it. The Exception looks like this. The service itself is working correctly.

2017-04-21 09:09:14.341  WARN 1 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server        at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute ...

Run the following Curl commands in the Docker terminal to verify the installation

#Add connection from @john to @mary

curl --data "ownerName=@john" 192.168.99.100:5001/v0.1/connections/@mary

# Verify the connection from @mary to @john

curl --data "ownerName=@mary" 192.168.99.100:5001/v0.1/connections/@john

# Fetch connections from @mary

curl 192.168.99.100:5001/v0.1/connections?ownerName=@mary

# Discard the connection from @mary to @john

curl -X "DELETE" 192.168.99.100:5001/v0.1/connections/@john?ownerName=@mary

# Fetch connections from @mary, connection to @john is discarded

curl 192.168.99.100:5001/v0.1/connections?ownerName=@mary

From the first Curl command you should see the JSON response that tells the state of the connection, as seen below

$ curl --data "ownerName=@john" 192.168.99.100:5001/v0.1/connections/@mary

{"created":1492766233467,"updated":1492766233467,"applicationId":0,"owner":{"created":1492766233311,"updated":1492766233311,"applicationId":0,"name":"@john","type":"USER","new":false,"saved":true},"ownerType":"USER","endpoint":{"created":1492766233380,"updated":1492766233380,"applicationId":0,"name":"@mary","type":"USER","new":false,"saved":true},"endpointType":"USER","state":"PENDING","new":false,"saved":true}


What new ideas or thoughts this chapter gave you?