消费者的订阅从代码编写的角度分为两个部分:首先 Spring 的配置文件使用标签<hsf:consumer/>定义好一个 Bean;然后在使用的时候从 Spring 的 context 中将 Bean 取出来即可。Demo 工程的 detail 为消费者的示例,示例代码说明如下。
消费者配置
与生产者一样消费者的配置文件分为 Maven 依赖配置与 Spring 的配置,且 Maven 的依赖与生产者依赖一样,详情请参考文档
生产者实现服务 的 [backcolor=transparent]服务配置 小节。
除了必要的 Spring 所定义的配置之外,还需要在 Spring 的配置文件中增加消费者的定义,HSF 框架将根据该配置文件去服务中心订阅所需的服务,配置文件
/resource/hsf-consumer-beans.xml 内容与释义如下:
- [backcolor=transparent]<?[backcolor=transparent]xml version[backcolor=transparent]=[backcolor=transparent]"1.0"[backcolor=transparent] encoding[backcolor=transparent]=[backcolor=transparent]"UTF-8"[backcolor=transparent]?>
- [backcolor=transparent]<beans[backcolor=transparent] [backcolor=transparent]xmlns:xsi[backcolor=transparent]=[backcolor=transparent]"http://www.w3.org/2001/XMLSchema-instance"
- [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"
- [backcolor=transparent] [backcolor=transparent]xsi:schemaLocation[backcolor=transparent]=[backcolor=transparent]"http://www.springframework.org/schema/beans
- [backcolor=transparent] http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- [backcolor=transparent] http://www.taobao.com/hsf
- [backcolor=transparent] http://www.taobao.com/hsf/hsf.xsd"[backcolor=transparent] [backcolor=transparent]default-autowire[backcolor=transparent]=[backcolor=transparent]"byName"[backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<!-- 消费一个服务示例 -->
- [backcolor=transparent] <hsf:consumer
- [backcolor=transparent] [backcolor=transparent]<!-- Bean ID,在代码中可根据此ID进行注入并使用 -->
- [backcolor=transparent] id="item"
- [backcolor=transparent] [backcolor=transparent]<!-- 服务名,与服务提供者的相应配置对应,HSF 将根据 interface + version + group 查询并订阅所需服务 -->
- [backcolor=transparent] interface="com.alibaba.edas.carshop.itemcenter.ItemService"
- [backcolor=transparent] [backcolor=transparent]<!-- 版本号,与服务提供者的相应配置对应,HSF 将根据 interface + version + group 查询并订阅所需服务 -->
- [backcolor=transparent] version="1.0.0"
- [backcolor=transparent] [backcolor=transparent]<!-- 分组名 -->
- [backcolor=transparent] group="testHSFGroup">
- [backcolor=transparent] [backcolor=transparent]</hsf:consumer>
- [backcolor=transparent]</beans>
消费者使用服务
Demo 中的示例代码如下:
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]StartListener[backcolor=transparent] [backcolor=transparent]implements[backcolor=transparent] [backcolor=transparent]ServletContextListener[backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] contextInitialized[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]ServletContextEvent[backcolor=transparent] sce [backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]ApplicationContext[backcolor=transparent] ctx [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]WebApplicationContextUtils[backcolor=transparent].[backcolor=transparent]getWebApplicationContext[backcolor=transparent]([backcolor=transparent] sce[backcolor=transparent].[backcolor=transparent]getServletContext[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 根据 Spring 配置中的Bean ID “item” 获取订阅到的服务
- [backcolor=transparent] [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]ItemService[backcolor=transparent] itemService [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]([backcolor=transparent] [backcolor=transparent]ItemService[backcolor=transparent] [backcolor=transparent])[backcolor=transparent] ctx[backcolor=transparent].[backcolor=transparent]getBean[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]"item"[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]……
- [backcolor=transparent] [backcolor=transparent]// 调用服务 ItemService 的 getItemById 方法
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent] itemService[backcolor=transparent].[backcolor=transparent]getItemById[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]1111[backcolor=transparent] [backcolor=transparent])[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 调用服务 ItemService 的 getItemByName 方法
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent] itemService[backcolor=transparent].[backcolor=transparent]getItemByName[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]"myname is le"[backcolor=transparent] [backcolor=transparent])[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]……
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
消费者配置属性清单
除了示例代码中体现的属性(interface,group,version)之外,还有下表的属性配置以供选择:
标签配置示例:
- [backcolor=transparent]<hsf:consumer[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"service"[backcolor=transparent] [backcolor=transparent]interface[backcolor=transparent]=[backcolor=transparent]"com.taobao.edas.service.SimpleService"
- [backcolor=transparent] [backcolor=transparent]version[backcolor=transparent]=[backcolor=transparent]"1.1.0"[backcolor=transparent] [backcolor=transparent]group[backcolor=transparent]=[backcolor=transparent]"test1"[backcolor=transparent] [backcolor=transparent]clientTimeout[backcolor=transparent]=[backcolor=transparent]"3000"
- [backcolor=transparent] [backcolor=transparent]target[backcolor=transparent]=[backcolor=transparent]"10.1.6.57:12200?_TIMEOUT=1000"[backcolor=transparent] [backcolor=transparent]maxWaitTimeForCsAddress[backcolor=transparent]=[backcolor=transparent]"5000"[backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<hsf:methodSpecials>
- [backcolor=transparent] [backcolor=transparent]<hsf:methodSpecial[backcolor=transparent] [backcolor=transparent]name[backcolor=transparent]=[backcolor=transparent]"sum"[backcolor=transparent] [backcolor=transparent]timeout[backcolor=transparent]=[backcolor=transparent]"2000"[backcolor=transparent] [backcolor=transparent]></hsf:methodSpecial>
- [backcolor=transparent] [backcolor=transparent]</hsf:methodSpecials>
- [backcolor=transparent]</hsf:consumer>
pom 中添加 edas-sdk 依赖
- [backcolor=transparent]<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]com.alibaba.edas[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]edas-sdk[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]<version>[backcolor=transparent]1.6.1[backcolor=transparent]</version>
- [backcolor=transparent]</dependency>
开发环境消费服务
完成代码和配置的开发任务之后,在 Eclipse 和 IDEA 中,可直接以 Ali-Tomcat 运行该服务(具体请参照文档
IDE 运行时启动配置。运行成功后,请参考
开发环境搭建 文档,在搭建好的 ConfigCenter 中查询到所需要消费的服务。