生产者将实现服务接口以提供具体实现,除了代码实现的工作之外,由于 HSF 是基于 Spring 框架来实现的,所以还需要再定义服务发布的 XML 文件。
代码实现服务接口
在 Demo 工程的 itemcenter 中可看到具体的示例:
- [backcolor=transparent]package[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]edas[backcolor=transparent].[backcolor=transparent]carshop[backcolor=transparent].[backcolor=transparent]itemcenter[backcolor=transparent];
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]ItemServiceImpl[backcolor=transparent] [backcolor=transparent]implements[backcolor=transparent] [backcolor=transparent]ItemService[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]Item[backcolor=transparent] getItemById[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]long[backcolor=transparent] id [backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]Item[backcolor=transparent] car [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Item[backcolor=transparent]();
- [backcolor=transparent] car[backcolor=transparent].[backcolor=transparent]setItemId[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]1l[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] car[backcolor=transparent].[backcolor=transparent]setItemName[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]"Mercedes Benz"[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] car[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]Item[backcolor=transparent] getItemByName[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] name [backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]Item[backcolor=transparent] car [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Item[backcolor=transparent]();
- [backcolor=transparent] car[backcolor=transparent].[backcolor=transparent]setItemId[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]1l[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] car[backcolor=transparent].[backcolor=transparent]setItemName[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]"Mercedes Benz"[backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] car[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
服务配置
上述例子主要实现了 com.alibaba.edas.carshop.itemcenter.ItemService,并在两个方法中返回了一个 Item 对象,代码开发好之后,除了必要的 web.xml 中的 Spring 常规配置,我们还需要增加相应的 Maven 依赖,同时在 Spring 配置文件使用<hsf /> 标签注册并发布该服务。具体内容如下:
在 pom.xml 中添加如下 Maven 依赖的内容:[backcolor=transparent]<dependencies>- [backcolor=transparent] [backcolor=transparent]<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]javax.servlet[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]servlet-api[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]<version>[backcolor=transparent]2.5[backcolor=transparent]</version>
- [backcolor=transparent] [backcolor=transparent]<scope>[backcolor=transparent]provided[backcolor=transparent]</scope>
- [backcolor=transparent] [backcolor=transparent]</dependency>
- [backcolor=transparent] [backcolor=transparent]<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]com.alibaba.edas.carshop[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]itemcenter-api[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]<version>[backcolor=transparent]1.0.0-SNAPSHOT[backcolor=transparent]</version>
- [backcolor=transparent] [backcolor=transparent]</dependency>
- [backcolor=transparent] [backcolor=transparent]<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-web[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]<version>[backcolor=transparent]2.5.6(及其以上版本)[backcolor=transparent]</version>
- [backcolor=transparent] [backcolor=transparent]</dependency>
- [backcolor=transparent] [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.5.0[backcolor=transparent]</version>
- [backcolor=transparent] [backcolor=transparent]</dependency>
- [backcolor=transparent]</dependencies>
增加 Spring 关于 HSF 服务的配置,Demo 工程 HSF 配置文件 /resources/hsf-provider-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] [backcolor=transparent]<bean[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"itemService"[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent]=[backcolor=transparent]"com.alibaba.edas.carshop.itemcenter.ItemServiceImpl"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]<!-- 用 hsf:provider 标签表明提供一个服务生产者 -->
- [backcolor=transparent] <hsf:provider id=“itemServiceProvider"
- [backcolor=transparent] [backcolor=transparent]<!-- 用 interface 属性说明该服务为此类的一个实现 -->
- [backcolor=transparent] interface=“com.alibaba.edas.carshop.itemcenter.ItemService"
- [backcolor=transparent] [backcolor=transparent]<!-- 此服务具体实现的 spring 对象 -->
- [backcolor=transparent] ref=“itemService"
- [backcolor=transparent] [backcolor=transparent]<!-- 发布该服务的版本号,可任意指定,默认为 1.0.0 -->
- [backcolor=transparent] version=“1.0.0"
- [backcolor=transparent] [backcolor=transparent]<!-- 服务分组 -->
- [backcolor=transparent] group=“testHSFGroup-09-04“>
- [backcolor=transparent] [backcolor=transparent]</hsf:provider>
- [backcolor=transparent]</beans>
生产者配置属性清单
关于 HSF 生产者的属性配置,除了上述内容提到的之外,还有如下的内容可供选择:
标签配置示例:
- [backcolor=transparent]<bean[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"impl"[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent]=[backcolor=transparent]"com.taobao.edas.service.impl.SimpleServiceImpl"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]<hsf:provider[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"simpleService"[backcolor=transparent] [backcolor=transparent]interface[backcolor=transparent]=[backcolor=transparent]"com.taobao.edas.service.SimpleService"
- [backcolor=transparent] [backcolor=transparent]ref[backcolor=transparent]=[backcolor=transparent]"impl"[backcolor=transparent] [backcolor=transparent]version[backcolor=transparent]=[backcolor=transparent]"1.0.1"[backcolor=transparent] [backcolor=transparent]group[backcolor=transparent]=[backcolor=transparent]"test1"[backcolor=transparent] [backcolor=transparent]clientTimeout[backcolor=transparent]=[backcolor=transparent]"3000"[backcolor=transparent] [backcolor=transparent]enableTXC[backcolor=transparent]=[backcolor=transparent]"true"
- [backcolor=transparent] [backcolor=transparent]serializeType[backcolor=transparent]=[backcolor=transparent]"hessian"[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]/>
- [backcolor=transparent] [backcolor=transparent]</hsf:methodSpecials>
- [backcolor=transparent] [backcolor=transparent]</hsf:provider>
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 运行时启动配置,运行成功后,可在轻量配置中心查询到所发布的服务,具体请参考
服务查询文档。
服务提供者额外的 JVM 启动参数
在服务的提供者中,有一些额外的启动参数来改变 HSF 的行为,具体如下: