本文主要针对使用 Spring Boot 开发应用的用户,介绍如何快速让 HSF 在 Spring Boot 中运行,并提供了完整的 Demo下载。
开发在 EDAS 中运行的 Spring Boot 项目
开发 Spring Boot 项目需要使用 Servlet 3.0 及以上版本,同时保持生成环境和 Spring Boot 编译环境 JDK 版本一致。具体开发步骤参见下文。
创建 Web 项目,引入 Spring Boot 依赖包。
pom.xml[backcolor=transparent] [backcolor=transparent]<!-- 打包方式由JAR 改为 WAR -->- [backcolor=transparent] [backcolor=transparent]<packaging>[backcolor=transparent]war[backcolor=transparent]</packaging>
- [backcolor=transparent] [backcolor=transparent]<!-- 添加 spring-boot-starter-parent 依赖 -->
- [backcolor=transparent] [backcolor=transparent]<parent>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-starter-parent[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]<version>[backcolor=transparent]1.4.1.RELEASE[backcolor=transparent]</version>
- [backcolor=transparent] [backcolor=transparent]</parent>
- [backcolor=transparent] [backcolor=transparent]<!-- Spring Boot 依赖,这里必须排除嵌入式的 Tomcat ,否则在 Tomcat4E 会出现问题;若使用 Main 函数启动,则不用排除 -->
- [backcolor=transparent] [backcolor=transparent]<dependencies>
- [backcolor=transparent] [backcolor=transparent]<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-starter-web[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]<exclusions>
- [backcolor=transparent] [backcolor=transparent]<exclusion>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-starter-tomcat[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]</exclusion>
- [backcolor=transparent] [backcolor=transparent]</exclusions>
- [backcolor=transparent] [backcolor=transparent]</dependency>
- [backcolor=transparent] [backcolor=transparent]</dependencies>
- [backcolor=transparent] [backcolor=transparent]<!-- 添加 Spring Boot 插件 -->
- [backcolor=transparent] [backcolor=transparent]<plugins>
- [backcolor=transparent] [backcolor=transparent]<plugin>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-maven-plugin[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]</plugin>
- [backcolor=transparent] [backcolor=transparent]</plugins>
[backcolor=transparent]注意:
在 pom.xml 里面引入的 Spring Boot 依赖,若要以 WAR 包形式在 Tomcat 中运行,需要排除 spring-boot-starter-web 嵌入式 Tomcat 依赖,否则会和 Tomcat 默认的 lib 内容形成冲突,导致项目无法启动。如果在 Main 函数中启动则不用排除嵌入式 Tomcat,但是在打包发布时,请一定记住需要排除。
创建 ServletInitializer 类,继承 SpringBootServletInitializer。
类似初始化 Spring 上下文,标记该项目打包成 WAR 项目在 Tomcat 中运行。
继承 SpringBootServletInitializer 类,实现 configure 方法 ServletInitializer.class。
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]ServletInitializer[backcolor=transparent] [backcolor=transparent]extends[backcolor=transparent] [backcolor=transparent]SpringBootServletInitializer[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]protected[backcolor=transparent] [backcolor=transparent]SpringApplicationBuilder[backcolor=transparent] configure[backcolor=transparent]([backcolor=transparent]SpringApplicationBuilder[backcolor=transparent] application[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] application[backcolor=transparent].[backcolor=transparent]sources[backcolor=transparent]([backcolor=transparent]StartupDemoApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
创建 Spring Boot 启动类。
类名:StartupDemoApplication.class
- [backcolor=transparent] [backcolor=transparent]@SpringBootApplication
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]StartupDemoApplication[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]//启动服务
- [backcolor=transparent] [backcolor=transparent]SpringApplication[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]StartupDemoApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent] args[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
删除 webapp 下面的 web.xml 文件。
若该文件存在,在容器加载 Web 的时候,就会直接加载该配置文件,但是由于该配置文件没有做任何配置,因此会造成 Spring Boot 加载不成功。
[backcolor=transparent]排除方式:
若发现发布在 EDAS 中的 WAR 没有执行您自己的代码,那么有可能就是该问题导致,请及时检查是否存在 web.xml 文件。
打包 WAR,上传 EDAS 启动即可。
如果测试阶段,需要直接在 Main 方法中运行,请把 pom.xml 文件中的 spring-boot-starter-tomcat 排除取消。
Spring Boot 结合 HSF 开发
添加 HSF 服务提供者配置文件(hsf-provider-beans.xml)。[backcolor=transparent] [backcolor=transparent]<?[backcolor=transparent]xml version[backcolor=transparent]=[backcolor=transparent]"1.0"[backcolor=transparent] encoding[backcolor=transparent]=[backcolor=transparent]"UTF-8"[backcolor=transparent]?>- [backcolor=transparent] [backcolor=transparent]<beans[backcolor=transparent] [backcolor=transparent]xmlns:xsi[backcolor=transparent]=[backcolor=transparent]"http://www.w3.org/2001/XMLSchema-instance"
- [backcolor=transparent] [backcolor=transparent]xmlns:hsf[backcolor=transparent]=[backcolor=transparent]"http://www.taobao.com/hsf"[backcolor=transparent] [backcolor=transparent]xmlns[backcolor=transparent]=[backcolor=transparent]"http://www.springframework.org/schema/beans"
- [backcolor=transparent] [backcolor=transparent]xsi:schemaLocation[backcolor=transparent]=[backcolor=transparent]"http://www.springframework.org/schema/beans
- [backcolor=transparent] http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- [backcolor=transparent] http://www.taobao.com/hsf
- [backcolor=transparent] http://www.taobao.com/hsf/hsf.xsd"
- [backcolor=transparent] [backcolor=transparent]default-autowire[backcolor=transparent]=[backcolor=transparent]"byName"[backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<!-- 服务处理 bean -->
- [backcolor=transparent] [backcolor=transparent]<bean[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"memberServiceImpl"
- [backcolor=transparent] [backcolor=transparent]class[backcolor=transparent]=[backcolor=transparent]"com.alibaba.edas.springboot.service.impl.MemberServiceImpl"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]<!-- 提供一个 HSF 服务示例 -->
- [backcolor=transparent] [backcolor=transparent]<hsf:provider[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"memberService"
- [backcolor=transparent] [backcolor=transparent]interface[backcolor=transparent]=[backcolor=transparent]"com.alibaba.edas.springboot.service.MemberService"[backcolor=transparent] [backcolor=transparent]ref[backcolor=transparent]=[backcolor=transparent]"memberServiceImpl"[backcolor=transparent] [backcolor=transparent]group[backcolor=transparent]=[backcolor=transparent]"test"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]</beans>
导入 HSF 服务提供者配置。
- [backcolor=transparent] [backcolor=transparent]@ImportResource[backcolor=transparent]([backcolor=transparent]locations[backcolor=transparent]={[backcolor=transparent]"classpath:hsf-provider-beans.xml"[backcolor=transparent]})
- [backcolor=transparent] [backcolor=transparent]@SpringBootApplication
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]StartupDemoApplication[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]SpringApplication[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]StartupDemoApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent] args[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
打包上传 EDAS ,具体请参考
服务上线。
添加 HSF 消费者配置文件(hsf-consumer-beans.xml)。
- [backcolor=transparent] [backcolor=transparent]<?[backcolor=transparent]xml version[backcolor=transparent]=[backcolor=transparent]"1.0"[backcolor=transparent] encoding[backcolor=transparent]=[backcolor=transparent]"UTF-8"[backcolor=transparent]?>
- [backcolor=transparent] [backcolor=transparent]<beans[backcolor=transparent] [backcolor=transparent]xmlns:xsi[backcolor=transparent]=[backcolor=transparent]"http://www.w3.org/2001/XMLSchema-instance"
- [backcolor=transparent] [backcolor=transparent]xmlns:hsf[backcolor=transparent]=[backcolor=transparent]"http://www.taobao.com/hsf"[backcolor=transparent] [backcolor=transparent]xmlns[backcolor=transparent]=[backcolor=transparent]"http://www.springframework.org/schema/beans"
- [backcolor=transparent] [backcolor=transparent]xsi:schemaLocation[backcolor=transparent]=[backcolor=transparent]"http://www.springframework.org/schema/beans
- [backcolor=transparent] http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- [backcolor=transparent] http://www.taobao.com/hsf
- [backcolor=transparent] http://www.taobao.com/hsf/hsf.xsd"
- [backcolor=transparent] [backcolor=transparent]default-autowire[backcolor=transparent]=[backcolor=transparent]"byName"[backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<!-- 消费一个 HSF 服务示例 -->
- [backcolor=transparent] [backcolor=transparent]<hsf:consumer[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"memberService"[backcolor=transparent] [backcolor=transparent]interface[backcolor=transparent]=[backcolor=transparent]"com.alibaba.edas.springboot.service.MemberService"[backcolor=transparent] [backcolor=transparent]group[backcolor=transparent]=[backcolor=transparent]"test"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]</beans>
导入 HSF 消费者配置。
- [backcolor=transparent] [backcolor=transparent]@ImportResource[backcolor=transparent]([backcolor=transparent]locations[backcolor=transparent]=[backcolor=transparent]"classpath:hsf-consumer-beans.xml"[backcolor=transparent])
- [backcolor=transparent] [backcolor=transparent]@SpringBootApplication
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]StartupDemoApplication[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]SpringApplication[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]StartupDemoApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent] args[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
打包上传 EDAS ,具体请参考
服务上线。