淘宝HSF服务具体来说分三个应用:api接口,service服务,本地应用。
最基本的Api服务应该是十分干净的,不含方法,只有接口。它是要被打包(jar包的形式)到中央仓库去的。
service服务是api接口的实现,它是要被打包成(最常见的是war包)安装到远程tomcat,或jboss中,作为服务要随时等待各种应用的调用的。
本地应用自然是各种应用了。
接口部分的pom文件:
接口:
package com.taobao.itest; public interface HelloService { public void sayHello(); } 实现类的信息: pom文件:
applicationContext.xml:
package com.taobao.itest.impl;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.taobao.hsf.hsfunit.HSFEasyStarter;public class Main { public static void main(String[] args) { try { HSFEasyStarter.startFromPath("D:\\taobao-hsf"); Thread.sleep(1000); new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("Start end by zhanqiong!"); } catch (Exception e) { System.out.println(e.getMessage()); } }}
也可以将server交给容器启动,此时首先需要该工程为war工程
在web.xml文件中添加spring管理
打包部署到tomcat或jboss容器下
本地调用:
pom文件:
applicationContext.xml文件:
本地调用代码:
package com.taobao.clienthsf;import org.springframework.beans.BeansException;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.taobao.hsf.hsfunit.HSFEasyStarter;import com.taobao.itest.HelloService;public class Main { private HelloService helloWorldService; private void test(){ try { HSFEasyStarter.startFromPath("D:\\taobao-hsf"); Thread.sleep(1000); helloWorldService = (HelloService) new ClassPathXmlApplicationContext("applicationContext.xml").getBean("helloWorldService"); } catch (BeansException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } helloWorldService.sayHello(); } public HelloService getHelloWorldService() { return helloWorldService; } public void setHelloWorldService(HelloService helloWorldService) { this.helloWorldService = helloWorldService; } public static void main(String[] args) { for(int i=0;i<10;i++){ new Main().test(); } }}
三个工程的具体实现我已经跑通并且放到了我本地的资源文件了。
发表评论