开发者社区> 问答> 正文

EDAS 中的 Spring Boot 开发


本文主要针对使用 Spring Boot 开发应用的用户,介绍如何快速让 HSF 在 Spring Boot 中运行,并提供了完整的 Demo下载。

开发在 EDAS 中运行的 Spring Boot 项目


开发 Spring Boot 项目需要使用 Servlet 3.0 及以上版本,同时保持生成环境和 Spring Boot 编译环境 JDK 版本一致。具体开发步骤参见下文。


  1. 创建 Web 项目,引入 Spring Boot 依赖包。
    pom.xml[backcolor=transparent] [backcolor=transparent]<!-- 打包方式由JAR 改为 WAR -->
  2. [backcolor=transparent] [backcolor=transparent]<packaging>[backcolor=transparent]war[backcolor=transparent]</packaging>
  3. [backcolor=transparent] [backcolor=transparent]<!-- 添加 spring-boot-starter-parent 依赖 -->
  4. [backcolor=transparent] [backcolor=transparent]<parent>
  5. [backcolor=transparent]     [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
  6. [backcolor=transparent]     [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-starter-parent[backcolor=transparent]</artifactId>
  7. [backcolor=transparent]     [backcolor=transparent]<version>[backcolor=transparent]1.4.1.RELEASE[backcolor=transparent]</version>
  8. [backcolor=transparent] [backcolor=transparent]</parent>
  9. [backcolor=transparent] [backcolor=transparent]<!-- Spring Boot 依赖,这里必须排除嵌入式的 Tomcat ,否则在 Tomcat4E 会出现问题;若使用 Main 函数启动,则不用排除 -->
  10. [backcolor=transparent] [backcolor=transparent]<dependencies>
  11. [backcolor=transparent]     [backcolor=transparent]<dependency>
  12. [backcolor=transparent]         [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
  13. [backcolor=transparent]         [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-starter-web[backcolor=transparent]</artifactId>
  14. [backcolor=transparent]         [backcolor=transparent]<exclusions>
  15. [backcolor=transparent]             [backcolor=transparent]<exclusion>
  16. [backcolor=transparent]                 [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
  17. [backcolor=transparent]                 [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-starter-tomcat[backcolor=transparent]</artifactId>
  18. [backcolor=transparent]             [backcolor=transparent]</exclusion>
  19. [backcolor=transparent]         [backcolor=transparent]</exclusions>
  20. [backcolor=transparent]     [backcolor=transparent]</dependency>
  21. [backcolor=transparent] [backcolor=transparent]</dependencies>
  22. [backcolor=transparent] [backcolor=transparent]<!-- 添加 Spring Boot 插件 -->
  23. [backcolor=transparent] [backcolor=transparent]<plugins>
  24. [backcolor=transparent]     [backcolor=transparent]<plugin>
  25. [backcolor=transparent]         [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.boot[backcolor=transparent]</groupId>
  26. [backcolor=transparent]         [backcolor=transparent]<artifactId>[backcolor=transparent]spring-boot-maven-plugin[backcolor=transparent]</artifactId>
  27. [backcolor=transparent]     [backcolor=transparent]</plugin>
  28. [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。
  1. [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]{
  2. [backcolor=transparent]     [backcolor=transparent]@Override
  3. [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]{
  4. [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]);
  5. [backcolor=transparent]     [backcolor=transparent]}
  6. [backcolor=transparent] [backcolor=transparent]}

创建 Spring Boot 启动类。
类名:StartupDemoApplication.class
  1. [backcolor=transparent] [backcolor=transparent]@SpringBootApplication
  2. [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]StartupDemoApplication[backcolor=transparent] [backcolor=transparent]{
  3. [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]{
  4. [backcolor=transparent]         [backcolor=transparent]//启动服务
  5. [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]);
  6. [backcolor=transparent]     [backcolor=transparent]}
  7. [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 开发


  1. 添加 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]?>
  2. [backcolor=transparent] [backcolor=transparent]<beans[backcolor=transparent] [backcolor=transparent]xmlns:xsi[backcolor=transparent]=[backcolor=transparent]"http://www.w3.org/2001/XMLSchema-instance"
  3. [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"
  4. [backcolor=transparent]     [backcolor=transparent]xsi:schemaLocation[backcolor=transparent]=[backcolor=transparent]"http://www.springframework.org/schema/beans
  5. [backcolor=transparent]        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. [backcolor=transparent]        http://www.taobao.com/hsf
  7. [backcolor=transparent]        http://www.taobao.com/hsf/hsf.xsd"
  8. [backcolor=transparent]     [backcolor=transparent]default-autowire[backcolor=transparent]=[backcolor=transparent]"byName"[backcolor=transparent]>
  9. [backcolor=transparent]     [backcolor=transparent]<!-- 服务处理 bean -->
  10. [backcolor=transparent]     [backcolor=transparent]<bean[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"memberServiceImpl"
  11. [backcolor=transparent]         [backcolor=transparent]class[backcolor=transparent]=[backcolor=transparent]"com.alibaba.edas.springboot.service.impl.MemberServiceImpl"[backcolor=transparent] [backcolor=transparent]/>
  12. [backcolor=transparent]     [backcolor=transparent]<!-- 提供一个 HSF 服务示例 -->
  13. [backcolor=transparent]     [backcolor=transparent]<hsf:provider[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"memberService"
  14. [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]/>
  15. [backcolor=transparent] [backcolor=transparent]</beans>

导入 HSF 服务提供者配置。
  1. [backcolor=transparent] [backcolor=transparent]@ImportResource[backcolor=transparent]([backcolor=transparent]locations[backcolor=transparent]={[backcolor=transparent]"classpath:hsf-provider-beans.xml"[backcolor=transparent]})
  2. [backcolor=transparent] [backcolor=transparent]@SpringBootApplication
  3. [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]StartupDemoApplication[backcolor=transparent] [backcolor=transparent]{
  4. [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]{
  5. [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]);
  6. [backcolor=transparent]     [backcolor=transparent]}
  7. [backcolor=transparent] [backcolor=transparent]}

打包上传 EDAS ,具体请参考 服务上线
添加 HSF 消费者配置文件(hsf-consumer-beans.xml)。
  1. [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]?>
  2. [backcolor=transparent] [backcolor=transparent]<beans[backcolor=transparent] [backcolor=transparent]xmlns:xsi[backcolor=transparent]=[backcolor=transparent]"http://www.w3.org/2001/XMLSchema-instance"
  3. [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"
  4. [backcolor=transparent]     [backcolor=transparent]xsi:schemaLocation[backcolor=transparent]=[backcolor=transparent]"http://www.springframework.org/schema/beans
  5. [backcolor=transparent]        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. [backcolor=transparent]        http://www.taobao.com/hsf
  7. [backcolor=transparent]        http://www.taobao.com/hsf/hsf.xsd"
  8. [backcolor=transparent]     [backcolor=transparent]default-autowire[backcolor=transparent]=[backcolor=transparent]"byName"[backcolor=transparent]>
  9. [backcolor=transparent]     [backcolor=transparent]<!-- 消费一个 HSF 服务示例 -->
  10. [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]/>
  11. [backcolor=transparent] [backcolor=transparent]</beans>

导入 HSF 消费者配置。
  1. [backcolor=transparent] [backcolor=transparent]@ImportResource[backcolor=transparent]([backcolor=transparent]locations[backcolor=transparent]=[backcolor=transparent]"classpath:hsf-consumer-beans.xml"[backcolor=transparent])
  2. [backcolor=transparent] [backcolor=transparent]@SpringBootApplication
  3. [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]StartupDemoApplication[backcolor=transparent] [backcolor=transparent]{
  4. [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]{
  5. [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]);
  6. [backcolor=transparent]     [backcolor=transparent]}
  7. [backcolor=transparent] [backcolor=transparent]}

打包上传 EDAS ,具体请参考 服务上线

展开
收起
猫饭先生 2017-10-26 10:42:40 5396 0
1 条回答
写回答
取消 提交回答
  • ReEDAS 中的 Spring Boot 开发
    楼主,demo下载地址没提供吗?
    2018-01-05 10:16:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载