淘寶HSF服務的原理以及簡單的實現
淘寶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();
}
}
}
三個工程的具體實現我已經跑通並且放到了我本地的資原始檔了。