淘宝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();
}
}
}
三个工程的具体实现我已经跑通并且放到了我本地的资源文件了。