Running a java application with spring-boot, and g

2019-04-10 18:55发布

问题:

I am trying to build a restful service with eclipse, java, gradle, spring-boot and host it on a digital ocean server. I want to get a JSON response when I access the digital ocean url on a specific port or something, so I can do 123.456.78:90 and see JSON in the browser.

When I run this in eclipse and access localhost, this works fine. Accessing localhost:port# gives the desired output, but I'm noticing differences in the console log from eclipse and when I run it on the server.

From eclipse:

2016-01-24 09:42:56.245  INFO 7372 --- [           main] Keen.KeenAPICallTester                   : Starting KeenAPICallTester on ****-THINK with PID 7372 (C:\Users\****\workspace\****-ML\bin started by **** in C:\Users\****\workspace\****-ML)
2016-01-24 09:42:56.247  INFO 7372 --- [           main] Keen.KeenAPICallTester                   : No active profile set, falling back to default profiles: default
2016-01-24 09:42:56.299  INFO 7372 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@116af24: startup date [Sun Jan 24 09:42:56 EST 2016]; root of context hierarchy
2016-01-24 09:42:56.893  INFO 7372 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-24 09:42:57.492  INFO 7372 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9011 (http)
2016-01-24 09:42:57.502  INFO 7372 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-24 09:42:57.503  INFO 7372 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-24 09:42:57.599  INFO 7372 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-24 09:42:57.599  INFO 7372 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1301 ms
2016-01-24 09:42:57.880  INFO 7372 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-24 09:42:57.883  INFO 7372 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-24 09:42:57.884  INFO 7372 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-24 09:42:57.884  INFO 7372 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-24 09:42:57.885  INFO 7372 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-24 09:42:58.120  INFO 7372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@116af24: startup date [Sun Jan 24 09:42:56 EST 2016]; root of context hierarchy
2016-01-24 09:42:58.183  INFO 7372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/extractionTest],methods=[GET],produces=[application/json]}" onto public java.lang.Object Keen.MLJSONResponseController.testExtractionRequest()
2016-01-24 09:42:58.184  INFO 7372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/inspectAllProjectsTest],methods=[GET],produces=[application/json]}" onto public java.lang.Object Keen.MLJSONResponseController.testInspectionRequest()
2016-01-24 09:42:58.186  INFO 7372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-01-24 09:42:58.186  INFO 7372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-01-24 09:42:58.208  INFO 7372 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-24 09:42:58.208  INFO 7372 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-24 09:42:58.246  INFO 7372 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-24 09:42:58.349  INFO 7372 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-01-24 09:42:58.402  INFO 7372 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9011 (http)
2016-01-24 09:42:58.406  INFO 7372 --- [           main] Keen.KeenAPICallTester                   : Started KeenAPICallTester in 2.445 seconds (JVM running for 2.739)

From Digital Ocean:

2016-01-24 09:22:11.190  INFO 29453 --- [           main] Keen.KeenAPICallTester                   : Starting KeenAPICallTester on **** with PID 29453 (/root/java-ml/learning/build/classes/main started by root in /root/java-ml/learning)
2016-01-24 09:22:11.224  INFO 29453 --- [           main] Keen.KeenAPICallTester                   : No active profile set, falling back to default profiles: default
2016-01-24 09:22:11.949  INFO 29453 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@8b87145: startup date [Sun Jan 24 09:22:11 EST 2016]; root of context hierarchy
2016-01-24 09:22:16.874  INFO 29453 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-24 09:22:20.350  INFO 29453 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 80 (http)
2016-01-24 09:22:20.448  INFO 29453 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-24 09:22:20.457  INFO 29453 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-24 09:22:21.015  INFO 29453 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-24 09:22:21.016  INFO 29453 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 9086 ms
2016-01-24 09:22:22.582  INFO 29453 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-24 09:22:22.613  INFO 29453 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-24 09:22:22.636  INFO 29453 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-24 09:22:22.637  INFO 29453 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-24 09:22:22.637  INFO 29453 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]

My guess is that for some reason my digital ocean server isn't playing nice with Tomcat or something. I opened ports via the ufw command, checked iptables, installed apache2 (I don't even know if I had to to be honest) and made sure that there were configurations set to accept outbound and inbound requests in port 8080 and 8000.

I noticed that my request methods are not getting mapped, which makes me think that there is some dependency problem. Here is my build.gradle for reference.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE")

    }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

mainClassName = "Keen.KeenAPICallTester"
applicationDefaultJvmArgs = ["--server.port=8000"]

jar {
    baseName = '****-ML'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8



dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
    compile fileTree(dir: 'libs', include: '*.jar')
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

task(runSimple, dependsOn: 'classes', type: JavaExec) {
    main = 'Keen.KeenAPICallTester'
    classpath = sourceSets.main.runtimeClasspath
    args '--server.port=8000'
}

Is there something wrong with the way I'm handling this? I also have a runSimple command so that I could play with the JVM port run, but it makes no difference, even if I set it to 80 and go straight to the default digital ocean IP address.

I've run applications on digital ocean before and usually it was as simple as opening a specific port via ufw and running the application on the port, then accessing it via the internet. But there's something under the hood that's going on that I would like to understand.

回答1:

Seems like the application is stuck because it might be waiting for /dev/random (blocking device, might have run dry).

You could use /dev/urandom instead by starting your application passing the JVM-param -Djava.security.egd=file:/dev/./urandom.

See Avoiding JVM Delays Caused by Random Number Generation

Furthermore, to drill down such weird issues, tools such as jstack or strace might be of help.