1、Hessian
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据
Hessian实现的样例见:http://www.alisdn.com/wordpress/?p=478
2、Web Service
3、RMI
spring对rmi进行了封装,操作起来更加的方便
- package com.alibaba.ural.rmi;
- import java.io.Serializable;
- /**
- * POJO 类
- * @author keju.wangkj
- *
- */
- public class Account implements Serializable {
- private static final long serialVersionUID = 2093515007620385610L;
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- package com.alibaba.ural.rmi;
- import java.util.List;
- public interface AccountService {
- public void insertAccount(Account acc);
- public List<String> getAccounts(String name);
- }
- package com.alibaba.ural.rmi;
- import java.util.ArrayList;
- import java.util.List;
- public class AccountServiceImpl implements AccountService {
- public void insertAccount(Account acc) {
- // do something
- System.out.println("rmi invoke: account name -> " + acc.getName());
- }
- public List<String> getAccounts(String name) {
- // do something
- List<String> list = new ArrayList<String>();
- list.add("rmi invoke: " + name);
- return list;
- }
- }
- package com.alibaba.ural.rmi;
- import java.rmi.Remote;
- import java.rmi.RemoteException;
- import java.util.List;
- public interface RemoteAccountService extends Remote {
- public void insertAccount(Account acc) throws RemoteException;
- public List<String> getAccounts(String name) throws RemoteException;
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
- <bean id="accountService" class="com.alibaba.ural.rmi.AccountServiceImpl">
- <!-- any additional properties, maybe a DAO? -->
- </bean>
- <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
- <property name="serviceName" value="AccountService" />
- <property name="service" ref="accountService" />
- <property name="serviceInterface" value="com.alibaba.ural.rmi.AccountService" />
- <!-- defaults to 1099 -->
- <property name="registryPort" value="1199" />
- </bean>
- </beans>
启动服务:
- package com.alibaba.ural.rmi;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ServerStart {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[] { "rmiServer.xml" }); // 加载提供者配置
- context.start();
- synchronized (ServerStart.class) {
- while (true) {
- try {
- ServerStart.class.wait(); // 阻止JVM退出
- } catch (InterruptedException e) {
- // ignore
- }
- }
- }
- }
- }
客户端代码:
- package com.alibaba.ural.rmi.client;
- import com.alibaba.ural.rmi.AccountService;
- public class SimpleObject {
- private AccountService accountService;
- public void setAccountService(AccountService accountService) {
- this.accountService = accountService;
- }
- public AccountService getAccountService() {
- return accountService;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
- <bean id="simpleObject" class="com.alibaba.ural.rmi.client.SimpleObject">
- <property name="accountService" ref="accountService" />
- </bean>
- <bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
- <property name="serviceUrl" value="rmi://localhost:1199/AccountService" />
- <property name="serviceInterface" value="com.alibaba.ural.rmi.AccountService" />
- </bean>
- </beans>
开启调用:
- package com.alibaba.ural.rmi.client;
- import java.util.List;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.alibaba.ural.rmi.AccountService;
- public class Client {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[] { "rmiClient.xml" }); // 加载消费者配置
- context.start();
- AccountService helloService = ((SimpleObject) context
- .getBean("simpleObject")).getAccountService(); // 获取远程服务代理
- for (int i = 0; i < Integer.MAX_VALUE; i++) {
- try {
- Thread.sleep(1000);
- List<String> hello = helloService.getAccounts("world"); // 执行远程服务
- System.out.println("(" + i + ") " + hello.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
4、COBAR
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/396922,如需转载请自行联系原作者