目录


mac-rpc - 示例

前往下载 RPC应用示例 | 依赖JAR包与源码下载

同步调用

  服务提供者:provide.xml
<service ref="demoService" group="demo" name="demo1" interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0" timeout="3000"> </service>   服务提供者:DemoServiceImpl.java
@Component("demoService") public class DemoServiceImpl implements DemoService { @Override public String helloSC(User u) { if (u == null || Util.strIsEmpty(u.getName())) { throw new IllegalArgumentException("Invalid parameter"); } return "Hello ".concat(u.getName()); } }   服务消费者:consume.xml
<reference id="demoService" group="demo" name="demo1" interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0" timeout="6000"> </reference>   服务消费者:Main.java
// 从Spring容器中获取,也可以通过@Autowired等注解方式注入 DemoService ds = (DemoService) ctx.getBean("demoService"); Object rs = null; ro = ds.helloSC(new User("Mac_J")); log.info("Result = {}", ro); try { // 此句会抛出来自服务提供者的异常 ro = ds.helloSC(new User()); log.info("Result = {}", ro); } catch (Exception e) { log.error("AC failed", e); }

指定服务提供者

  RpcContext.specify2用于指定服务提供者
RpcContext.specify2("127.0.0.1:9101"); try { ro = ds.helloSC(new User("Mac_J")); log.info("Result = {}", ro); } finally { RpcContext.specify2(null); }

异步调用

  异步调用方法的返回类型必须声明为Object,在编写实现时,需要返回真实的结果。但调用方在调用时获得的是一个Future<Object>
  服务消费者:consume.xml
<reference id="demoService" group="demo" name="demo1" interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0" timeout="6000"> <method name="helloAC" type="AC"> <arg type="com.boarsoft.rpc.sample.User" /> </method> </reference>   服务消费者:Main.java
Future<Object> ft = null; ft = (Future<Object>) ds.helloAC(new User("Mac_J")); log.info("Do something before get actual result"); ro = ft.get(); log.info("Result = {}", ro); // 此句不会抛出异常,返回的结果是远程方法内部抛出的异常 ft = (Future<Object>) ds.helloAC(new User()); log.info("Do something before get actual result"); ro = ft.get(); log.info("Result = {}", ro);

同步广播方法

  同步广播方法也是是需要特殊处理的方法,其返回值必须是Object类型。
  在编写服务方的实现时,需要返回真实的结果。但在服务的调用方(服务消费者)拿到的对象却时Map<String, Object>类型。此map的key是执行此方法的节点的地址,形如:IP:PORT,而map的value才是实际的结果。   服务消费者:consume.xml
<reference id="demoService" group="demo" name="demo1" interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0" timeout="6000"> <method name="helloSB" type="SB"> <arg type="com.boarsoft.rpc.sample.User" /> </method> </reference>   服务消费者:Main.java
Map<String, Object> rm = null; rm = (Map<String, Object>) ds.helloSB(new User("Mac_J")); for (String k : rm.keySet()) { ro = rm.get(k); log.info("{} = {}", k, ro); } rm = (Map<String, Object>) ds.helloSB(new User()); for (String k : rm.keySet()) { ro = rm.get(k); log.info("{} = {}", k, ro); }

异步广播方法

  异步广播方法与同步广播方法在处理上基本相同。异步广播方法采用Future模式,返回Map<String, Future<Object>>,因此在获取实际结果前不会阻塞当前线程。   服务消费者:consume.xml
<reference id="demoService" group="demo" name="demo1" interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0" timeout="6000"> <method name="helloAB" type="AB"> <arg type="com.boarsoft.rpc.sample.User" /> </method> </reference>   服务消费者:Main.java
Map<String, Object> rm = null; rm = (Map<String, Object>) ds.helloAB(new User("Mac_J")); for (String k : rm.keySet()) { ro = rm.get(k); if (ro != null && ro instanceof Future) { ro = ((Future<Object>) ro).get(); } log.info("{} = {}", k, ro); } rm = (Map<String, Object>) ds.helloAB(new User()); for (String k : rm.keySet()) { ro = rm.get(k); if (ro != null && ro instanceof Future) { ro = ((Future<Object>) ro).get(); } log.info("{} = {}", k, ro); }