开发者社区> 问答> 正文

基于 Spring Boot 和 Spring Cloud 开发 EDAS 服务实践(3)



异步调用


  • HSF 异步回调支持
    通过实现 HSFResponseCallback 以及在实现类上标注 @AsyncOn 可以很方便地使用 HSF Consumer 的 callback listener。
  1. [backcolor=transparent]    [backcolor=transparent]@AsyncOn[backcolor=transparent]([backcolor=transparent]interfaceName[backcolor=transparent]=[backcolor=transparent]VersionInfoApi[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent]methodName[backcolor=transparent]=[backcolor=transparent]"getVersionInfo"[backcolor=transparent])
  2. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]VersionInfoResponseListener[backcolor=transparent] [backcolor=transparent]implements[backcolor=transparent] [backcolor=transparent]HSFResponseCallback[backcolor=transparent] [backcolor=transparent]{
  3. [backcolor=transparent]        [backcolor=transparent]@Override
  4. [backcolor=transparent]            [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onAppException[backcolor=transparent]([backcolor=transparent]Throwable[backcolor=transparent] t[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  5. [backcolor=transparent]        [backcolor=transparent]}
  6. [backcolor=transparent]        [backcolor=transparent]@Override
  7. [backcolor=transparent]        [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onAppResponse[backcolor=transparent]([backcolor=transparent]Object[backcolor=transparent] appResponse[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  8. [backcolor=transparent]            [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"onAppResponse#####:"[backcolor=transparent]+[backcolor=transparent]appResponse[backcolor=transparent]);
  9. [backcolor=transparent]        [backcolor=transparent]}
  10. [backcolor=transparent]        [backcolor=transparent]@Override
  11. [backcolor=transparent]        [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onHSFException[backcolor=transparent]([backcolor=transparent]HSFException[backcolor=transparent] hsfEx[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  12. [backcolor=transparent]        [backcolor=transparent]}
  13. [backcolor=transparent]    [backcolor=transparent]}

  • HSF Future 调用支持
    通过在 @HSFConsumer 中包含 futureMethods (以 , 分隔)在指定希望哪些方法走 Future 调用。
    1. [backcolor=transparent]   [backcolor=transparent] @HSFConsumer[backcolor=transparent] ([backcolor=transparent] serviceGroup [backcolor=transparent] =[backcolor=transparent] [backcolor=transparent] "HSF"[backcolor=transparent] ,[backcolor=transparent] clientTimeout [backcolor=transparent] =[backcolor=transparent] [backcolor=transparent] 3000[backcolor=transparent] ,[backcolor=transparent] serviceVersion [backcolor=transparent] =[backcolor=transparent] [backcolor=transparent] "1.0.0"[backcolor=transparent] ,[backcolor=transparent] futureMethods [backcolor=transparent] =[backcolor=transparent] [backcolor=transparent] "countVersionInfo"[backcolor=transparent] )
    2. [backcolor=transparent]  [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]VersionInfoApi[backcolor=transparent] versionInfoApi[backcolor=transparent];

    调用执行后,可以通过以下的代码获取到执行结果。更详细的用法请参阅 HSF Future 调用文档
    1. [backcolor=transparent]  [backcolor=transparent]HSFFuture[backcolor=transparent] hsfFuture [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]HSFResponseFuture[backcolor=transparent].[backcolor=transparent]getFuture[backcolor=transparent]();
    2. [backcolor=transparent]  [backcolor=transparent]Object[backcolor=transparent] result [backcolor=transparent]=[backcolor=transparent] future[backcolor=transparent].[backcolor=transparent]getResponse[backcolor=transparent]([backcolor=transparent]5000[backcolor=transparent]);


RESTful 服务开发



全局配置


使用方式:在 application.properties 添加 EDAS 服务的相关配置,样例配置如下:
  1. [backcolor=transparent]    spring[backcolor=transparent].[backcolor=transparent]application[backcolor=transparent].[backcolor=transparent]name[backcolor=transparent]=[backcolor=transparent]spring[backcolor=transparent]-[backcolor=transparent]cloud[backcolor=transparent]-[backcolor=transparent]provider
  2. [backcolor=transparent]    server[backcolor=transparent].[backcolor=transparent]port[backcolor=transparent]=[backcolor=transparent]18080

其他相关参数 (可选)
  1. [backcolor=transparent]    spring[backcolor=transparent].[backcolor=transparent]edas[backcolor=transparent].[backcolor=transparent]dauth[backcolor=transparent].[backcolor=transparent]enable[backcolor=transparent]=[backcolor=transparent]true[backcolor=transparent] [backcolor=transparent]#接口是否开启鉴权,默认为true
  2. [backcolor=transparent]    spring[backcolor=transparent].[backcolor=transparent]edas[backcolor=transparent].[backcolor=transparent]dauth[backcolor=transparent].[backcolor=transparent]whitelist[backcolor=transparent].[backcolor=transparent]paths[backcolor=transparent]=/[backcolor=transparent]appwl [backcolor=transparent]#白名单,不做鉴权的路径,只判断第一层路径
  3. [backcolor=transparent]    spring[backcolor=transparent].[backcolor=transparent]sentinel[backcolor=transparent].[backcolor=transparent]filter[backcolor=transparent].[backcolor=transparent]urlPatterns[backcolor=transparent]=[backcolor=transparent]/* #需要进行限流的url,这里添加所有都进行限流
  4. [backcolor=transparent]    vipserver.register.doms=my-provider,sc-provider,service-name #vipserver注册的服务名
  5. [backcolor=transparent]    vipserver.register.enabled=false #关闭服务注册的功能

[backcolor=transparent]说明:
spring.edas.dauth.enable: 鉴权相关的配置,默认为开启,可不填。
spring.sentinel.filter.urlPatterns:限流降级相关的配置,在这里可以指定需要监控的URL后,可以通过EDAS控制台对这些URL进行限流降级规则配置。
vipserver.register.doms:服务名的配置,用英文逗号隔开。样例中的三个服务名是对等的关系,订阅者可以使用其中任意一个调用。若不填写,默认为应用名称。
vipserver.register.enabled:服务发布的开关,如果此工程只有服务消费者,没有服务提供者,应该将此值设置为false。

服务端启动

  1. [backcolor=transparent]    [backcolor=transparent]@EnableDiscoveryClient
  2. [backcolor=transparent]    [backcolor=transparent]@SpringBootApplication
  3. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]ProviderApplication[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]// 启动Pandora Boot 用于加载 Pandora 容器
  6. [backcolor=transparent]            [backcolor=transparent]PandoraBootstrap[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]args[backcolor=transparent]);
  7. [backcolor=transparent]            [backcolor=transparent]// 启动Spring Boot
  8. [backcolor=transparent]            [backcolor=transparent]SpringApplication[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]ProviderApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent] args[backcolor=transparent]);
  9. [backcolor=transparent]            [backcolor=transparent]// 标记服务启动完成,并设置线程 wait。防止用户业务代码运行完毕退出后,导致容器退出。
  10. [backcolor=transparent]            [backcolor=transparent]PandoraBootstrap[backcolor=transparent].[backcolor=transparent]markStartupAndWait[backcolor=transparent]();
  11. [backcolor=transparent]        [backcolor=transparent]}
  12. [backcolor=transparent]    [backcolor=transparent]}


消费端启动

  1. [backcolor=transparent]    [backcolor=transparent]@EnableDiscoveryClient
  2. [backcolor=transparent]    [backcolor=transparent]@EnableFeignClients
  3. [backcolor=transparent]    [backcolor=transparent]@SpringBootApplication
  4. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]ConsumerApplication[backcolor=transparent] [backcolor=transparent]{
  5. [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]{
  6. [backcolor=transparent]            [backcolor=transparent]PandoraBootstrap[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]args[backcolor=transparent]);
  7. [backcolor=transparent]            [backcolor=transparent]SpringApplication[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]ConsumerApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent] args[backcolor=transparent]);
  8. [backcolor=transparent]            [backcolor=transparent]PandoraBootstrap[backcolor=transparent].[backcolor=transparent]markStartupAndWait[backcolor=transparent]();
  9. [backcolor=transparent]        [backcolor=transparent]}
  10. [backcolor=transparent]    [backcolor=transparent]}

可以看出,传统的 Spring Cloud 应用部署到 EDAS 上在代码层面只需要做一点小的修改,在SpringApplication.run()前后加上PandoraBootStrap启动相关的动作即可。

服务发布


首先使用注解@EnableDiscoveryClient激活服务发布和订阅的功能,发布的服务名在application.properties中配置
  1. [backcolor=transparent]vipserver[backcolor=transparent].[backcolor=transparent]register[backcolor=transparent].[backcolor=transparent]doms[backcolor=transparent]=[backcolor=transparent]my[backcolor=transparent]-[backcolor=transparent]provider[backcolor=transparent],[backcolor=transparent]sc[backcolor=transparent]-[backcolor=transparent]provider[backcolor=transparent],[backcolor=transparent]service[backcolor=transparent]-[backcolor=transparent]name

注意@EnableDiscoveryClient既是服务提供者进行发布的开关,也是服务消费者订阅的开关。如果此工程只有服务消费者,没有服务提供者,应该在application.properties配置文件中将发布功能关闭。
  1. [backcolor=transparent]vipserver[backcolor=transparent].[backcolor=transparent]register[backcolor=transparent].[backcolor=transparent]enabled[backcolor=transparent]=[backcolor=transparent]false


服务订阅



服务发现


VipServerClient已经自动集成到FeignClient、RestTemplate和AsyncRestTemplate中,自动实现了服务列表维护和自动更新等功能,使用者无需关心,直接使用即可。

服务消费


  • FeignClient模式
    在FeignClient中,如果想使用某服务,只需在@FeignClient的name字段指定服务名即可。
    1. [backcolor=transparent]   [backcolor=transparent] @FeignClient[backcolor=transparent] ([backcolor=transparent] name [backcolor=transparent] =[backcolor=transparent] [backcolor=transparent] "my-provider"[backcolor=transparent] )
    2. [backcolor=transparent]  [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]interface[backcolor=transparent] [backcolor=transparent]IDemoApi[backcolor=transparent] [backcolor=transparent]{
    3. [backcolor=transparent]      [backcolor=transparent]@RequestMapping[backcolor=transparent]([backcolor=transparent]value [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"/test"[backcolor=transparent],[backcolor=transparent] method [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]RequestMethod[backcolor=transparent].[backcolor=transparent]GET[backcolor=transparent])
    4. [backcolor=transparent]      [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] echo[backcolor=transparent]([backcolor=transparent]@RequestParam[backcolor=transparent]([backcolor=transparent]"content"[backcolor=transparent])[backcolor=transparent]String[backcolor=transparent] content[backcolor=transparent]);
    5. [backcolor=transparent]      [backcolor=transparent]@RequestMapping[backcolor=transparent]([backcolor=transparent]value [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"/test"[backcolor=transparent],[backcolor=transparent] method [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]RequestMethod[backcolor=transparent].[backcolor=transparent]POST[backcolor=transparent])
    6. [backcolor=transparent]      [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] pay[backcolor=transparent]();
    7. [backcolor=transparent]  [backcolor=transparent]}

    注意此name应该为vipserver.register.doms中的一个,其他配置与原生的 Spring Cloud 服务无差别。

  • RestTemplate模式
    使用RestTemplate 访问微服务
    直接将域名改为服务名即可
    1. [backcolor=transparent]       [backcolor=transparent] @RequestMapping[backcolor=transparent] ([backcolor=transparent] method [backcolor=transparent] =[backcolor=transparent] [backcolor=transparent] RequestMethod[backcolor=transparent] .[backcolor=transparent] GET[backcolor=transparent] )
    2. [backcolor=transparent]      [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] consumerEchoMethod[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] content[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
    3. [backcolor=transparent]          [backcolor=transparent]String[backcolor=transparent] msg [backcolor=transparent]=[backcolor=transparent] restTemplate[backcolor=transparent].[backcolor=transparent]getForObject[backcolor=transparent]([backcolor=transparent]"http://my-provider/test?content="[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] content[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent]);
    4. [backcolor=transparent]          LOGGER[backcolor=transparent].[backcolor=transparent]info[backcolor=transparent]([backcolor=transparent]"returnMsg:"[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] msg [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]",content:"[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] content[backcolor=transparent]);
    5. [backcolor=transparent]          [backcolor=transparent]return[backcolor=transparent] msg[backcolor=transparent];
    6. [backcolor=transparent]      [backcolor=transparent]}


部署



本地部署


本地部署时,必须先启动轻量级配置中心,同时,将hosts中的jmenv.tbsite.net地址,设为轻量级配置中心所在的ip地址,详情见 安装轻量配置中心

  • 使用[backcolor=transparent]本地轻量级配置中心时,[backcolor=transparent]必须通过在启动时添加JVM参数来指定如下配置:-Daddress.server.domain=127.0.0.1 (轻量级配置中心的ip地址)
  • -Daddress.server.port=8080 (地址服务器端口)
  • -Dvipserver.client.port=8080 (Vipserver服务的监听端口)

  • 在 IDE 中,通过 main 方法直接启动。

  • 本地打包 FatJar ,通过 JAVA 命令启动。
    排除 taobao-hsf.sar 依赖启动方式(加入-D 指定 SAR 位置)
    1. [backcolor=transparent]   [backcolor=transparent] #java -jar -Dpandora.location=/Users/yizhan/.m2/repository/com/taobao/pandora/taobao-hsf.sar/dev.3.5/taobao-hsf.sar-dev.3.5.jar spring-cloud-provider-0.0.1-SNAPSHOT.jar

    [backcolor=transparent]注意 : -Dpandora.location 指定的路径必须是全路径

  • 不排除 taobao-hsf.sar 依赖启动方式(通过插件设置)通过 pandora-boot-maven-plugin 插件,把 excludeSar 设置为 false ,默认是 true ,打包时就会自动包含该 SAR 包。
    1. [backcolor=transparent]   [backcolor=transparent] <plugin>
    2. [backcolor=transparent]    [backcolor=transparent]<groupId>[backcolor=transparent]com.taobao.pandora[backcolor=transparent]</groupId>
    3. [backcolor=transparent]    [backcolor=transparent]<artifactId>[backcolor=transparent]pandora-boot-maven-plugin[backcolor=transparent]</artifactId>
    4. [backcolor=transparent]    [backcolor=transparent]<version>[backcolor=transparent]2.1.6.3[backcolor=transparent]</version>
    5. [backcolor=transparent]    [backcolor=transparent]<configuration>
    6. [backcolor=transparent]      [backcolor=transparent]<excludeSar>[backcolor=transparent]false[backcolor=transparent]</excludeSar>
    7. [backcolor=transparent]   [backcolor=transparent]</configuration>
    8. [backcolor=transparent]    [backcolor=transparent]<executions>
    9. [backcolor=transparent]        [backcolor=transparent]<execution>
    10. [backcolor=transparent]            [backcolor=transparent]<phase>[backcolor=transparent]package[backcolor=transparent]</phase>
    11. [backcolor=transparent]            [backcolor=transparent]<goals>
    12. [backcolor=transparent]                [backcolor=transparent]<goal>[backcolor=transparent]repackage[backcolor=transparent]</goal>
    13. [backcolor=transparent]            [backcolor=transparent]</goals>
    14. [backcolor=transparent]        [backcolor=transparent]</execution>
    15. [backcolor=transparent]    [backcolor=transparent]</executions>
    16. [backcolor=transparent]  [backcolor=transparent]</plugin>

    直接启动
    1. [backcolor=transparent]  java [backcolor=transparent]-[backcolor=transparent]jar spring[backcolor=transparent]-[backcolor=transparent]cloud[backcolor=transparent]-[backcolor=transparent]provider[backcolor=transparent]-[backcolor=transparent]0.0[backcolor=transparent].[backcolor=transparent]1[backcolor=transparent]-[backcolor=transparent]SNAPSHOT[backcolor=transparent].[backcolor=transparent]jar


EDAS 中部署


在应用管理中,创建的应用需要选择支持 FatJar 功能的容器(3.2.5及以上)。选择部署应用,上传 FatJar 应用即可部署。那么以后默认该应用只能上传 FatJar 了,不再支持 WAR 。

展开
收起
猫饭先生 2017-10-26 10:44:07 4107 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

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