`

如何使用HSF

    博客分类:
  • webx
 
阅读更多

   应用场景: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接口了。
 
三、参考:
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics