Pair autoscaled app & db servers together with consul - java

Currently, I have two GCE auto-scaled groups of servers, bootstrapped by the chef. 1st - redis servers (db), 2nd - java servers (app).
Any app server can talk to any db server. Every db need to be served by app server, and there should be no situation when one app server has connections to two separate db servers.
So, I need to figure out if I can connect newly created app-server with newly-created DB-server (all in the same network), using consul.
All in all, I need to automatically pair new up-scaled servers, by adding appropriate db server IP or hostname to the command, that starting java on the app server.
I'm very new to service discovery and stuff, so any help is greatly appreciated.

Answer after edit:
So if I understand you correctly, any new app server can talk to any new redis server, but once they picked a db server to talk to, they should stick with that server.
I can see a few ways to achieve that with consul:
Map each app server 1-to-1 to a redis server and expose each server as a different service in consul with domain names like: app1.service.consul, redis1.service.consul. The drawback here is that you can not scale your app servers independently from your redis servers.
Use Redis Sentinel and let it abstract the sharding of the data for you and the just expose it under one domain name in consul: redis.service.consul
I would suggest to look into the second option, since it allows you to scale your app and db servers independently.
Old answer:
It sounds like you have two services in your network: An app service and a db service. You would then typically make consul serve as the DNS server for both of them.
This can be achieved by creating a service file for each of them.
On the server where your app is running you would create a service file in /usr/local/etc/consul.d/my_app.json:
{
"service": {
"name": "my_app",
"port": 1234
}
}
Where you replace port with the port your app is listening on.
You then need to reload consul with consul reload. You can check that the changes where applied correctly by running consul monitor.
Your app should now be reachable from my_app.service.consul on your internal network.
You can check this by issuing a DNS query with dig my_app.service.consul. This should return the IP address of the app server in the ANSWER section of the DNS response.
You then have to repeat these steps on your database server where you need to create another service file for the database with the appropriate port and service name.

Related

Consul multiple client

I am new at the consul. So, I have multiple client instances running and have a java application.
To connect to the client I have given IP of one of the clients. I believe this wrong. Should I give the load balancer IP tat connect to the client or something else?
Consul operates as a distributed system, and is designed for client agents to serve as the primary access point for applications which need to interface with Consul's DNS or HTTP APIs.
Consul clients should be deployed on every node/server in your environment. Applications which are running on a given server should send HTTP/DNS queries to the Consul agent which is running on the same server. This is typically achieved by configuring the Consul agent to listen on localhost (via the -client/client_addr option), and configuring your applications to connect to Consul over the same address.

How to merge local database server into a localhost service?

I've been recently learning how databases work and how to write a service that can operate the database. So the situation is following:
I have a MySQL database ran on a localhost server (XAMPP). Accessing the database is simple: localhost:3036/my_database.
In addition to that, I wrote a Java Spring service (actually just copied the sample from https://github.com/spring-guides/gs-accessing-data-mysql.git) that is supposed to be an interface accepting curl requests and based on them to operate the database. So far so good. Everything works. The problem is that this service also runs on a localhost address: localhost:8080.
I would like to implement the MySQL server into my Spring service so that I can just run this service and the database will run and be hosted on the same port. The data is supposed to be available only through this service so I don't want an additional independent MySQL server to be running on a different localhost port.
At the end of the day, I would like to generate an executable jar file that when ran provides me the service on a localhost address and deals with locally stored database seamlessly.
Edit:
I wouldn't like my database to be independent form the service. I don't want to take care of running it and connecting to the service. What I'm searching for is sommething like #jr593 mentioned in the answers below, an embedded database. But is it possible for such databases to save the data locally on the device that the service is running beetwen service runs?
There is a few solution for this.
Change port for java app. It it's Spring Boot you could check property server.port and set it to e.g 8081.
You can chcek what is working on XAMPP on port 8080 and shoud down it (it could be apache or something else)
You don't need XAMPP to have MySQL on local host. You can stop/remove xampp and install standalone MySql, so you will take only port 3036 and nat 8080 and 3036.
You could use Docker with MySql image instead of XAMPP.

Add Java API to online server not localhost

I have just created a Hello World RESTful Java API with IntelliJ IDEA and Spring and I make several requests with http://localhost:8080/function and it works fine and return the JSON data well, but now I want to make request from my android app to get the same JSON data from another network. How to create global API or make my local API global or online?
If it is working on your PC, your next step is to host it somewhere which is reachable from the outside world. If it is still in testing mode, probably what you need to do is port-forwarding from your router, so that your requests to your public IP get forwarded to your PC.
In the long term of course you need to host it somewhere, unless you want to leave your PC running. You can buy a cheap hosting to start with on something like Digital Ocean, or go for the more advanced cloud service providers like Google and AWS.
in your router you have to redirect port 8080 to you network IP and allow port 8080 in your firewall
And use this IP address https://whatismyip.com.br/
You can create your java application in a container and deploy it on one of the cloud providers (AWS, Azure, Google App Engine).

OpenShift Java application connection to external Amazon RDS Database

Amazon RDS Databases require that I supply the IP address of any machine that should be permitted to make connections. In my local Apache Tomcat development server, my Java application is able to connect to my Amazon RDS database, and I had to supply my computer's IP address to allow this connection.
Fast forward to deploying my application to OpenShift. My application is deployed successfully and I can get to my log in page. I created a test page on the application to output the OpenShift server IP address on which my application is running. I added that IP address to the security protocol on Amazon RDS just like I did for my local machine. However, the deployed application on Openshift is still not successfully making a connection to my Amazon RDS database.
I'm using the free OpenShift account. I'm wondering if the free account version doesn't permit external database connections? Or, am I not capturing the correct IP address of the OpenShift server where my application is stored?
In general, you can conduct an experiment:
Create a small function in your application - to fetch some URL at another server you have control over, that is you can read its access.log and it accepts connections from any IP.
Then run this function (by accessing your test page, using remote shell, or by scheduling a cron job).
And check the access.log.
So you will determinate IP address (if it is changed by some kind of proxy).
If nothing is logged, then it seems external connections are blocked.
You need to determinate IP address of openstack's Web Proxy.
See about Web Proxy and ports 8000, 8443 at https://developers.openshift.com/en/managing-port-binding-routing.html
If there are many such IPs, you can create SSH tunnel and forward one port, so your connection to the database will be local.

java desktop application - sharing one database host on local server

I am working on java desktop application (swing) which needs to be install/run in three different computers on same Local Network ( LAN ). But this application need to share one database and each application should be able to insert, update, delete records in the same database.
How do i achieved that in java, any suggestion/help would be appreciated ?
Thanks in advance
I came across this while reading some questions. I also created a desktop application which is being accessed by other computers on the same network which includes laptops and desktop PCs in the past.
Step 1:
Choose which of the PCs will be the server. In other words, which among the PCs will be your server. Your chosen PC-server will have a server application(e.g. XAMPP) installed. This PC is where you will import your existing database (.sql file). Other PCs don't have to have XAMPP(or other server applications) installed on them. Other PCs only need to have a copy of the desktop application that you created. No need to set other PCs with server application and sql.
This PC-server that you have chosen will have a connection string with localhost since I assume that all of them are not accessing the internet to use the database.
Example: jdbc:mysql://localhost:3306/yourdatabasename
Step 2:
Grant privileges to the database that was setup on your chosen PC-server
The simplest example is the one below.
grant all privileges on db_name.* to 'username'#'localhost' identified by 'password';
Don't forget to replace with your connections' username and password
But you may also select certain privileges based on your preference.
Step 3:
Set other PCs' connection string that has copy of the desktop application you created.
If PC-server has an IP Address of let's say 192.168.2.3 then, the connection string of other PCs will be
jdbc:mysql://192.168.2.3:3306/yourdatabasename
Other PCs will connect using the PC-Server's IP address since our database and server application was setup on PC-Server.
I hope this helps and others who might see this question.
Peace.
This is a basic approach to this problem:
-Have a client side application and server side
Request access to the database, have the server side application either accept or reject the request.
Once authenticated to the database, have the server side application send the information over to the client application containing the database info
When the client application wants to perform one of the actions you described : update,delete,etc...have it send requests to the server application to complete these.
It works exactly as if the database is local.
THe only main difference is that the url to the database is not in the form 127.0.0.1 (or localhost) but has the ip of the machine where the database is present.
If changing that you have connection problems probably a firewall is blocking your requests. Check if a firewall exists and open the port of the database.
If more applications try to access to data at the same time you need to put autocommit to false and manually commit the data only when the update is finished. Other threads reading or trying to update the same data will wait until the first thread has committed (or rollbacked) the transaction.

Categories

Resources