阅读243 返回首页    go iPhone_iPad_Mac_手机_平板_苹果apple


EDAS 中的 Spring Boot 开发__服务开发_开发者指南_企业级分布式应用服务 EDAS-阿里云

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

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

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

  1. 创建 Web 项目,引入 Spring Boot 依赖包。

    pom.xml

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

    注意:

    在 pom.xml 里面引入的 Spring Boot 依赖,若要以 WAR 包形式在 Tomcat 中运行,需要排除 spring-boot-starter-web 嵌入式 Tomcat 依赖,否则会和 Tomcat 默认的 lib 内容形成冲突,导致项目无法启动。如果在 Main 函数中启动则不用排除嵌入式 Tomcat,但是在打包发布时,请一定记住需要排除。

  2. 创建 ServletInitializer 类,继承 SpringBootServletInitializer。

    类似初始化 Spring 上下文,标记该项目打包成 WAR 项目在 Tomcat 中运行。

    继承 SpringBootServletInitializer 类,实现 configure 方法 ServletInitializer.class。

    1. public class ServletInitializer extends SpringBootServletInitializer {
    2. @Override
    3. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    4. return application.sources(StartupDemoApplication.class);
    5. }
    6. }
  3. 创建 Spring Boot 启动类。

    类名:StartupDemoApplication.class

    1. @SpringBootApplication
    2. public class StartupDemoApplication {
    3. public static void main(String[] args) {
    4. //启动服务
    5. SpringApplication.run(StartupDemoApplication.class, args);
    6. }
    7. }
  4. 删除 webapp 下面的 web.xml 文件。

    若该文件存在,在容器加载 Web 的时候,就会直接加载该配置文件,但是由于该配置文件没有做任何配置,因此会造成 Spring Boot 加载不成功。

    排除方式:

    若发现发布在 EDAS 中的 WAR 没有执行您自己的代码,那么有可能就是该问题导致,请及时检查是否存在 web.xml 文件。

  5. 打包 WAR,上传 EDAS 启动即可。

    如果测试阶段,需要直接在 Main 方法中运行,请把 pom.xml 文件中的 spring-boot-starter-tomcat 排除取消。

Spring Boot 结合 HSF 开发

  1. 添加 HSF 服务提供者配置文件(hsf-provider-beans.xml)。

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:hsf="https://www.taobao.com/hsf" xmlns="https://www.springframework.org/schema/beans"
    4. xsi:schemaLocation="https://www.springframework.org/schema/beans
    5. https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. https://www.taobao.com/hsf
    7. https://www.taobao.com/hsf/hsf.xsd"
    8. default-autowire="byName">
    9. <!-- 服务处理 bean -->
    10. <bean id="memberServiceImpl"
    11. class="com.alibaba.edas.springboot.service.impl.MemberServiceImpl" />
    12. <!-- 提供一个 HSF 服务示例 -->
    13. <hsf:provider id="memberService"
    14. interface="com.alibaba.edas.springboot.service.MemberService" ref="memberServiceImpl" group="test" />
    15. </beans>
  2. 导入 HSF 服务提供者配置。

    1. @ImportResource(locations={"classpath:hsf-provider-beans.xml"})
    2. @SpringBootApplication
    3. public class StartupDemoApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(StartupDemoApplication.class, args);
    6. }
    7. }
  3. 打包上传 EDAS ,具体请参考服务上线

  4. 添加 HSF 消费者配置文件(hsf-consumer-beans.xml)。

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:hsf="https://www.taobao.com/hsf" xmlns="https://www.springframework.org/schema/beans"
    4. xsi:schemaLocation="https://www.springframework.org/schema/beans
    5. https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. https://www.taobao.com/hsf
    7. https://www.taobao.com/hsf/hsf.xsd"
    8. default-autowire="byName">
    9. <!-- 消费一个 HSF 服务示例 -->
    10. <hsf:consumer id="memberService" interface="com.alibaba.edas.springboot.service.MemberService" group="test" />
    11. </beans>
  5. 导入 HSF 消费者配置。

    1. @ImportResource(locations="classpath:hsf-consumer-beans.xml")
    2. @SpringBootApplication
    3. public class StartupDemoApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(StartupDemoApplication.class, args);
    6. }
    7. }
  6. 打包上传 EDAS ,具体请参考服务上线

最后更新:2016-11-23 16:04:20

  上一篇:go HSF 单元测试__服务开发_开发者指南_企业级分布式应用服务 EDAS-阿里云
  下一篇:go 服务打包__线上服务发布_开发者指南_企业级分布式应用服务 EDAS-阿里云