idea springboot 发布webservice 发布服务_SpringBoot发布8个原则和4个问题的解决方案|干货分享...

本文详细介绍了将SpringBoot多模块项目发布到外部Tomcat的8大原则,包括正确打包、排除内置Tomcat、设置启动类等,并提供了常见问题及解决方案,如端口号配置、依赖加载失败等问题,帮助开发者顺利部署SpringBoot应用。

a2680b9ab3e11d1d818b8e60e31218c0.png

如果使用 SpringBoot 多模块发布到外部 Tomcat,可能会遇到各种各样的问题。本文归纳了以下 8 个原则和发布时经常出现的 4 个问题的解决方案,掌握了这些原则和解决方案,几乎可以解决绝大数 SpringBoot 发布问题。

SpringBoot 多模块发布的 8 大原则

1 在发布模块打包,而不是父模块上打包

比如,以下项目目录:

451e7c72b120408e193406c73125f9d4.png

如果要发布 api 就直接在它的模块上打包,而不是在父模块上打包。

2 公共调用模块,打包类型设置为 jar 格式

公共模块,比如 commonmodel 需要设置 packagingjar 格式,在 pom.xml 配置:

<packaging>jar</packaging>

3 发布模块打包类型设置为 war 格式

在发布的模块 pom.xml 中设置:

<packaging>war</packaging>

4 排除内置 tomcat

在发布的模块 pom.xml 中设置:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
</dependency>

当设置 scope=provided 时,此 jar 包不会出现在发布的项目中,从而就排除了内置的 tomcat。

5 设置启动类

此步骤相当于告诉 tomcat 启动的入口在哪。需要在启动类添加如下代码:

@SpringBootApplication
public class ApiApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ApiApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

6 如果使用拦截器一定要排除静态文件

比如我在项目中使用了 swagger,那我就需要排除 swagger 的静态文件,代码如下:

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 排除静态文件
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    // do something
}

7 先装载公共模块,再发布项目

如果发布的模块引用了本项目的其他公共模块,需要先把本项目的公共模块装载到本地仓库。

操作方式,双击父模块的 install 即可, install 成功之后,点击发布模块的 package 生成 war 包,就完成了项目的打包,如下图所示:

1cd5993b1ef5fb4efba71274db1e7d83.png

8 部署项目

有了 war 包之后,只需要把单个 war 包,放入 tomcat 的 webapps 目录,重新启动 tomcat 即可,如下图所示:

42d8d24f93e71f6b10ea0792fccec202.png

项目正常运行会在 webapps 目录下生成同名的文件夹,如下图所示:

1cce392264616e27b8f82b068e45b762.png

完成以上配置,就可以 happy 的访问自己发布的项目了。

可能出现的问题和解决方案

问题一:SpringBoot 配置了端口号影不影响程序发布?

答:不影响,配置的 server.port 会被覆盖,以 tomcat 本身的端口号为准,tomcat 端口号在 tomcat/config/server.xml 文件中配置。

问题二:发布报错,不能找到其他模块或项目中的公共模块,怎么办?

答:因为没有执行父节点 maven 的 install 操作,install 就是把公共模块放入本地仓库,提供给其它项目使用。

问题三:不能找到 SpringBoot 运行的 main 类,怎么办?

答:因为没有设置启动类导致的,设置方式:

  • pom.xml 配置启动类,配置 configuration><mainClass>com.bi.api.ApiApplication</mainClass></configuration>
  • 启动类继承 SpringBootServletInitializer 实现 SpringApplicationBuilder 方法,具体代码参考文中第五部分。

问题四:把 SpringBoot 项目部署到 Tomcat 7 一直提示找不到 xxx.jar 包?

答:这是因为 SpringBoot 版本太高,tomcat 版本太低的原因。如果你使用的是最新版的 SpringBoot,可以考虑把 tomcat 也升级为 tomcat 8.x+ 最新的版本,就可以解决这个问题。

### Spring Boot发布 Web 服务的实现方法 在 Spring Boot发布 Web 服务可以通过多种方式来完成,其中一种常见的方式是基于 Apache CXF 的集成。以下是详细的实现过程: #### 配置依赖项 为了支持 Web Service 功能,在项目的 `pom.xml` 文件中需要引入以下 Maven 依赖项: ```xml <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.4.7</version> </dependency> ``` 此依赖会自动配置 JAX-WS 所需的基础组件。 --- #### 创建 Web Service 接口服务实现类 定义一个标准的 JAX-WS 接口以及其实现类。例如: ##### 定义接口 ```java import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface TeacherService { @WebMethod String getTeacherName(String id); } ``` ##### 实现接口 ```java import javax.jws.WebService; @WebService(endpointInterface = "com.example.TeacherService") public class TeacherServiceImpl implements TeacherService { @Override public String getTeacherName(String id) { return "Teacher Name for ID: " + id; } } ``` --- #### 注册 Web Service 端点 通过创建一个配置类并注册端点,可以将 Web Service 发布到指定路径下: ```java import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import javax.xml.ws.Endpoint; @Component public class PublishWebService { @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public TeacherService teacherService() { return new TeacherServiceImpl(); } @Bean(destroyMethod = "stop") public Server endpoint(Bus bus, TeacherService teacherService) { EndpointImpl endpoint = new EndpointImpl(bus, teacherService); endpoint.publish("/teacher"); return endpoint; } } ``` 在此代码片段中,`/teacher` 是发布的 Web Service 路径[^1]。 --- #### 启动应用程序 启动 Spring Boot 应用程序后,访问 URL 如 `http://localhost:8080/services/teacher?wsdl` 即可获取 WSDL 文档,用于客户端调用该 Web Service。 --- #### 自动化配置选项 如果希望进一步简化配置流程,也可以采用自动化配置方案。例如,使用 Axis2 或其他框架时,可通过自定义 Servlet Listener 来实现类似的发布逻辑[^2]。 --- #### 注意事项 1. **路径冲突**:确保 Web Service 的路径不会与其他 RESTful API 或静态资源路径发生冲突。 2. **安全性**:对于生产环境中的 Web Service,建议启用身份验证机制(如 Basic Auth 或 OAuth),以保护敏感数据的安全性。 3. **性能优化**:针对高并发场景下的 Web Service 请求,考虑调整线程池大小或其他性能参数。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值