Binding Java application to MySQL in Cloud Foundry - java

I'm trying to bind MySQL service, which is in on prem cloud foundry, to my Java application. could anyone suggest me what are the necessary changes to be made in my code to access MySQL database.

If you are using Spring Boot, this should happen almost automatically as described in a Spring blog post.
If you are using Spring without Spring Boot, you may want to use Spring Cloud Connectors.
If you are using neither, you can interrogate the VCAP_SERVICES environment variable for details of your service.

On cloud foundry, you will have to provision a service for your app.
Look for available services.
cf marketplace
Identify the mysql service and the plan. Create the service instance
cf create-service p-mysql 512mb mysql
To see the service instances
cf services
Bind the service instance to your app
cf bind-service myapp mysql
You can see the details as
cf env myapp
EngineerBetter has already provided you articles on how to use Spring to leverage the service in your app code.
Here's a good quick reference for CF CLI

Related

How can a springboot application deployed on PCF read messages from Websphere MQ

I'm developing a spring boot application that will be deployed in pivotal cloud foundry and it needs to read/listen to messages from Websphere MQ. Is there a way to do that?
Yes you can! Follow this tutorial to get up and running - https://developer.ibm.com/languages/spring/tutorials/mq-jms-application-development-with-spring-boot/
Change the hard-coded queue to pick up the queue name from application.properties eg.
#JmsListener(destination = "${queue.name}")
public void receive(MyDataObject data) {
...
}
where application.properties would amongst all the other required settings, something like:
queue.name=DEV.QUEUE.2
See https://github.com/ibm-messaging/mq-jms-spring for a list of the properties that you can set. The rest is just spring JMS. For example, if you want Spring to perform object marshelling / unmarshelling make sure to add a MessageConverter which is usually a MappingJackson2MessageConverter
There are guides to help you get your spring app deployed to cloud foundry:
From the CF perspective - https://docs.cloudfoundry.org/buildpacks/java/getting-started-deploying-apps/gsg-spring.html
and from the Spring perspective - https://spring.io/guides/gs/sts-cloud-foundry-deployment/
I'm developing a spring boot application that will be deployed in pivotal cloud foundry and it needs to read/listen to messages from Websphere MQ. Is there a way to do that?
Yes, it should be possible. What you need to first validate is that you have network connectivity (DNS resolves, traffic is routable and no firewalls block access) from traffic on your CF foundation to your IBM MQ.
If any of those do not work, you need to talk with your network or possibly MQ administrators to get traffic allowed.
You can validate by running cf ssh to enter a container on your CF foundation and then using nc -v <IP> <port> which will attempt to make a TCP connection to your MQ IP/port. If you see netcat indicate that a connection was established, then you're all set.
Beyond that, you would just develop the Spring Boot application like you would if you were deploying a Spring Boot application anywhere else (i.e. there nothing specific you must do). You could put your creds into application.properties or use env variables to pass in the credentials to your app.
If you want to get a little fancier, you can look at java-cfenv which can be used to perform dynamic service binding, but you don't have to.
With service binding, you could create a user-provided service (cf cups) and use that to store your IBM MQ connection info/creds. Then you cf bind the service to your app, and you app will have access to the bound credentials by reading the VCAP_SERVICES environment variable, which java-cfenv will do automatically for you.
The benefit of using service binding instead of putting your credentials into application.properties or env variables is that you can cf unbind / cf bind and move services around very easily. All you need to do is restart your app, and it'll start using the new service.

Spring Cloud Config Server in AWS Lambda

I have seen few examples of running spring boot application in AWS Lambda. Is there a way to run Spring Cloud Config Server in AWS Lambda?
It probably doesn't make much sense if you are using config server backed by git or svn since it uses local disk for state in those cases. If you are using jdbc backend, it could work, though I don't know if anyone has tried.

Spring Cloud Config Server with Zookeeper or HashiCorp Vault Backend

My question relates to using either Zookeeper or Hashicorp's Vault as a back-end data store to Spring's Cloud Config Server.
We're currently running a number of Spring Boot micro-services that rely on a Spring Config Server to serve each service's configuration. This works well and we have no issues with it.
Initially, config server ran on the native profile and had the config files embedded in the application. This doesn't work as each time we make a configuration change to any of the applications we needed to redeploy config-server.
Using GIT is obviously more robust and we were in the process of switching to a standalone GIT backend when we were asked to look into using Zookeeper or Vault instead.
Which brings me the question:- is it at all possible to use Vault/Zookeeper as the back-end data store for Config Server without needing each application to talk to Vault/Zookeeper directly?
Thanks
Yes, it's possible to use a different backend (like Vault or SVN, called EnvironmentRepository) in Spring Cloud Config without touching your clients.
See the reference docs on more details.
To update this:
We switched out the Zookeeper backend for Consul instead as we were able to use SSL for the connection between Vault and Consul. This isn't currently available when using Zookeeper as the storage backend.
We now have a working configuration stack comprising of Consul, Vault and Spring Cloud Config Server with SSL enabled between all three. Additionally, Consul and Vault are both running in a clustered mode with replication between all nodes in the cluster.
Working well thus far.

How to deploy java spring restful service on AWS based on tomcat server with mysql database?

I have restful spring web service running on tomcat server and have mysql database on backend. I deployed war file of my service using aws elastic beanstalk free account yet but I am unable to setup mysql database. Can anyone guide me on this matter ? Secondly, our application has android side code which will call my REST API so is there any other way to do this instead of setting up amazon web services for testing purposes ?
You are going the right way; Android should call REST APIs. These APIs can be implemented in following ways:
As you suggested; use spring on tomcat to expose such apis.
Use API Gateway exposed by AWS. This api gateway can even call lambda expressions in backend which can be written in Java/Python etc.
I will suggest you to go ahead with 1 as u are already aware of spring/tomcat etc. and MOST companies use this only.
For MySQL database you have following options:
Install MySQL on your local EC2 server ( where you have tomcat running ); or on another EC2 server.
You can use MySQL as a service which is RDS. It is expensive but easy to configure.

JMX - Pivotal Cloud Foundry

I am having a java app which I am planning to migrate to Pivotal Cloud Foundry. The application uses JMX to change of the properties at runtime. Is it possible to retain the same architecture when I migrate the app to PCF or should I explore a different approach?
are you using Spring Boot in your Java app? If so, you can use JMX features with Actuator. Jolokia helps you to do this via JMX over HTTP.
Please refer: Spring Boot JMX Management
If this is a traditional Java App, you have pushed into PCF, you can use Java build pack features to enable JMX.
Please refer: Enable JMX port via Java Build Pack
Please try and let us know how it goes.
For a PCF app, the cloud environment should provide dependencies needed for your app. You can inject these dependencies for runtime in various ways, for instance, provide environment settings.
If you need say credentials at runtime, you can look at Spring Cloud Services, and the Config server. If you are looking for other services, you can use Service registry and discovery (based on Netflix Eureka component) within Spring Cloud Services.
It all depends on your use case.
Can you elaborate more on "change properties at runtime"?

Categories

Resources