我如何使用 Bluemix Liberty 应用程序监听自定义端口?

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

我有一个 spring boot camel hello world 应用程序,我想将其部署到 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";
    }
}

我也会发布我的 build.gradle 以防有用:

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'
}

在 Bluemix 日志文件中,我可以看到 camel 已绑定到端口:

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

但是,当我尝试连接时返回 "Connection refused" 消息。

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

在我的用例中,camel 使用 netty 来侦听端口 18080。我看到 question on developerWorks 指出无法绑定到 Bluemix Liberty 应用程序上的自定义端口。是否有另一种方法可以在不使用 Docker 或 VM 的情况下支持此用例?

Bluemix 运行时实际上不允许使用自定义端口,唯一允许的是 http/https,TCP 上的 80 和 443

IBM 容器允许来自端口子集的自定义端口,无论如何您都必须在 docker 文件中公开它们。

虚拟服务器测试版允许您使用自定义端口,您必须相应地在 openstack 上设置安全策略(或者只是打开所有端口,即使不是很安全)