HSF相比DUBBO更妥當和快速,可是阿里雲的edas又是收費的。雖然HSF不開源,可是本身搭建起來仍是能夠跑的java
1.新建的maven項目裏面加入如下依賴web
<dependency>
<groupId>com.alibaba.hsf</groupId>
<artifactId>LightApi</artifactId>
<version>1.0.0</version>
</dependency>spring
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>hsf.schema</artifactId>
<version>edas1.0.0</version>
</dependency>tomcat
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>app
hsf是和spring集成的,因此要加入spring的依賴。maven
LightApi 裏面有輕量級測試HSF的APIide
hsf.schema 裏面包含 hsf的xml定義性能
2.新建一個接口io.example.hsf.ICalService測試
新建類實io.example.hsf.CalServiceImp現這個接口。用@Service("calService")標註這個類,由於以後會使用spring的掃描得把這個bean註冊到spring。阿里雲
3.在classpath下面新建一個applicationContext文件。加入 <context:component-scan base-package="io.example.hsf"/> 目的就是掃描service,而且加入
<hsf:provider id="calService"
interface="io.example.hsf.ICalService" ref="calService"
version="1.0.0" group="testHSFGroup">
</hsf:provider>
hsf:provider 就是把這個服務用對應的接口名、版本、分組暴露出去。
4.啓動項目,首先要啓動edas-config-center,而且在hosts文件加入127.0.0.1 jmenv.tbsite.net,使HSF服務註冊在本地,在JVM參數裏面要加上潘多拉臨時文件的位置-Dcom.taobao.pandora.tmp_path=/tmp/phsf0,在alitomcat要加入潘多拉的位置,也就是taobao-hsf.sar的位置。啓動成功了會出現
5.測試服務。既然HSF服務以及有了,咱們能夠沒必要使用alitomcat測試HSF服務。main方法也能夠。
public static void main(String[] args) throws Exception { /** * 設置潘多拉路徑 */ ServiceFactory.addJVMProperty("com.taobao.pandora.tmp_path", "E:/tmp/p201"); ServiceFactory factory=ServiceFactory.getInstanceWithPath("F:/ali/"); ConsumerService consumerService=factory.consumer("testconsumer"). service("io.example.hsf.ICalService"). version("1.0.0").group("testHSFGroup"); consumerService.subscribe(); consumerService.sync(); System.out.println(consumerService.addresses()); ICalService itemService= (ICalService)consumerService.subscribe(); System.out.println( itemService.addNum(1, 2)); }
使用main方法也同樣要指定潘多拉相關的參數。
以後使用consumer.version()設置版本group設置分組service設置接口名。
調用consumer.subscribe 就會嘗試獲取這個服務而且得到代理對象。屢次調用並不會影響性能,第二次以後的調用會直接拿consumer的代理對象。