由于最近公司启动了微服务项目,使用框架springcloud搭建微服务,因此开始了解java。 由于工作中用的基本为apache、nginx,发现java程序需要部署在tomcat下,因此开始漫长的 tomcat“采”坑之路
首先说先项目基本情况
- 微服务的基本框架需要部署在tomcat下
- 基础业务也用java来实现【可选】
- 业务服务器部署两台
- 使用springboot开发项目
在部署tomcat时,遇到几个问题如下:
- 如何打包
- 如何部署
- 部署之后无效果
- 两台相同业务服务部署出错
针对以上问题,总结下自己的坑:
第1步搭建springboot项目
本人大家eureka服务注册于发现,中间涉及到不需要的代码可忽略
- 项目搭建完成后 需要将启动程序设置为springbootapplication,采用注解方式,并添加依赖
- 更新启动类集成springbootserletinitiallizer
- 重新config方法
第2步:取消其他类中springbootapplication配置
第3步:在application.properties中配置 default-domain
即使是同一个服务 ,不同端口部署也需要修改此配置,如果在同一个服务器上部署多个服务会导致出错
第4步 更新pom文件
- 打包方式更新为war为了部署
- eureka-server 配置是为了服务注册与发现,此处可忽略,不影响到部署
- 注意,本人搭建项目中没有tomcat 的import ,因此如果此处有tomcat 请注释掉
- 这个插件难道是为了maven的package打包工具吗?我猜
第5步开始打包
如果有这个插件就双击,如果没有 应该去安装吧
打包出现BUILD SUCCESS 表示打包成功了
对我这种初次使用tomcat的用户来说,简直了。。。 Tomcat 配置内容还是要了解下,否则一头雾水
当然,如果你像我这样一头雾水也能配置成功,就忽略,当心给自己挖坑
- 对应单个服务配置,需要修改的是端口号及服务路径,就是war包放置的位置
- 初次之外,增加了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 "%r" %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 "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>