(运维)--应用服务器部署

Posted YX_blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(运维)--应用服务器部署相关的知识,希望对你有一定的参考价值。

在现有的tomcat+nginx 配置

– add by yx
15.6环境下
目前已经存在了 应用test2已经搭建完成

配置服务器

我们以新建一个test为例

## 进入目录
cd  /home/tomcat/servers
## copy一份 test 服务器
cp -R  test2 test3
cd test3
## 查看目录
ll ./
## war包存放位置 webapps
## 日志文件存放 logs
## 配置文件存放 conf
## 修改对应的端口
cd conf 
## 修改数据源 <Resource 标签,具体可以参考其他项目搭建
vim context.xml

查看context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>$catalina.base/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
<Resource name="jdbc/service"
            type="com.mchange.v2.c3p0.ComboPooledDataSource"  
            maxPoolSize="30" minPoolSize="1" acquireIncrement="1"  maxIdleTime="120" initialPoolSize="1"
            factory="org.apache.naming.factory.BeanFactory"  
            user="inno_bdsp_dev2" password="inno_bdsp_dev2"  
                        driverClass="com.mysql.jdbc.Driver"  
                        jdbcUrl="jdbc:mysql://192.168.15.12:3306/inno_bdsp_dev2?characterEncoding=utf8"  
            />
</Context>

查看server.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- 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="6080" 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">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector URIEncoding="UTF-8" port="6099" protocol="org.apache.coyote.http11.Http11AprProtocol"
               connectionTimeout="20000"
               redirectPort="8443"
         emptySessionPath="true" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation with the JSSE engine. When
         using the JSSE engine, the JSSE configuration attributes must be used.
    -->
  <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation. When using the
         APR/native implementation or the OpenSSL engine with NIO or NIO2 then
         the OpenSSL configuration attributes must be used.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true" >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <!--Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /-->


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

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

**查看对应的配置文件 server.conf **

#tomcat实例目录
#export CATALINA_BASE=/home/alpha/servers/"$NAME"

#设置JVM内存大小
export JAVA_OPTS="-Xms2048m -Xmx2048m"

#这里指定的是项目所独有的环境变量
#export RUN_ENV="test"
export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=test"

#远程调试
export JAVA_OPTS="$JAVA_OPTS -Xrunjdwp:transport=dt_socket,address=46900,server=y,suspend=n"
#jmx
export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=45660 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -DrestTable=false "

[========]

## 修改对应的端口  因为服务器中部署了多台,所以端口需要不一样 ,修改点2处 Server  port  + Http prot端口 我这里是 http-->6099
vim server.xml 
## 配置启动参数、远程调用等端口,如果不需要可以注释
vim server.xml
## webapps 因为之前的war 在里面,就没有添加,如果是其他项目,直接更换就好了
## 启动项目--发现没有bin目录 因为数据被bin隐藏了。建立一个软连接即可
ln -s /etc/init.d/tomcat8 test3

## 查看日志
cd /home/tomcat/servers/test3/logs
## 清理之前的日志
rm -rf *  
## 启动项目 
service test3 status
service test3 start
## 查看日志
tail -f catalina.out
## 没报错表示成功启动

[========]

[========]

##配置nginx

## 配置nginx 进入主目录 root 权限
 cd /usr/local/nginx
 ## 开始配置nginx 
 cd conf
 ## 复制配置文件
 cp test2.conf  test3.conf
 ## 编辑test3.conf
 vim test3.conf 

test3.conf 具体如下

#test2
 server 
        listen       80; # 默认监听80端口
        server_name  yx.test3.service.cn; ## yx 这个表示配置的域名访问
        charset utf-8;
        #access_log  logs/host.access.log  main;
        error_page 405 = $uri;

        location / 
            root   html/test;
            index  index.html index.html;
        

        location /test/ 
           proxy_redirect off;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           client_max_body_size 55m;
           proxy_read_timeout      120;
           proxy_pass http://test3;  ## yx 这个与 nginx.config 的upstream配合使用
        


         location /test_res 
            expires  3d; ## 静态资源缓存时间
            alias  /data2/test2 ;## 因为这里是和test2共用的,所以就直接使用即可,其他的直接修改即可 。动静分离
            autoindex on;
        

        #error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;

        location = /50x.html 
            root   html;
        
 

##配置nginx 并且引入test3.conf
 vim nginx.conf
    upstream test3
        server 192.168.15.6:6099;
        

    include test3.conf;
## 重启nginx
## nginx 平滑重启
  /usr/local/nginx/sbin/nginx  -s reload

通知运维将我们配置的域名统一
yx.test3.service.cn 配置进入dns 即可。
如果本地想要看效果,直接配置hosts 也可以

ip yx.test3.service.cn
就可以直接访问了。

以上是关于(运维)--应用服务器部署的主要内容,如果未能解决你的问题,请参考以下文章

Kubernetes运维之部署主流JAVA应用

华为云和开源Istio运维管理对比样例应用部署

自动化运维之Ansible服务部署

运维工程师系统日志收集ELK之Elasticsearch服务部署

自动化运维工具 puppet安装部署

服务器运维标准化