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
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.
It describes how to start Cludant, connect to Cloud Foundry, and connect to VCAP_SERVICES.
Create a cloudant. If possible, access Dashboard, create a DB, and register the data.
Create a Cloud Foundry. I will rewrite the service later, but I created it with Tomcat for the time being.
Once created, connect Cloudant.
For the time being, IAM will be created automatically.
Follow the instructions to restart the app.
When you access Cloud Foundry and open the Runtime tab, VCAP_SERVEICE is created in the Environment Variables item.
When registering in application.yml or application.properties, delete the JSON line breaks and write it in one line!
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"}]
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
The source code is below. https://github.com/amanoese/cloudant-demo
https://console.bluemix.net/docs/cli/index.html https://github.com/cloudant/java-cloudant
Recommended Posts