淘宝HSF服务具体来说分三个应用:api接口,service服务,本地应用。

       最基本的Api服务应该是十分干净的,不含方法,只有接口。它是要被打包(jar包的形式)到中央仓库去的。

       service服务是api接口的实现,它是要被打包成(最常见的是war包)安装到远程tomcat,或jboss中,作为服务要随时等待各种应用的调用的。

       本地应用自然是各种应用了。

       接口部分的pom文件:

                                                         4.0.0                                     com.taobao.hsftest                                      itest                                      1.0.0.SNAPSHOT                      

         接口:

                        package com.taobao.itest;                                public interface HelloService {                                                 public void sayHello();                        }            实现类的信息:           pom文件:

                 4.0.0  com.taobao.testimpl  testimpl  1.0.0.SNAPSHOT  hsf hello          1.6            hsf-sample                                    maven-compiler-plugin                                    ${java.version}                    ${java.version}                                                            log4j            log4j            1.2.14                            org.apache.geronimo.specs            geronimo-servlet_2.5_spec            1.2            provided                            org.springframework            spring            2.5.6            compile                            com.taobao.hsf            hsfunit            1.0.2                            junit            junit            4.8.2                                commons-logging                commons-logging                1.1.1                            org.springframework            spring-test            3.0.5.RELEASE                        com.taobao.hsftest          itest          1.0.0.SNAPSHOT       

     applicationContext.xml:

                               com.taobao.itest.HelloService                                                        1.0.0.zhanqiong                 启动文件:

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管理

  service      contextConfigLocation    classpath:applicationcontext.xml             org.springframework.web.context.ContextLoaderListener 

打包部署到tomcat或jboss容器下

本地调用:

pom文件:

  4.0.0  com.taobao.client.clienthsf  clienthsf  1.0.0-SNAPSHOT                      log4j            log4j            1.2.14                            org.apache.geronimo.specs            geronimo-servlet_2.5_spec            1.2            provided                            org.springframework            spring            2.5.6            compile                            com.taobao.hsf            hsfunit            1.0.2                            junit            junit            4.8.2                                commons-logging                commons-logging                1.1.1                            org.springframework            spring-test            3.0.5.RELEASE                        com.taobao.hsftest          itest          1.0.0.SNAPSHOT            

applicationContext.xml文件:

                        com.taobao.itest.HelloService                            1.0.0.zhanqiong            

 

本地调用代码:

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();                        }    }}

      三个工程的具体实现我已经跑通并且放到了我本地的资源文件了。

查看原文