Develop Like It's Local - Remote Java Development with IntelliJ IDEA

Develop Like It's Local - Remote Java Development with IntelliJ IDEA

In this article, we are going to set up the code for our simple project on a remote server, but develop it using IntelliJ IDEA (and its "Remote Development" capability).

We will just set up the "Hello World" Spring application, so it is simple and we can concentrate on the feature of remote development instead of Java code in this case.

Setting Up a Remote VM

Our code will reside on a remote VM, and we will connect to it with IntelliJ IDEA, which can do this seamlessly using its "Remote Development" configuration.

This way, when we develop our code - it will update the files straight on the remote VM. When we run our application - it will run on the remote VM too. We will also be able to debug.

Another benefit is that you can run several applications on several VMs, without any conflicts with ports and without your local machine memory swelling up.

Please use this guide to set up a remote VM on Google Cloud: What's the Cheapest Remote VM for Development?, and this guide to SSH to it: How to SSH into a Google Compute Engine VM.

Checking Out the Project with Git

I will first install Git on my remote machine using the following commands...

Update the package list:

sudo apt-get update        

Install git:

sudo apt-get install git        

Verify the installation:

git --version        

The above commands are for Debian Linux. If you are using CentOS, then you will install it as follows:

sudo yum install git        

Once I have my Git installed, I will clone the project like this:

git clone https://github.com/spring-guides/gs-spring-boot.git        

Setting Up IntelliJ Remote Project

Now that you have your code on the remote machine, you are ready to start working it with your local IDE!

On IntelliJ startup - choose Remote Development --> SSH:

Select "Connect to Host...":

Enter your host parameters. You can check again How to SSH into a Google Compute Engine VM to see what parameters you should enter.

Then, click on the "plus" ("Open Project") button:

In the next window, click to choose the project directory on your remote server:

I will go ahead and select the "complete" project from my sample, so I have all the code:

Next, select "Download IDE and Connect":

The way it works - it installs IntelliJ IDE backend on your remote server, for you to be able to work remotely on your project. So it will show the progress message like this:

Warning! Make sure you follow the minimal requirements of remote IDE, otherwise it will probably get stuck. At this point it is 4 vCPUs and 8G of memory. You can find more details at this link: System requirements for remote development.

Once the process finishes, it will show the license agreement - agree with it:

Then the IDE will start up, click on "Get Started":

Import your project as usual.

After the import, I am getting this error because I don't have a JDK installed on my VM yet.

I've installed a JDK onto my VM and configured IntelliJ IDEA to use it. I've also closed and re-opened my project. I have no errors now.

I will build it as usual, then select Run configuration and configure it to run as Application:

I ran the project and it says it is listening on port 8080:

Great! The easiest way to access it via a web browser is to "forward port", so you access it as if it is running on your local machine:

Once you select the above option, it will open your browser and you will see this:

If you don't like working with it as if it is on your own machine (with port forwarding), then you will need to enable a specific firewall rule - see below.

I don't like port forwarding, because I am going to have several applications I develop at the same time, running on the same port, so I prefer to access them from the remote server directly.

Opening a Port on Your Google Cloud VM

If we just try to access our application through a remote server URL like this: https://gce--spring-samples--hello-world:8080, then it wouldn't be able to connect:

In order for this to work, we need to enable a firewall rule on Google Cloud to allow external access to port 8080.

Go to your Google Cloud and click on "VPC Network":

Then select "Firewall" from the left menu:

Click on "Create Firewall Rule":

Here are the values that I had to fill in (and I left everything else default):

  • name - select any name you like
  • targets - select "All instances in the network", which means that our rule will apply to all VM instances within the specified VPC (Virtual Private Cloud) network
  • source IPv4 ranges - I entered 0.0.0.0/0, which means this rule will apply to all IP addresses (so is public)

...then on the same page - specify TCP protocol, and port = 8080, and click "Create":

Now I can access my application from the remote server:

Conclusion

In this tutorial, we set up IntelliJ to work with the code that we store remotely on our Google Cloud VM, over SSH.

Please post in comments if you have any questions!

要查看或添加评论,请登录

Olga Strijewski的更多文章

其他会员也浏览了