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

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

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

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

接口部分的pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org        /POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
                                    <modelVersion>4.0.0</modelVersion>
                                     <groupId>com.taobao.hsftest</groupId>
                                      <artifactId>itest</artifactId>
                                      <version>1.0.0.SNAPSHOT</version>
                       </project>

接口:

package com.taobao.itest;

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.taobao.testimpl</groupId>
  <artifactId>testimpl</artifactId>
  <version>1.0.0.SNAPSHOT</version>
  <description>hsf hello</description>
  <properties>
        <java.version>1.6</java.version><!-- JDK版本配置属性 -->
  </properties>
  <build>
        <finalName>hsf-sample</finalName><!-- 打包时的war包名称: hsf-sample.war -->
        <plugins>
            <plugin><!-- 对maven编辑插件进行定制 -->
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source><!-- 指定JDK版本 -->
                    <target>${java.version}</target><!-- 指定JDK版本 -->
                </configuration>
            </plugin>
        </plugins>
    </build>
  <dependencies>
      <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-servlet_2.5_spec</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.taobao.hsf</groupId>
            <artifactId>hsfunit</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
        <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
      <dependency>
          <groupId>com.taobao.hsftest</groupId>
          <artifactId>itest</artifactId>
          <version>1.0.0.SNAPSHOT</version>
      </dependency>
  </dependencies>
</project>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="helloWorldServiceImpl" class="com.taobao.itest.impl.HelloService"></bean>
    <bean class="com.taobao.hsf.app.spring.util.HSFSpringProviderBean" init-method="init">
        <property name="serviceInterface">
            <value>com.taobao.itest.HelloService</value>
        </property>
        <property name="target">
            <ref bean="helloWorldServiceImpl" />
        </property>
        <property name="serviceVersion">
            <value>1.0.0.zhanqiong</value>
        </property>
    </bean>
</beans>
     启动文件:

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>service</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationcontext.xml</param-value>
  </context-param>
  <listener>    
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

打包部署到tomcat或jboss容器下

本地调用:

pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.taobao.client.clienthsf</groupId>
  <artifactId>clienthsf</artifactId>
  <version>1.0.0-SNAPSHOT</version>
    <dependencies>
      <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-servlet_2.5_spec</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.taobao.hsf</groupId>
            <artifactId>hsfunit</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
        <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
      <dependency>
          <groupId>com.taobao.hsftest</groupId>
          <artifactId>itest</artifactId>
          <version>1.0.0.SNAPSHOT</version>
      </dependency>
      </dependencies>
</project>

applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="helloWorldService" class="com.taobao.hsf.app.spring.util.HSFSpringConsumerBean"
        init-method="init">
        <property name="interfaceName">
            <value>com.taobao.itest.HelloService</value>
        </property>
        <property name="version">
            <value>1.0.0.zhanqiong</value>
        </property>
    </bean>
</beans>

本地调用代码:

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

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

淘宝HSF服务的原理以及简单的实现的更多相关文章

  1. 淘宝HSF 框架使用 总结

    @(JAVA开发) 淘宝HSF 框架使用 总结 随着网站访问量增加,仅仅靠增加机器已不能满足系统的要求,于是需要对应用系统进行垂直拆分和水平拆分.在拆分之后,各个被拆分的模块如何通信?如何保证性能?如 ...

  2. 后盾网lavarel视频项目---5、淘宝镜像cnpm的原理及如何使用

    后盾网lavarel视频项目---5.淘宝镜像cnpm的原理及如何使用 一.总结 一句话总结: 原理:把npm上面的所有软件copy过来 使用:npm install -g cnpm --regist ...

  3. 教你使用docker部署淘宝rap2服务

    什么是rap2 先来说说起因,在上一个星期的分享会上,谈到前后端联调上,有同事提到了rap2,特意去了解了一下,觉得使用这个东西来进行前后端的接口联调来真是太方便了,对比我们之前公司内部开发的API ...

  4. 关于django 京东淘宝 混合搜索实现原理

    混合搜索在各大网站如京东.淘宝都有应用,他们的原理都是什么呢?本博文将为你介绍它们的实现过程. 混合搜索的原理,用一句话来说就是:关键字id进行拼接. 混合搜索示例: 数据库设计: 视频方向: 1 2 ...

  5. HSF服务的开发与使用

    1.HSF服务的开发 1) 基于Maven创建一个web工程HSFService,如下图,其他的可以自定义. 2)创建好好在src/main目录下创建一个java目录,并将其设置为sources fo ...

  6. C#编写Windows服务程序 (服务端),client使用 消息队列 实现淘宝 订单全链路效果

    需求: 针对 淘宝提出的 订单全链路 产品接入 .http://open.taobao.com/doc/detail.htm?id=102423&qq-pf-to=pcqq.group oms ...

  7. 从Hadoop骨架MapReduce在海量数据处理模式(包括淘宝技术架构)

    从hadoop框架与MapReduce模式中谈海量数据处理 前言 几周前,当我最初听到,以致后来初次接触Hadoop与MapReduce这两个东西,我便稍显兴奋,认为它们非常是神奇.而神奇的东西常能勾 ...

  8. 淘宝接口 TopAPi

    演示一下调用淘宝的接口,让大家心里有个数, 很简单,新建一个工程,拖一个IDHttp,Button和Memo到窗体上去 然后在这个Button的OnClick事件中写入如下代码: [delphi] v ...

  9. 【转】淘宝技术牛p博客整理

    转自:http://blog.csdn.NET/zdp072/article/details/19574793 淘宝技术委员会是由淘宝技术部高级技术人员组成的一个组织,共分为Java分会.C/C++分 ...

随机推荐

  1. 自定义 Lint 规则简介

    上个月,笔者在巴黎 Droidcon 的 BarCamp 研讨会上聆听了 Matthew Compton 关于编写自己的 Lint 规则的讲话.深受启发之后,笔者想就此话题做进一步的探索. 定义 如果 ...

  2. Es索引优化

    https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html https://www.elastic.co/gui ...

  3. TC Asia Competition

    250PT不说了.很水得一题. 500PT 给定n(<=1e18),求最大的因子,且这个因子为完全平方,假设这个因子为x那么满足x*x*y = n, 一直枚举因子到n^(1/3)就可以了. 最后 ...

  4. [codility]Grocery-store

    http://codility.com/demo/take-sample-test/hydrogenium2013 用Dijkstra求最短路径,同时和D[i]比较判断是不是能到.用了优先队列优化,复 ...

  5. sdut2536字母哥站队(dp)

    简单DP  说是简单 还是推了好一会 推出来觉得好简单 保留当前i的最小值 dp[i] = min(dp[i],dp[j]+i-j-1) j<i #include <iostream> ...

  6. bzoj2286

    很明显,20%=mincut 40%=每次暴力树形dp那么正解是什么呢?不难发现∑ki<=500000,也就是每次询问的复杂度都要跟k有关从树形dp工作的角度来看,确实有很多点我们根本就没必要访 ...

  7. jquery easyui treegrid使用小结

    在实际应用中可能会碰到不同的需求,比如会根据每行不同的参数或属性设置来设置同列不同的editor类型,这时原有的例子就显的有点太过简单,不能实现我们的需求,现在应用我在项目中的操作为例,显示下实现同列 ...

  8. How to Set Word Document Properties with C#

    Word properties shows a brief description about one document. Through properties, we can learn gener ...

  9. ArcGIS for Android 中MapView的地图背景设置

    转自:http://blog.csdn.net/wozaifeiyang0/article/details/7535704 根据多方面测速,终于解决了一个蛋疼的问题,MapView的背景设置问题. 在 ...

  10. 关于HTTP返回码301、302区别与SEO

    301(永久移动)请求的网页已永久移动到新位置.服务器返回此响应时,会自动将请求者转到新位置.您应使用此代码告诉搜索引擎Spider某个网页或网站已永久移动到新位置.建议在URL规范化的时候采用301 ...