【RPC框架Hessian一】Hessian RPC Hello World
编程技术  /  houtizong 发布于 3年前   66
The Hessian binary web service protocol makes web services usable without requiring a large framework, and without learning yet another alphabet soup of protocols. Because it is a binary protocol, it is well-suited to sending binary data without any need to extend the protocol with attachments.
<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"> <parent> <artifactId>user.project</artifactId> <groupId>com.tom</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>learn.hessian</artifactId> <dependencies> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!--jetty plugin to manage embedded jetty--> <!--No goal is specified--> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.7</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8668</port> <maxIdleTime>30000</maxIdleTime> </connector> </connectors> <webAppSourceDirectory>${project.basedir}/web </webAppSourceDirectory> <contextPath>/web</contextPath> </configuration> </plugin> </plugins> </build></project>
上面主要配置了三部分内容,一是依赖的Hessian包,二是Hessian依赖的servlet-api包(Hessian包虽然依赖servlet-api,但在Hessian包的依赖关系中并没有显示的去依赖servlet-api包),三是添加jetty插件,用于启动web项目
在HelloWorld中,没有复杂的Model对象,在实际项目中,服务器端和客户端共同依赖的内容包括模型对象和接口Service
package com.tom.hessian.common;import java.io.IOException;public interface IGreetingService { public String greeting(String name) throws IOException;}
package com.tom.hessian.server;import com.tom.hessian.common.IGreetingService;import java.io.IOException;public class GreetingService implements IGreetingService{ @Override public String greeting(String name) throws IOException { return "Welcome ot the Hassian world, " + name; }}
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>hessian</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>home-api</param-name><!--接口声明--> <param-value>com.tom.hessian.common.IGreetingService</param-value> </init-param> <init-param> <param-name>home-class</param-name><!--接口实现--> <param-value>com.tom.hessian.server.GreetingService</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hessian</servlet-name> <url-pattern>/hessian</url-pattern> </servlet-mapping></web-app>
package com.tom.hessian.client;import com.caucho.hessian.client.HessianProxyFactory;import com.tom.hessian.common.IGreetingService;public class GreetingServiceTest { public static void main(String[] args) throws Exception { //RPC访问地址 String url = "http://localhost:8668/web/hessian"; //接口的动态代理工厂 HessianProxyFactory factory = new HessianProxyFactory(); IGreetingService greetingService = (IGreetingService) factory.create(IGreetingService.class, url); //远程方法调用 System.out.println("hello(), " + greetingService.greeting("tom")); }}
启动jetty服务器,运行客户端main方法,得到如下输出,证明远程方法调用成功
hello, welcome ot the Hassian world, tom
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接