由于最近公司启动了微服务项目,使用框架springcloud搭建微服务,因此开始了解java。 由于工作中用的基本为apache、nginx,发现java程序需要部署在tomcat下,因此开始漫长的 tomcat“采”坑之路

首先说先项目基本情况
  1. 微服务的基本框架需要部署在tomcat下
  2. 基础业务也用java来实现【可选】
  3. 业务服务器部署两台
  4. 使用springboot开发项目

在部署tomcat时,遇到几个问题如下:
  1. 如何打包
  2. 如何部署
  3. 部署之后无效果
  4. 两台相同业务服务部署出错

针对以上问题,总结下自己的坑:


第1步搭建springboot项目

本人大家eureka服务注册于发现,中间涉及到不需要的代码可忽略
  1. 项目搭建完成后 需要将启动程序设置为springbootapplication,采用注解方式,并添加依赖
  2.  更新启动类集成springbootserletinitiallizer
  3. 重新config方法




第2步:取消其他类中springbootapplication配置




第3步:在application.properties中配置 default-domain

    即使是同一个服务 ,不同端口部署也需要修改此配置,如果在同一个服务器上部署多个服务会导致出错






第4步 更新pom文件

  1. 打包方式更新为war为了部署
  2. eureka-server 配置是为了服务注册与发现,此处可忽略,不影响到部署
  3. 注意,本人搭建项目中没有tomcat 的import ,因此如果此处有tomcat 请注释掉



  1. 这个插件难道是为了maven的package打包工具吗?我猜


提供本人完整pom文件
<? xml version ="1.0" encoding ="UTF-8" ?>
   < modelVersion >4.0.0 </ modelVersion >

   < groupId >com.example </ groupId >
   < artifactId >servicehi </ artifactId >
   < version >0.0.1-8763 </ version >
   < packaging >war </ packaging >

   < name >server-hi </ name >
   < description >Demo project for Spring Boot </ description >

   < parent >
      < groupId >org.springframework.boot </ groupId >
      < artifactId >spring-boot-starter-parent </ artifactId >
      < version >2.0.0.RELEASE </ version >
      < relativePath /> <!-- lookup parent from repository -->
   </ parent >

   < properties >
      < project.build.sourceEncoding >UTF-8 </ project.build.sourceEncoding >
      < project.reporting.outputEncoding >UTF-8 </ project.reporting.outputEncoding >
      < java.version >1.8 </ java.version >
      < spring-cloud.version >Finchley.M9 </ spring-cloud.version >
   </ properties >

   < dependencies >
      < dependency >
         < groupId >org.springframework.cloud </ groupId >
         < artifactId >spring-cloud-starter-netflix-eureka-server </ artifactId >
      </ dependency >

      < dependency >
         < groupId >org.springframework.boot </ groupId >
         < artifactId >spring-boot-starter-test </ artifactId >
         < scope >test </ scope >
      </ dependency >
   </ dependencies >

   < dependencyManagement >
      < dependencies >
         < dependency >
            < groupId >org.springframework.cloud </ groupId >
            < artifactId >spring-cloud-dependencies </ artifactId >
            < version >${spring-cloud.version} </ version >
            < type >pom </ type >
            < scope >import </ scope >
         </ dependency >
      </ dependencies >
   </ dependencyManagement >

   < build >
      < plugins >
         < plugin >
            < groupId >org.springframework.boot </ groupId >
            < artifactId >spring-boot-maven-plugin </ artifactId >
         </ plugin >
         < plugin >
            < groupId >org.apache.maven.plugins </ groupId >
            < artifactId >maven-war-plugin </ artifactId >
            < configuration >
               < warName >psb </ warName >
            </ configuration >
         </ plugin >
      </ plugins >
   </ build >

   < repositories >
      < repository >
         < id >spring-milestones </ id >
         < name >Spring Milestones </ name >
         < url > https://repo.spring.io/milestone </ url >
         < snapshots >
            < enabled >false </ enabled >
         </ snapshots >
      </ repository >
   </ repositories >


</ project >







第5步开始打包

如果有这个插件就双击,如果没有 应该去安装吧




打包出现BUILD SUCCESS 表示打包成功了


第6步 部署
对我这种初次使用tomcat的用户来说,简直了。。。 Tomcat 配置内容还是要了解下,否则一头雾水
当然,如果你像我这样一头雾水也能配置成功,就忽略,当心给自己挖坑
  1. 对应单个服务配置,需要修改的是端口号及服务路径,就是war包放置的位置
  2. 初次之外,增加了Context的配置,用于指定服务包路径
        <Context docBase="servicehi-0.0.1-8762.war" path="/" reloadable="true" debug="0" privileged="true" />


完整的server.xml文件如下
附上本人的server.xml供大家参考

<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
-->
<Server port="8769" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->

  <Service name="Catalina">
    <Connector port="8762" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context docBase="servicehi-0.0.1-8762.war" path="/" reloadable="true" debug="0" privileged="true" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_8762_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>

  <Service name="Catalina2">
    <Connector port="8763" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    <Engine name="Catalina2" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps2"
            unpackWARs="true" autoDeploy="true">
        <Context docBase="servicehi-0.0.1-8763.war" path="/" reloadable="true" debug="0" privileged="true" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_8763_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>


</Server>







Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐