您的位置:首页 > 其它

如何使用HSF

2015-10-24 11:35 281 查看

   应用场景:B项目需要访问A项目的某些接口

 

一、A项目:

 

1.在pom.xml中添加hsf

 

<dependency>
<groupId>com.taobao.hsf</groupId>
<artifactId>hsf.app.spring</artifactId>
<version>2.1.0.8</version>
</dependency>

 

2.创建一个子项目api,只写需要向外提供的接口以及需要的bean:
package com.tiro.api;
public interface UserClientAPI {
List<User> listUser();
}
 3.在另一个子项目biz中,在pom.xml中添加api项目,并实现api中的接口:
<dependency>
<groupId>com.tiro</groupId>
<artifactId>api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
package com.tiro.api.impl;
public class UserClientAPIImpl implements UserClientAPI {
List<User> listUser(){
//实现代码
}
}
4.创建spring配置文件spring-hsf-provier.xml,添加接口到hsf服务中,配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-autowire="byName">
<bean id="userClientAPIImpl" class="com.tiro.api.impl.UserClientAPIImpl"/>
<bean id="useerClientAPIProvider" class="com.taobao.hsf.app.spring.util.HSFSpringProviderBean" init-method="init">
<property name="serviceInterface">
<value>com.tiro.api.UserClientAPI</value>
</property>
<property name="target">
<ref bean="userClientAPIImpl"/>
</property>
<property name="serviceName">
<value>userClientAPIService</value>
</property>
<property name="serviceVersion">
<value>1.0.0.daily</value>
</property>
</bean>
</beans>
   5.在webx.xml中导入该配置文件:
<beans:import resource="spring-hsf-provider.xml" />
 6. 将子项目api发布到中央仓库: mvn deploy 7. 使用jerry运行项目,添加taobao-hsf.sar扩展。   二、项目B: 1.在pom.xml添加api项目:
<dependency>
<groupId>com.tiro</groupId>
<artifactId>api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
 2.创建spring配置文件spring-hsf-consume.xml,从hsf服务中获取接口,配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">

<bean id="userClientAPI" class="com.taobao.hsf.app.spring.util.HSFSpringConsumerBean" init-method="init">
<property name="interfaceName" value="com.tiro.api.UserClientAPI" />
<property name="version">
<value>1.0.0.daily</value>
</property>
</bean>

</beans>
 3.在webx.xml中导入该配置文件:
<beans:import resource="spring-hsf-consume.xml" />
 4.这样你就可以在项目中直接使用userClientAPI接口了。   三、参考: http://googi.iteye.com/blog/1884754  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: