1

I had a tough time trying to deploy my Java project to Google cloud engine. I read most of Google cloud documentation but I still can't figure it out.

What I want is just tcp communication. I don't need the HTML itself to do something. A lot of guides talk about servlets and http get and post but I need just tcp. Maybe I lack of information and that is why I can't manage it.

So first - do I need some sort of http sever to run just tcp requests? And if not how can I deploy my project?

Right now my project has just Java. I used IntelliJ if it matters. It is something like that.

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MainServer {

    public static void main(String[] args) {
        final int SERVER_PORT = 3000;
        final int SERVER_TIMEOUT = 5000;

        try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT)) {
            while (true) {
                try {
                    Socket clientSocket = serverSocket.accept();
                    clientSocket.setSoTimeout(SERVER_TIMEOUT);
                    new ClientThread(clientSocket).start();
                } catch (IOException ignored) {
                }
            }
        } catch (IOException ignored) {
        }
    }
}

And in ClientThread I work with every request.

I tried it locally and it works perfectly.

Now I just need to deploy this project somehow to the compute engine and make it work.

Additionally I installed Java JRE on the server. I hope port 3000 is ok if not I can change it.

Thanks in advance and sorry if I wasn't so clear.

Tzlil Gavra
  • 88
  • 1
  • 1
  • 9

2 Answers2

1

Create a runnable JAR file. Copy it to your Compute Engine instance. Run it.

If you want to be able to start/stop this program without logging into your instance, then you need a simple servlet. Look at the embedded Jetty - this is probably the best solution for this use case.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Thanks you!! It is so simple when you say it like that. All I had to do was like you said - create a runnable JAR , copy it to the server and run it. And don't forget to allow the relevant port and protocols in the firewall. – Tzlil Gavra Jul 15 '16 at 22:07
  • can you provide more detail please? with this work with angularjs project created using "ng new" to create the project? how do i make a runnable JAR? how do i copy to the cloud? you mention copy to CE, I want to copy to AE (application engine) -- my understanding is they are two different things. – mobibob Jan 29 '18 at 00:01
  • Compute Engine and App Engine are different indeed: https://stackoverflow.com/questions/22697049/what-is-the-difference-between-google-app-engine-and-google-compute-engine/22697189#22697189 – Andrei Volgin Jan 30 '18 at 18:19
0

My preferred method, is simply use command line tools on the compute engine machine to run the following sequence - same as you would do on your development computer:

Before deploying - do the following setup (one time task when creating the project)

  • Install Java JDK
  • Install maven
  • Install git

Then, after setup is done - you can do the following 2 simple steps, and repeat them every time you want to update your server with a newer version:

  1. Clone your source code from your development repository to the compute engine machine
  2. mvn jetty:run in the cloned project folder

That's it. I like it since it's just what I would do on my local computer.

Ronen Rabinovici
  • 8,680
  • 5
  • 34
  • 46