EDAS入门-springcloud+HSF开发实战

淩亂°似流年 2022-05-13 13:16 412阅读 0赞
  1. 该系列博文旨在整理阿里云帮助文档中有关EDAS开发相关知识,自己在写学习了解时,个人在理解阿里帮助文档之后,整理代码使其更适合企业级应用,顺便做个备忘录。

前言

  1. 目前 EDAS 已经完全支持 Spring Cloud 应用了,您可以将 Spring Cloud 应用直接部署到 EDAS 中。
  • Spring Boot 提出的概念是 Build Anything,解决繁琐的 xml 配置的问题。
  • Spring Cloud 提出的概念是 Coordinate Anything,通过提供大量的、方便组件接入的 spring-cloud-starter 的支持,简化了分布式微服务的开发。

EDAS 也实现了自己的 Spring Cloud Starter HSF,您同样可以通过 Spring Cloud 来开发 HSF 应用。 本文档将介绍如何使用 Spring Cloud 来开发 HSF 应用。

该文档参考阿里帮助文档:https://help.aliyun.com/document_detail/63867.html?spm=a2c4g.11186623.6.667.1e40cab5VVvbG8

源码地址:https://github.com/feifuzeng/sc-edas-demo

创建POM工程

  1. 创建一个POM工程,取名sc-edas-demo,pom.xml文件如下

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>com.feifz</groupId>
    7. <artifactId>sc-edas</artifactId>
    8. <version>0.0.1-SNAPSHOT</version>
    9. <packaging>pom</packaging>
    10. <name>sc-edas</name>
    11. <description>EDAS 入门 springcloud+HSF</description>
    12. <modules>
    13. <module>sc-edas-api</module>
    14. <module>sc-edas-provider</module>
    15. <module>sc-edas-consumer</module>
    16. </modules>
    17. <parent>
    18. <groupId>org.springframework.boot</groupId>
    19. <artifactId>spring-boot-starter-parent</artifactId>
    20. <version>1.5.8.RELEASE</version>
    21. <relativePath/> <!-- lookup parent from repository -->
    22. </parent>
    23. <properties>
    24. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    25. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    26. <java.version>1.8</java.version>
    27. </properties>
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>
  9. </project>

创建服务提供者及API工程

  1. 创建一个 普通maven工程,命名为 sc-edas-api,在该工程内定义提供者暴露的所有HSF服务,单独发布成jar供调用方使用
  2. 创建创建一个 Spring Cloud 工程,命名为 sc-edas-provider,端口设置为18081
  3. pom.xml 内容如下:

    <?xml version=”1.0” encoding=”UTF-8”?>
    <project xmlns=”http://maven.apache.org/POM/4.0.0“

    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>com.feifz</groupId>
    5. <artifactId>sc-edas-provider</artifactId>
    6. <version>0.0.1-SNAPSHOT</version>
    7. <packaging>jar</packaging>
    8. <name>sc-edas-provider</name>
    9. <description>提供者</description>
    10. <parent>
    11. <groupId>com.feifz</groupId>
    12. <artifactId>sc-edas</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. </parent>
    15. <dependencies>
    16. <!--@Api-->
    17. <dependency>
    18. <groupId>com.feifz</groupId>
    19. <artifactId>sc-edas-api</artifactId>
    20. <version>0.0.1-SNAPSHOT</version>
    21. </dependency>
    22. <dependency>
    23. <groupId>org.springframework.cloud</groupId>
    24. <artifactId>spring-cloud-starter-hsf</artifactId>
    25. <version>1.3</version>
    26. </dependency>
    27. <dependency>
    28. <groupId>org.springframework.cloud</groupId>
    29. <artifactId>spring-cloud-starter-pandora</artifactId>
    30. <version>1.3</version>
    31. </dependency>
    32. <dependency>
    33. <groupId>org.springframework.boot</groupId>
    34. <artifactId>spring-boot-starter-web</artifactId>
    35. </dependency>
    36. </dependencies>
    37. <dependencyManagement>
    38. <dependencies>
    39. <dependency>
    40. <groupId>org.springframework.cloud</groupId>
    41. <artifactId>spring-cloud-dependencies</artifactId>
    42. <version>Dalston.SR4</version>
    43. <type>pom</type>
    44. <scope>import</scope>
    45. </dependency>
    46. </dependencies>
    47. </dependencyManagement>
  1. </project>
  2. 虽然 HSF 服务框架并不依赖于 Web 环境,但是 EDAS 管理应用的生命周期过程中需要使用到 Web 相关的特性,所以需要添加spring-boot-starter-web 的依赖。 如果您的工程不想将 parent 设置为 spring-boot-starter-parent,也可以通过如下方式添加 dependencyManagement ,设置 scope=import ,来达到依赖版本管理的效果。
  3. <dependencyManagement>
  4. <dependencies>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-dependencies</artifactId>
  8. <version>1.5.8.RELEASE</version>
  9. <type>pom</type>
  10. <scope>import</scope>
  11. </dependency>
  12. </dependencies>
  13. </dependencyManagement>
  14. 注意,其中提供者工程里引入了api工程jar包,目的为方便消费者消费时与提供者提供的包路径信息相同。对于api工程中定义的接口具体实现均在提供者工程里进行实现并添加注解进行暴露服务,具体可见提供者工程代码。
  1. 在api工程中定义接口

    1. package com.feifz.scedasapi.api;
    2. /**
    3. * @author feifz
    4. * @version 1.0.0
    5. * @Description 示例接口
    6. * @Date 2018/10/24 15:08
    7. */
    8. public interface HelloService {
    9. public String hello(String str);
    10. }
  1. 在provider工程编写api接口实现

    1. package com.feifz.scedasprovider.service;
    2. import com.alibaba.boot.hsf.annotation.HSFProvider;
    3. import com.feifz.scedasapi.api.HelloService;
    4. /**
    5. * @author feifz
    6. * @version 1.0.0
    7. * @Description TOOD
    8. * @Date 2018/10/24 15:09
    9. */
    10. @HSFProvider(serviceGroup = "sc-edas-provider",serviceInterface = HelloService.class ,serviceVersion = "1.0.0")
    11. public class HelloServiceImpl implements HelloService {
  1. @Override
  2. public String hello(String str) {
  3. return "sc-edas-provider-提供者返回->"+str;
  4. }
  5. }
  1. 在启动类main方法添加PandoraBootstrap启动

    1. package com.feifz.scedasprovider;
    2. import com.taobao.pandora.boot.PandoraBootstrap;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. /**
    6. * 提供者启动类
    7. */
    8. @SpringBootApplication
    9. public class ScEdasProviderApplication {
    10. public static void main(String[] args) {
    11. // 启动 Pandora Boot 用于加载 Pandora 容器
    12. PandoraBootstrap.run(args);
    13. SpringApplication.run(ScEdasProviderApplication.class, args);
    14. // 标记服务启动完成,并设置线程 wait。防止业务代码运行完毕退出后,导致容器退出。
    15. PandoraBootstrap.markStartupAndWait();
    16. }
    17. }

创建消费者工程

  1. 创建消费者springCloud,取名sc-edas-consumer,端口设置为18082
  2. POM.xml内容如下

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>com.feifz</groupId>
    7. <artifactId>sc-edas-consumer</artifactId>
    8. <version>0.0.1-SNAPSHOT</version>
    9. <packaging>jar</packaging>
    10. <name>sc-edas-consumer</name>
    11. <description>消费者</description>
    12. <parent>
    13. <groupId>com.feifz</groupId>
    14. <artifactId>sc-edas</artifactId>
    15. <version>0.0.1-SNAPSHOT</version>
    16. </parent>
    17. <dependencies>
    18. <!--@Api-->
    19. <dependency>
    20. <groupId>com.feifz</groupId>
    21. <artifactId>sc-edas-api</artifactId>
    22. <version>0.0.1-SNAPSHOT</version>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.springframework.cloud</groupId>
    26. <artifactId>spring-cloud-starter-hsf</artifactId>
    27. <version>1.3</version>
    28. </dependency>
    29. <dependency>
    30. <groupId>org.springframework.cloud</groupId>
    31. <artifactId>spring-cloud-starter-pandora</artifactId>
    32. <version>1.3</version>
    33. </dependency>
    34. <dependency>
    35. <groupId>org.springframework.boot</groupId>
    36. <artifactId>spring-boot-starter-web</artifactId>
    37. </dependency>
    38. </dependencies>
    39. <dependencyManagement>
    40. <dependencies>
    41. <dependency>
    42. <groupId>org.springframework.cloud</groupId>
    43. <artifactId>spring-cloud-dependencies</artifactId>
    44. <version>Dalston.SR4</version>
    45. <type>pom</type>
    46. <scope>import</scope>
    47. </dependency>
    48. </dependencies>
    49. </dependencyManagement>
    50. </project>
  1. 编写消费者配置类,通过注解的方式将服务消费者的实例注入到 Spring 的 Context 中。在 Config 类里配置一次 @HSFConsumer ,然后在多处通过 @Autowired 注入使用。通常一个 HSF Consumer 需要在多个地方使用,但并不需要在每次使用的地方都用 @HSFConsumer 来标记。只需要写一个统一的 Config 类,然后在其它需要使用的地方,直接通过 @Autowired 注入即可。

    1. package com.feifz.scedasconsumer;
    2. import com.alibaba.boot.hsf.annotation.HSFConsumer;
    3. import com.feifz.scedasapi.api.HelloService;
    4. import org.springframework.context.annotation.Configuration;
    5. /**
    6. * @author feifz
    7. * @version 1.0.0
    8. * @Description 消费者 配置类
    9. * @Date 2018/10/24 15:20
    10. */
    11. @Configuration
    12. public class HsfConfig {
    13. @HSFConsumer(serviceGroup = "sc-edas-provider",clientTimeout = 3000,serviceVersion = "1.0.0")
    14. private HelloService helloService;
    15. }
  1. 编写control层demo代码

    1. package com.feifz.scedasconsumer.control;
  1. import com.feifz.scedasapi.api.HelloService;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @author feifz
  9. * @version 1.0.0
  10. * @Description 消费者 示例controller
  11. * @Date 2018/10/24 15:29
  12. */
  13. @RestController
  14. public class HelloController {
  15. @Autowired
  16. private HelloService helloService;
  17. @RequestMapping(value = "/hello/{str}",method = RequestMethod.GET)
  18. public String hello(@PathVariable String str){
  19. return helloService.hello(str);
  20. }
  21. }
  22. **注:**在IDEA中编写该control时,IDEA可能会提示Could not autowire. No beans of 'HelloService' type found,可忽略,正常编译运行即可。
  1. 在启动类main方法添加PandoraBootstrap启动

    1. package com.feifz.scedasconsumer;
    2. import com.taobao.pandora.boot.PandoraBootstrap;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. /**
    6. * 消费者 启动类
    7. */
    8. @SpringBootApplication
    9. public class ScEdasConsumerApplication {
    10. public static void main(String[] args) {
    11. // 启动 Pandora Boot 用于加载 Pandora 容器
    12. PandoraBootstrap.run(args);
    13. SpringApplication.run(ScEdasConsumerApplication.class, args);
    14. // 标记服务启动完成,并设置线程 wait。防止业务代码运行完毕退出后,导致容器退出。
    15. PandoraBootstrap.markStartupAndWait();
    16. }
    17. }

启动运行

  1. 依次启动sc-edas-provider工程、sc-edas-consumer工程,访问 http://127.0.0.1:18082/hello/world
  2. 服务返回:sc-edas-provider-提供者返回->world

欢迎关注微信公众号:码仔zonE

2020081910375831.png

发表评论

表情:
评论列表 (有 0 条评论,412人围观)

还没有评论,来说两句吧...

相关阅读

    相关 什么是EDA

    EDA是指数据探索分析(Exploratory Data Analysis)的缩写。它是一种统计分析方法,旨在了解数据的基本特征,并发现数据中的规律和模式。EDA通常是数据分析

    相关 java从入门实战开发

      手记文章   小伙伴们元宵节快乐,记得吃元宵哦~ 在日常开发中,小伙伴们多多少少都有用过 MyBatis 插件,松哥猜测大家用的最多的就是 MyBatis 的分页插件!不