首页 文章

在外部Tomcat上部署项目

提问于
浏览
8

我按照以下Spring Web套接字指南在stomp上使用Spring Web Sockets创建了一个应用程序 . https://spring.io/guides/gs/messaging-stomp-websocket/当我使用Spring Boot嵌入式Tomcat运行应用程序时,该应用程序正常工作 . 但是现在我想将它部署在Tomcat 7的本地实例上 .

我已将Application类修改为以下内容,

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(applicationClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.scan(WebSocketConfig.class.getPackage().getName());

        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(context));

        appServlet.setLoadOnStartup(1);
        Set<String> mappings = appServlet.addMapping("/app");

        if (!mappings.isEmpty()) {
            throw new IllegalStateException("Conflicting mappings found! Terminating. " + mappings);
        }
    }

    private static Class<Application> applicationClass = Application.class;
}

WebSocket配置如下,

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}

我的build.gradle如下,

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

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

war {
    baseName = 'opl-ws-webui'
    version =  '0.1.0'
}
repositories {
    jcenter()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

bootRepackage {
    enabled false
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-websocket")
    compile("org.springframework:spring-messaging")
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.1'
    compile files('lib/opla230.jar')
    //providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    testCompile("junit:junit")
}

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

当我右键单击项目并从eclipse内部在服务器上运行它时,它会在以下url处提供正确的index.html . http://localhost:8080/opl-ws-webui/

但是,当我按下应该打开文本输入框的连接按钮时,我什么也得不到 . 在检查开发人员工具时,这是我收到的错误消息 .

sockjs-0.3.4.js:807 GET http://localhost:8080/hello/info 404 (Not Found)
Whoops! Lost connection to undefined

我已用Google搜索错误消息并尝试在以下链接中实施建议,Whoops! Lost connection to undefined - Connection Lost Just After The Connection Established Stomp over socket using sockjs can't connect with Spring 4 WebSocket

但我仍然得到同样的错误 . 我已经尝试在Tomcat 7和8上进行部署,但仍然是相同的 . 真的很感激这个问题的一些帮助 .

5 回答

  • 0

    如果您使用的是Spring Boot,那么您可以切换到 war 包装而不是jar,并更改为tomcat依赖的 provided 范围 .

    有关详细信息,请检查

    Jar to war

    Build config

    Serve static content

  • 1

    也许应用程序上下文有问题在本地系统中运行应用程序时,它会映射到上下文根(/) . 但是当你将它上传到tomcat时,上下文将是你的文件名 . 例如,如果您的文件名是"myapp",则上下文将是http://server:8080/myapp,但在本地系统中它以root应用程序运行 . 您可以通过将应用程序设置为tomcat的根上下文或将应用程序路径更改为 生产环境 上下文(在本例中为myapp)来解决此问题 . 设置ROOT应用程序转到(Ubuntu):

    /var/lib/tomcat7/conf/server.xml

    然后加,

    <Context path="ROOT" docBase="myapp">
        <!-- Default set of monitored resources -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
    </Context>
    

    并删除其他root配置(如果有) . 最后重启tomcat服务器 .

  • 0

    我为此目的使用Apache Tomcat Maven插件,但您使用的是Gradle . 它与Tomcat 7完美配合,但不支持Tomcat 8.请参阅[link] [1] . http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/

  • 1

    SockJS不尊重相对URL . 找到相关问题的方法here

  • 0

    你不能用IDE本身运行应用程序,你必须制作一个war文件并在tomcat中部署它将运行它 . 确保端口号8080未被任何其他服务器/应用程序使用 .

相关问题