EDAS入门-springcloud+HSF开发实战

        该系列博文旨在整理阿里云帮助文档中有关EDAS开发相关知识,自己在写学习了解时,个人在理解阿里帮助文档之后,整理代码使其更适合企业级应用,顺便做个备忘录。

前言

      目前 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文件如下
    
    
        4.0.0
    
        com.feifz
        sc-edas
        0.0.1-SNAPSHOT
        pom
    
        sc-edas
        EDAS 入门 springcloud+HSF
    
        
            sc-edas-api
            sc-edas-provider
            sc-edas-consumer
        
        
            org.springframework.boot
            spring-boot-starter-parent
            1.5.8.RELEASE
             
        
    
        
            UTF-8
            UTF-8
            1.8
        
    
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
    

     

创建服务提供者及API工程

  1. 创建一个 普通maven工程,命名为 sc-edas-api,在该工程内定义提供者暴露的所有HSF服务,单独发布成jar供调用方使用
  2. 创建创建一个 Spring Cloud 工程,命名为 sc-edas-provider,端口设置为18081
  3.  pom.xml 内容如下:
    
    
        4.0.0
    
        com.feifz
        sc-edas-provider
        0.0.1-SNAPSHOT
        jar
    
        sc-edas-provider
        提供者
    
        
            com.feifz
            sc-edas
            0.0.1-SNAPSHOT
        
    
        
            
            
                com.feifz
                sc-edas-api
                0.0.1-SNAPSHOT
            
    
            
                org.springframework.cloud
                spring-cloud-starter-hsf
                1.3
            
            
                org.springframework.cloud
                spring-cloud-starter-pandora
                1.3
            
            
                org.springframework.boot
                spring-boot-starter-web
            
    
        
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    Dalston.SR4
                    pom
                    import
                
            
        
    
    
    
    

    虽然 HSF 服务框架并不依赖于 Web 环境,但是 EDAS 管理应用的生命周期过程中需要使用到 Web 相关的特性,所以需要添加spring-boot-starter-web 的依赖。 如果您的工程不想将 parent 设置为 spring-boot-starter-parent,也可以通过如下方式添加 dependencyManagement ,设置 scope=import ,来达到依赖版本管理的效果。

     
         
                       
                 org.springframework.boot
                 spring-boot-dependencies
                 1.5.8.RELEASE
                 pom
                 import
             
         
     

    注意,其中提供者工程里引入了api工程jar包,目的为方便消费者消费时与提供者提供的包路径信息相同。对于api工程中定义的接口具体实现均在提供者工程里进行实现并添加注解进行暴露服务,具体可见提供者工程代码。

  4. 在api工程中定义接口

    package com.feifz.scedasapi.api;
    
    /**
     * @author feifz
     * @version 1.0.0
     * @Description 示例接口
     * @Date 2018/10/24 15:08
     */
    public interface HelloService {
    
        public String hello(String str);
    }
    

     

  5. 在provider工程编写api接口实现

    package com.feifz.scedasprovider.service;
    
    import com.alibaba.boot.hsf.annotation.HSFProvider;
    import com.feifz.scedasapi.api.HelloService;
    
    /**
     * @author feifz
     * @version 1.0.0
     * @Description TOOD
     * @Date 2018/10/24 15:09
     */
    @HSFProvider(serviceGroup = "sc-edas-provider",serviceInterface = HelloService.class ,serviceVersion = "1.0.0")
    public class HelloServiceImpl implements HelloService {
    
    
        @Override
        public String hello(String str) {
            return "sc-edas-provider-提供者返回->"+str;
        }
    }
    

     

  6. 在启动类main方法添加PandoraBootstrap启动

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

     

创建消费者工程

  1. 创建消费者springCloud,取名sc-edas-consumer,端口设置为18082
  2. POM.xml内容如下
    
    
        4.0.0
    
        com.feifz
        sc-edas-consumer
        0.0.1-SNAPSHOT
        jar
    
        sc-edas-consumer
        消费者
    
        
            com.feifz
            sc-edas
            0.0.1-SNAPSHOT
        
        
            
            
                com.feifz
                sc-edas-api
                0.0.1-SNAPSHOT
            
    
            
                org.springframework.cloud
                spring-cloud-starter-hsf
                1.3
            
            
                org.springframework.cloud
                spring-cloud-starter-pandora
                1.3
            
            
                org.springframework.boot
                spring-boot-starter-web
            
        
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    Dalston.SR4
                    pom
                    import
                
            
        
    
    

     

  3. 编写消费者配置类,通过注解的方式将服务消费者的实例注入到 Spring 的 Context 中。在 Config 类里配置一次 @HSFConsumer ,然后在多处通过 @Autowired 注入使用。通常一个 HSF Consumer 需要在多个地方使用,但并不需要在每次使用的地方都用 @HSFConsumer 来标记。只需要写一个统一的 Config 类,然后在其它需要使用的地方,直接通过 @Autowired 注入即可。
    package com.feifz.scedasconsumer;
    
    import com.alibaba.boot.hsf.annotation.HSFConsumer;
    import com.feifz.scedasapi.api.HelloService;
    
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author feifz
     * @version 1.0.0
     * @Description 消费者 配置类
     * @Date 2018/10/24 15:20
     */
    @Configuration
    public class HsfConfig {
    
        @HSFConsumer(serviceGroup = "sc-edas-provider",clientTimeout = 3000,serviceVersion = "1.0.0")
        private HelloService helloService;
    }
    

     

  4. 编写control层demo代码
    package com.feifz.scedasconsumer.control;
    
    
    import com.feifz.scedasapi.api.HelloService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author feifz
     * @version 1.0.0
     * @Description 消费者 示例controller
     * @Date 2018/10/24 15:29
     */
    @RestController
    public class HelloController {
    
        @Autowired
        private HelloService helloService;
    
        @RequestMapping(value = "/hello/{str}",method = RequestMethod.GET)
        public String hello(@PathVariable String str){
            return helloService.hello(str);
        }
    }
    

    注:在IDEA中编写该control时,IDEA可能会提示Could not autowire. No beans of 'HelloService' type found,可忽略,正常编译运行即可。

  5. 在启动类main方法添加PandoraBootstrap启动
    package com.feifz.scedasconsumer;
    
    import com.taobao.pandora.boot.PandoraBootstrap;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * 消费者 启动类
     */
    @SpringBootApplication
    public class ScEdasConsumerApplication {
    
        public static void main(String[] args) {
            // 启动 Pandora Boot 用于加载 Pandora 容器
            PandoraBootstrap.run(args);
            SpringApplication.run(ScEdasConsumerApplication.class, args);
            // 标记服务启动完成,并设置线程 wait。防止业务代码运行完毕退出后,导致容器退出。
            PandoraBootstrap.markStartupAndWait();
        }
    }
    

     

启动运行

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

你可能感兴趣的:(springcloud)