简体   繁体   中英

How to host a Java TCP server in AWS Elastic Beanstalk

I have a Java project using the java.net library to create a TCP server, and I wish to run it on AWS Elastic Beanstalk.

I understand that nginx can't host native TCP sockets (prior to v1.9, which EB doesn't support yet), and EB uses NGINX v1.8 (even though it says it uses v1.6.2 here ).

Is there any way to get my server running on Elastic Beanstalk? Should I look into switching from Java SE to Java EE?

Thanks,
Nathan

@Zoyt You can deploy TCP server on the AWS Elastic Beanstalk (EB). EB is primary focused on the typical web application. However, there is ways to configure EB as you need. What I would recommended:

  1. Add to your application health check web page - eb is focused on the web, so we need to give to the eb web page;
  2. With help of the eb configuration, we can modify ELB settings and specify that we want to listen TCP port and set other required parameters to us. Available environment configuration options here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html
  3. In order to modify software and OS configuration you can use: .ebextensions configurations file: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html - this configs give ability to install packages, create file and run some scripts, required for the bootstrapping of the your instances. In general the idea is a same like: Cloud-init ( https://cloudinit.readthedocs.org/en/latest/ ). Also if customisation is to complex, you may build own AMI and use it. Approach with the custom AMI also speed up start of the ec2 instances, because complicated bootstrap scripts can run slowly or fails.

As summary you have all the tools that allows to deploy your server on the EB.

if your application supports HTTP, then there's nothing to do, so I assume it doesn't. EB is tightly coupled with HTTP for traffic routing, monitoring and balancing - so you'll have to configure some stuff to let EB do its thing.

  1. Create a load balanced Java based EB application
  2. ensure that the designated port is open for this instance (set an appropriate security group)
  3. In the Load Balancer configuration choose TCP as your protocol with a designated port
  4. Pack your java code as a .war file, to allow EB to deploy it

Now this should work, but unfortunately monitoring may still fail. If that's the case, write a small servlet that monitors your TCP server and add it to your deployment war. Configure the LB to check that URL and EB will create/destroy instances as your application load changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM