美文网首页
tomcat配置

tomcat配置

作者: sunpy | 来源:发表于2022-12-18 19:11 被阅读0次

    jvm配置


    配置catalina.sh命令,设置内存参数:

    # linux下:在tomcat的bin目录下编辑catalina.sh cygwin=false上面加入
    JAVA_OPTS="-Xms1024m -Xmx2048m -Xss1024K -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize1024m"
    

    服务器配置


    context.xml

    context.xml 是 tomcat公用的环境配置,tomcat 服务器会定时去扫描这个文件。一旦发现文件被修改(时间戳改变了),就会自动重新加载这个文件,而不需要重启服务器。

    服务一旦启动,在去修改server.xml,就得需要重新加载配置文件,或者重新启动服务来加载文件。 而context.xml的优势是无需重启。 所以我们一般会在这个文件中独立配置。

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 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>WEB-INF/tomcat-web.xml</WatchedResource>
        <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
        
        <!--本地测试项目-->
        <!-- name,指定JNDI名称 -->
        <!-- auth,表示认证方式,一般为Container -->
        <!-- maxActive,连接池支持的最大连接数 -->
        <!-- maxIdle,连接池中最多可空闲连接数 -->
        <!-- maxWait,连接池中连接用完时,新的请求等待时间,单位毫秒 -->
        <!-- username,password,数据库用户名/密码 -->
        <!-- driverClassName,jdbc驱动 -->
        <!-- url,数据库url地址 -->
        <Resource name="jdbc/opslocal" 
        auth="Container" 
        type="javax.sql.DataSource" 
        username="scott" 
        password="admin" 
        driverClassName="oracle.jdbc.driver.OracleDriver" 
        url="jdbc:oracle:thin:@localhost:1521:orcl" 
        maxActive="100000" 
        maxIdle="4"/>
        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->
    </Context>
    

    server.xml

    server.xml是对tomcat的设置,可以设置端口号,添加虚拟机这些的,是对服务器的设置。

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      port="8005" tomcat监听关闭服务器的端口
      shutdown="SHUTDOWN" 关闭服务器的指令的字符串
    -->
    <Server port="8005" shutdown="SHUTDOWN">
    <!--
      VersionLoggerListener 监听器以日志形式输出服务器、操作系统、jvm版本信息
      AprLifecycleListener 监听器用以加载和停止APR库。不影响tomcat启动
      JreMemoryLeakPreventionListener 监听器用以避免JRE内存泄露
      GlobalResourcesLifecycleListener 监听器用以加载和销毁全局命名服务
      ThreadLocalLeakPreventionListener 用于在context停止时重建Executor池中的线程,避免ThreadLocal内存泄露
    -->
      <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
      <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
      <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
      <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
    <!--
      GlobalNamingResources 定义全局命名服务
    -->
      <GlobalNamingResources>
        <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>
      <Service name="Catalina">
    <!--
        Connector 链接器
        port="8080" Connector用于创建服务端Socket并进行监听,以等待客户端请求链接。同一个IP地址的同一个端口号
            操作系统只允许存在一个服务端应用监听。如果该属性设置为0,tomcat将随机选择一个可用的端口号给当前Connector使用。
        protocol="HTTP/1.1" 当前支持的访问协议。
        connectionTimeout="20000" Connector接收链接后的等待超时时间,单位毫秒。 -1 表示不超时。
        redirectPort="8443" 如果当前Connector支持non-SSL请求,并且接收到一个请求,符合<security-contraint>约束,需要SSL传输,Catalina自动将请求重定向到此处指定端口。
    -->
        <Connector port="8080" protocol="HTTP/1.1"
                executor
                   connectionTimeout="20000"
                   redirectPort="8443" />
        <Connector port="8009" protocol="AJP/1.3"
                   address="::1"
                   redirectPort="8443" />
        <Engine name="Catalina" defaultHost="localhost">
    <!--
        Host 配置一个虚拟主机
        name 当前Host通用的网络名称,必须与DNS服务器注册信息一致。
            Engine中包含的Host必须存在一个名称与Engine的defaultHost设置一致。
        appBase 当前Host的应用基础目录,在当前Host上部署的Web应用均在该目录下。
            可以是一个绝对路径,也可以是一个相对路径。
        unpackWARs 设置为true,Host在启动时会将appBase目录下的WAR包解压为目录。
            设置为false,Host将直接从WAR文件中启动Web应用。Host的appBase目录外的WAR文件不会解压缩。
        autoDeploy 控制tomcat是否在运行时定期检测新增或者存在更新的web应用。
            如果为true,tomcat定期检测appBase和xmlBase目录,部署新发现的Web应用或者Context描述文件。
            存在更新的Web应用或者Context描述文件将触发Web应用的重新加载。
    -->
          <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
            <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>
    

    web.xml

    Web应用程序描述文件,都是关于是Web应用程序的配置文件。所有Web应用的 web.xml 文件的父文件。

    springboot多环境打包配置


    application.yml

    server:
      port: 3800
      servlet:
        encoding:
          charset: UTF-8
    spring:
      application:
        name: simple-web
      profiles:
        active: @spring.profiles.active@
    

    pom文件配置:

    <build>
            <finalName>simple-web</finalName>
            <plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <!-- maven 打包插件 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.6.3</version>
                    <configuration>
                        <!--打包时允许添加本地jar包-->
                        <!--<includeSystemScope>true</includeSystemScope>-->
                        <fork>true</fork>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
            </plugins>
    
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <!--开启过滤,用指定的参数替换directory下的文件中的参数-->
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
        <!-- maven多环境打包配置 -->
        <profiles>
            <!-- 开发环境 -->
            <profile>
                <id>dev</id>
                <properties>
                    <spring.profiles.active>dev</spring.profiles.active>
                </properties>
            </profile>
            <!-- 生产环境 -->
            <profile>
                <id>prod</id>
                <properties>
                    <spring.profiles.active>prod</spring.profiles.active>
                </properties>
                <!-- 设置为默认环境 -->
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
            <!-- 测试环境 -->
            <profile>
                <id>test</id>
                <properties>
                    <spring.profiles.active>test</spring.profiles.active>
                </properties>
            </profile>
        </profiles>
    

    tomcat配置数据源


      1. 配置server.xml文件
    <Service name="Catalina">
        <Context path="jdbc/1" reloadable="true"></Context>
        <Context path="jdbc/2" reloadable="true"></Context>
    
      1. 配置context.xml文件
    <Context>
    
        <!-- Default set of monitored resources. If one of these changes, the    -->
        <!-- web application will be reloaded.                                   -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
        <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
        <Resource name="jdbc/1"      
                auth="Container"  
                type="javax.sql.DataSource"  
                driverClassName="com.mysql.cj.jdbc.Driver"  
                url="jdbc:mysql://ip:3390/permission"  
                username="root"  
                password="pwd"  
                maxActive="500"  
                maxIdle="500"  
                maxWait="36000"  
                /> 
            <Resource name="jdbc/2"      
                auth="Container"  
                type="javax.sql.DataSource"  
                driverClassName="com.mysql.cj.jdbc.Driver"  
                url="jdbc:mysql://ip:3379/permission"  
                username="root"  
                password="pwd"  
                maxActive="30"  
                maxIdle="20"  
                maxWait="36000"/>
        <!-- Uncomment this to disable session persistence across Tomcat restarts -->
        <!--
        <Manager pathname="" />
        -->
    </Context>
    
      1. tomcat的lib下加入数据库驱动
      1. springboot不使用内置tomcat
        修改打包形式:
    <packaging>war</packaging>
    

    移除嵌入的tomcat插件:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- 移除嵌入式tomcat插件 -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    

    继承SpringBootServletInitializer重写configure方法

    @SpringBootApplication
    public class ExportApplication extends SpringBootServletInitializer {
        public static void main(String[] args) {
            SpringApplication.run(ExportApplication.class, args);
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(this.getClass());
        }
    }
    
      1. springboot2.x使用jndi单数据源,整合mybatisplus查询数据库
        application.yml配置:
    spring:
      datasource:
        jndi-name: jdbc/1
    mybatis-plus:
      # xml文件路径
      mapper-locations: classpath:mapper/*.xml
      # 实体类路径
      type-aliases-package: com.sunpy.simpleweb.po
      configuration:
        # 驼峰转换
        map-underscore-to-camel-case: true
        # 是否开启缓存
        cache-enabled: false
        # 打印sql
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      # 全局配置
      global-config:
        # 数据库字段驼峰下划线转换
        db-column-underline: true
    

    context.xml:

    <Resource name="jdbc/1"      
            auth="Container"  
            type="javax.sql.DataSource"  
            driverClassName="com.mysql.cj.jdbc.Driver"  
            url="jdbc:mysql://49.235.73.14:3390/education"  
            username="root"  
            password="spy1679358426"   
        />
    

    查询数据库:

    @GetMapping("/index")
    public ResultModel<Object> index() {
        ResultModel<Object> resultModel = new ResultModel<>();
        resultModel.setMsg(enterpriseParamModel.getEnv());
        resultModel.setTime(TimeUtil.getNowTime());
        QueryWrapper qw = new QueryWrapper<>();
        List<Teacher> teacherList = teacherMapper.selectList(qw);
        resultModel.setRes(teacherList);
        return resultModel;
    }
    
      1. springboot2.x使用jndi多数据源,整合mybatisplus查询数据库
        application.yml文件:
    spring:
      datasource:
        dynamic:
          datasource:
            master:
              jndi-name: jdbc/1
            slave:
              jndi-name: druid/1
    mybatis-plus:
      # xml文件路径
      mapper-locations: classpath:mapper/*.xml
      # 实体类路径
      type-aliases-package: com.sunpy.simpleweb.po
      configuration:
        # 驼峰转换
        map-underscore-to-camel-case: true
        # 是否开启缓存
        cache-enabled: false
        # 打印sql
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      # 全局配置
      global-config:
        # 数据库字段驼峰下划线转换
        db-column-underline: true
    

    context.xml文件:

    <Resource name="jdbc/1"      
            auth="Container"  
            type="javax.sql.DataSource"  
            driverClassName="com.mysql.cj.jdbc.Driver"  
            url="jdbc:mysql://IP1:3390/education"  
            username="root"  
            password="pwd1"   
        /> 
        <Resource name="druid/1"   
            factory="com.alibaba.druid.pool.DruidDataSourceFactory"
            auth="Container"  
            type="javax.sql.DataSource"  
            driverClassName="com.mysql.cj.jdbc.Driver"  
            url="jdbc:mysql://IP2:3379/education"  
            username="root"  
            password="pwd2"   
        /> 
    

    换druid查询数据库:

    @DS("slave")
    @GetMapping("/index2")
    public ResultModel<Object> index2() {
        ResultModel<Object> resultModel = new ResultModel<>();
        resultModel.setMsg(enterpriseParamModel.getEnv() + " druid/1");
        resultModel.setTime(TimeUtil.getNowTime());
        QueryWrapper qw = new QueryWrapper<>();
        List<Teacher> teacherList = teacherMapper.selectList(qw);
        resultModel.setRes(teacherList);
        return resultModel;
    }
    

    注意:
    在使用其他数据库的时候,别忘了在tomcat的lib下,加入druid数据库驱动包。

    参考


    https://blog.csdn.net/qq_42499737/article/details/118635728

    https://www.dandelioncloud.cn/article/details/1523532722996023298

    相关文章

      网友评论

          本文标题:tomcat配置

          本文链接:https://www.haomeiwen.com/subject/hxtjqdtx.html