美文网首页简友广场
愁人的 SpringCloud 微服务多模块打war包部署到t

愁人的 SpringCloud 微服务多模块打war包部署到t

作者: 梦昼初心 | 来源:发表于2020-05-29 20:24 被阅读0次

    注意:SpringCloud 微服务多模块项目需要一个一个打包部署,只要有启动类的都需要打包单独部署,如果打包过程中有错误切记不要盲目去找,要根据错误提示一点找问题所在

    pom.xml 需要加入的文件

    第一:加入packaging
    注意:如果当前pom文件中有packaging里面参数是pom,只需要修改成war就行了;
    若之前没有packaging需要从新添加入下面一行代码.

    <packaging>war</packaging>
    

    第二:SpringBoot 框架中自带tomcat,打包的时候需要剔除自身的tomcat

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <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>
                <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
                    相当于compile,但是打包阶段做了exclude操作-->
                <scope>provided</scope>
            </dependency>
            
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
            </dependency>
    

    将上面的代码放入dependencies中
    注意:需要根据自己项目的情况而定,我在打包的时候出现下面的错误

    Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project ruoyi-eureka: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    

    这个提示并不是你缺少依赖,而是在打包的时候,没有web.xml文件,因为SpringCloud 微服务多模块下没有web.xml文件,此时也不需要加入,在pom文件下加入一行代码如下:

        <!--如果想在没有web.xml文件的情况下构建WAR,请设置为false。-->
        <properties>
            <failOnMissingWebXml>false</failOnMissingWebXml>
        </properties>
    

    所有的依赖加入后,你可能打包的时候,文件名不是自己想要的此时可以在build文件下加入finalName,自定义打包名称

     <build>
            <finalName>ruoyi-eureka</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
    
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    

    启动类

    1.启动类需要继承 SpringBootServletInitializer,重写SpringApplicationBuilder 方法

    package com.ruoyi.eureka;
    
    import com.sun.jersey.spi.container.servlet.WebConfig;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer
    @SpringBootApplication
    public class RuoyiEurekaApp  extends SpringBootServletInitializer
    {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            // 注意这里要指向原先用main方法执行的Application启动类
            return builder.sources(RuoyiEurekaApp.class, WebConfig.class);
        }
    
        public static void main(String[] args)
        {
            SpringApplication.run(RuoyiEurekaApp.class, args);
        }
    }
    

    配置文件

    在这里插入图片描述

    上图有两个配置文件
    application-dev.yml

    server: 
      port: 8665 #端口号尽可能与tomcat保持一致
     
    eureka: 
      instance:
    
        hostname: 127.0.0.1 #eureka服务端的实例名称
      client: 
        register-with-eureka: false     #false表示不向注册中心注册自己。
        fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url: 
          #单机 
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
          defaultZone: http://${eureka.instance.hostname}:${server.port}/
          #defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    

    application-prod.yml

    server: 
      port: 8665 #端口号尽可能与tomcat保持一致
     
    eureka: 
      instance:
    
        hostname: 127.0.0.1 #eureka服务端的实例名称
      client: 
        register-with-eureka: false     #false表示不向注册中心注册自己。
        fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        service-url: 
          #单机 
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
          defaultZone: http://${eureka.instance.hostname}:${server.port}/
          #defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    

    因为对eureka集群不太了解所以两个都配置了

    打包完成

    使用 maven package打包


    在这里插入图片描述

    双击package打包,出现BUILD SUCCESS说明打包成功


    在这里插入图片描述
    打包完成的目录如下
    在这里插入图片描述

    将打包后的war文件放到tomcat的webapps文件下运行tomcat就可以了

    如何用浏览器查看是否部署成功

    请求的时一定要加上项目名称,这样才能访问到,访问地址

    在这里插入图片描述
    刚学,感觉好难啊,弄了两天才完成,欢迎交流学习

    相关文章

      网友评论

        本文标题:愁人的 SpringCloud 微服务多模块打war包部署到t

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