spring 整合Hessian Hessian服务暴露和服务调用 详细教程

源码地址

[email protected]:chenyaoBOY/hessian.git
https://github.com/chenyaoBOY/hessian.git

1.Hessian使用的介绍

Hessian是一个轻量级的RPC框架,基于HTTP协议


1.1 服务端配置

三部曲

  1. 通过springMVC的DispatherServlet控制Hessian服务接口的URL访问路径 即web.xml配置
  2. 新建Hessian服务 例如:DemoService 和实现类 DemoServiceImpl
  3. 配置Hessian服务的暴露 通过HessianServiceExporter

代码示例

1.1.1pom文件spring pom文件 + Hessian pom文件

<properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
        <spring.version>4.1.7.RELEASEspring.version>
    properties>

    <dependencies>
    
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.studygroupId>
            <artifactId>hessian-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>com.cauchogroupId>
            <artifactId>hessianartifactId>
            <version>4.0.38.1version>
        dependency>


        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>


    dependencies>

1.1.2 新建Hessian服务

//接口
public interface DemoService {

    String sayHello(String content);
}
import demo.DemoService;
//实现类
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String content) {
        System.out.println("content = " + content);
        return content;
    }
}

1.1.3 配置web.xml

 <servlet>
        <servlet-name>serviceservlet-name>
        
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            
            <param-value>classpath:applicationContext.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>serviceservlet-name>
        
        <url-pattern>/service/*url-pattern>
    servlet-mapping>

1.1.4 配置applicationContext.xml

         
        <bean id="demoService" class="demo.impl.DemoServiceImpl" />
        
        
        <bean name="/demo"   class="org.springframework.remoting.caucho.HessianServiceExporter">
            
            <property name="service" ref="demoService" />
            
            <property name="serviceInterface" value="demo.DemoService"/>
        bean>

综上 服务端的所有配置都已经完毕,启动Tomcat 端口8080 Hessian的服务路径就是
localhost:8080/service/demo

2 Hessian客户端的调用

2.1比较简单的方式
通过 HessianProxyFactory 工厂 获取具体的服务接口,然后入参 Hessian服务的URL即可

新建的工程 只需导入Hessian的pom文件即可,或者直接在服务端的工程中测试

 public void test(){
        HessianProxyFactory factory = new HessianProxyFactory();

        String url = "http://localhost:8080/service/demo";
        try {
            //入参两个参数  一个是接口的class  一个是接口的URL路径
            DemoService service = (DemoService) factory.create(DemoService.class, url);

            String hello = service.sayHello("hello world !");
            //输出hello wolrd!
            System.out.println(hello);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

这里要特别注意URL路径的问题,如果Tomcat配置了访问的项目名称,则需要在访问的时候加上项目名称
例如IDEA的使用如下
当配置了ApplicationContext的时候,需要在URL路径上配置该项目名称
localhost:8080/hessian-server/service/demo
spring 整合Hessian Hessian服务暴露和服务调用 详细教程_第1张图片

2.2 通过配置文件配置

  1. 新建client客户端工程,配置pom文件 需要spring和Hessian的jar ,和服务端配置一样即可,还要依赖Hessian服务接口 DemoService
  2. 创建spring 的配置文件 ,配置Hessian服务的消费

代码示例

spring配置文件applicationContext.xml


    <bean id="demoService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        
        <property name="serviceUrl">
            <value>http://localhost:8080/service/demovalue>
        property>
        
        <property name="serviceInterface">
            <value>demo.DemoServicevalue>
        property>
    bean>

启动spring容器 测试接口调用

public class DemoClient {

    public static void main(String[] args) {


        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        DemoService service = (DemoService) context.getBean("demoService");

        String hello = service.sayHello("say hello");

        System.out.println(hello);


    }
}

最后贴出源码地址

[email protected]:chenyaoBOY/hessian.git
https://github.com/chenyaoBOY/hessian.git

你可能感兴趣的:(rpc)