Create a Spring Boot web app that uses IBM Cloudant and deploy it to Cloud Foundry

You may want to use IBM Cloudant, but Qiita has a lot of Node Red articles. .. .. There are not many articles for people who want to use it in java, so I will post it as an introduction. Also, I will write until deploying to IBM's Cloud Foundry.

dependency Spring boot 2.0.4 cloudant-client 2.12.0 okhttp3 3.11.0

About cloudant authentication

Cloudant authentication used to have to issue an HMAC key, 2018/08/21 Currently HMAC, VCAP_SERVICES, IAM API key, local authentication or account endpoint? It seems that authentication is possible by various authentications such as.

Any authentication method is fine, but this time we will use VCAP_SERVICES without worrying about the difference between the local environment and the Cloud Foundry environment.

Connection between Cloud Foundry and Cludant, acquisition of VCAP_SERVICE

It describes how to start Cludant, connect to Cloud Foundry, and connect to VCAP_SERVICES.

Create a Cloudant

Create a cloudant. cloudant-0.png cloudant-2.png If possible, access Dashboard, create a DB, and register the data. cloudant-3.png

Create Cloud Foundry and connect Cloudant

Create a Cloud Foundry. I will rewrite the service later, but I created it with Tomcat for the time being. cf-0.png cf-1.png

Once created, connect Cloudant.

cf-2.png cf-3.png

For the time being, IAM will be created automatically. cf-4.png

Follow the instructions to restart the app. cf-5.png cf-6.png

Get VCAP_SERVICES

When you access Cloud Foundry and open the Runtime tab, VCAP_SERVEICE is created in the Environment Variables item. cf-7.png

When registering in application.yml or application.properties, delete the JSON line breaks and write it in one line!

Implementation

Describe VCAP_SERVEICES in one line in the properties file. [1](# cloud-foundry and cludant connection vcap_service acquisition) Also, if you deploy to the Cloud Foundry environment with this setting, the variables of properties will be used locally due to the priority of variables, and the variables of Cloud Foundry will be used in production, so you can develop with a good feeling.

application.properties


VCAP_SERVICES={ YOUR_VCAP_SERVICES_JSON : YOUR_JSON_PROPERTIES }

CloudantConfiguration.java


@Configuration
public class CloudantConfiguration {

    @Value("${VCAP_SERVICES}")
    private String vcapService;

    @Bean
    public CloudantClient cloudantClient()  {
        return ClientBuilder.bluemix(vcapService).build();
    }

}

If you have not created a Dao to receive, it is better to receive it with JsonObject. It works fine because the cloudant library uses the class by default.

CloudantdemoApplication.java


@RestController
@SpringBootApplication
public class CloudantdemoApplication {

	@Autowired
	CloudantClient cloudantClient;

	public static void main(String[] args) {
		SpringApplication.run(CloudantdemoApplication.class, args);
	}

	@RequestMapping("/")
	public String index() {
		Database db = cloudantClient.database("users",false);
		QueryResult<JsonObject> result  = db.query(new QueryBuilder(eq("name","LLENN")).build(), JsonObject.class);
		return result.getDocs().toString();
	}
}

If you run the application with run etc. and throw curl, it will be returned as follows.



```bash
$ curl http://localhost:8080/
[{"_id":"18f6e5d603317e7a8189513270bc6e28","_rev":"1-487469bb37faf2e391ce4f8897b54e75","name":"LLENN"}]

Deploy to Cloud Foundry

Set to output war in pom.xml Build the source code.

$ mvn -DskipTests=true package

Deploy to the created environment. Use the bx command in the IBM Cloud CLI (https://console.bluemix.net/docs/cli/index.html).

#Log in with the bx command.
$ bx login

#Select the cloud foundry environment.
$ bx target --cf

#Get a list of apps.
$ bx cf apps

Name Requested State Instance Memory Disk URL
Cludant-Demo   started          1/1            768M       1G         Cludant-Demo.mybluemix.net

#Deploy your app to Cloud Foundry.
$ bx cf push Cludant-Demo -p ./target/cloudantdemo-0.0.1-SNAPSHOT.war -b https://github.com/cloudfoundry/java-buildpack.git
...
...
Status Start Date CPU Memory Disk Details
#0 run 2018-09-01T00:00:00Z   0.0%   1.1M of 768M   1.3M of 1G   

Cloud Foundry will be started automatically, so try accessing the URL with curl.

$ curl https://Cludant-Demo.mybluemix.net/
[{"_id":"18f6e5d603317e7a8189513270bc6e28","_rev":"1-487469bb37faf2e391ce4f8897b54e75","name":"LLENN"}]

Let's stop it after checking.

$ bx cf stop Cludant-Demo

Remarks

The source code is below. https://github.com/amanoese/cloudant-demo

reference

https://console.bluemix.net/docs/cli/index.html https://github.com/cloudant/java-cloudant

Recommended Posts

Create a Spring Boot web app that uses IBM Cloudant and deploy it to Cloud Foundry
Inexperienced create a weather app using OpenWeatherMap and deploy it to Netlify
Create a portfolio app using Java and Spring Boot
Create a web app that is just right for learning [Spring Boot + Thymeleaf + PostgreSQL]
Steps to create a simple camel app using Apache Camel Spring Boot starters
Create a simple search app with Spring Boot
Create a web api server with spring boot
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)
How to create a Spring Boot project in IntelliJ
A solution that makes it easy to input Procon and Web tests to verify results
[Java] Deploy the Spring Boot application to Azure App Service
Create a Hello World web app with Spring framework + Jetty
How to deploy an app that references a local jar to heroku
Image Spring Boot app using jib-maven-plugin and start it with Docker
Create an app with Spring Boot 2
Create an app with Spring Boot
Try to create a server-client app
Create a Spring Boot project in intellij and exit immediately after launching
Create a Spring Boot app development project with the cURL + tar command
A shell script that builds a Docker image and pushes it to ECR
Java beginner tried to make a simple web application using Spring Boot
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
Deploy the WEB application by Spring Boot to Tomcat server as WAR
Create a program to post to Slack with GO and make it a container
How to create a server executable JAR and WAR with Spring gradle
How to create a convenient method that utilizes generics and functional interfaces
When internationalizing is supported by Spring Boot, a specific locale is not translated and I am addicted to it
When I defined a session scope bean in Spring Boot, it behaved strangely and needed to be adjusted.
Deploy a Java web app on Heroku
Add spring boot and gradle to eclipse
Sign in to a Spring Boot web application on the Microsoft ID platform
Find a value that is convenient to have a method and make it a ValueObject
[Spring sample code included] How to create a form and how to get multiple records
[Spring Boot] If you use Spring Boot, it was convenient to use a lot of util.
I tried to clone a web application full of bugs with Spring Boot
How to deploy VS Code Remote Containers in a docker-compose project that includes both the API and the front app