Search code examples
ibm-cloudwebsphere-liberty

How do I listen on a custom port with a Bluemix Liberty application?


I have a spring boot camel hello world application that I would like to deploy to Bluemix:

import org.apache.camel.spring.boot.*
import org.springframework.boot.*
import org.springframework.boot.autoconfigure.*
import org.springframework.context.*
import org.springframework.context.annotation.*
import org.springframework.scheduling.annotation.*

@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
class Application extends FatJarRouter {

    public static void main(String... args) {
        ApplicationContext applicationContext = new SpringApplication(Application.class).run(args);
        CamelSpringBootApplicationController applicationController =
                applicationContext.getBean(CamelSpringBootApplicationController.class);
        applicationController.run();
    }

    @Override
    public void configure() throws Exception {
        from("netty4-http:http://0.0.0.0:18080").
            setBody().simple("ref:helloWorld");
    }

    @Bean
    String helloWorld() {
        return "helloWorld";
    }
}

I'm also posting my build.gradle in case it is useful:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
    }
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'spring-boot'
apply plugin: 'eclipse'

jar {
    baseName = 'EAI_Service'
    version =  '0.1.0'
}
repositories {
    mavenCentral()
}
dependencies {
    compile("org.codehaus.groovy:groovy-all:2.2.1")
    compile("org.springframework.boot:spring-boot-starter-web:1.2.3.RELEASE")

    compile("org.apache.camel:camel-core:2.17.0")
    compile("org.apache.camel:camel-spring-boot-starter:2.17.0")
    compile('org.apache.camel:camel-netty4-http:2.17.0')
}
task wrapper(type: Wrapper) {
   gradleVersion = '2.0'
}

In the Bluemix log file, I can see camel has bound to the port:

ServerBootstrap binding to 0.0.0.0:18080
Netty consumer bound to: 0.0.0.0:18080
Route: route1 started and consuming from: Endpoint[http://0.0.0.0:18080]
Total 1 routes, of which 1 are started.
Apache Camel 2.17.0 (CamelContext: camel-1) started in 0.590 seconds
Tomcat started on port(s): 61649 (http)
Started Application in 7.345 seconds (JVM running for 9.164)
Apache Camel 2.17.0 (CamelContext: camel-1) is starting
Total 1 routes, of which 1 are started.
 Apache Camel 2.17.0 (CamelContext: camel-1) started in 0.000 seconds

However, a "Connection refused" message is returned when I try to connect.

snowch$ curl <<myapp>>.mybluemix.net:18080
curl: (7) Failed to connect to <myapp>>.mybluemix.net port 18080: Connection refused

In my use case, camel uses netty to listen on port 18080. I have seen a question on developerWorks that states that it isn't possible to bind to a custom port on a Bluemix Liberty application. Is there another way to support this use case without using Docker or a VM?


Solution

  • The Bluemix runtimes actually don't allow to use custom ports, the only one allowed are the http/https ones, 80 and 443 over TCP

    IBM containers are allowing custom ports from a subset of ports, you have to expose them on the docker file anyway.

    Virtual servers beta allow you to use your custom port, you have to setup your security policies on openstack accordingly (or simply to open all the ports, even if not really secure)