https://github.com/jremoting/jremoting

jremoting是一个类似dubbo的rpc服务治理框架,并且可以与dubbo相互调用。jremoting的实现是参考了dubbo的许多概念,但是代码相比 dubbo更简洁,模型也更精简更灵活。底层nio通信是基于netty 4.x实现的。注册中心同开源版本的dubbo一样基于zookeeper实现。

主要功能包括

  1. 透明方式的rpc调用,支持consumer端异步调用与provider的异步实现
  2. 服务的动态发现
  3. 负载均衡+ failover
  4. 动态路由
  5. 动态分组
  6. 服务限流(开发中)

如何使用:

服务提供方(provider)提供的服务接口定义,并将实现通过spring定义发布到注册中心(registry)

public interface HelloService {
String hello(String name);
}
public class HelloServiceImpl implements HelloService { @Override
public String hello(String name) {
return "hello,"+ name;
} } public class TestProvider {
public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("server-context.xml"); System.in.read(); context.close();
}
}
<?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.xsd"> <import resource="classpath:jremoting-context.xml"/> <bean id="helloServiceProvider" class="com.github.jremoting.spring.JRemotingProviderBean" init-method="start">
<constructor-arg name="interfaceName" value="com.github.jremoting.example.HelloService" />
<constructor-arg name="version" value="1.0" />
<constructor-arg name="target" ref="helloService" />
<constructor-arg name="rpcServer" ref="rpcServer" />
</bean> <bean id="helloService" class="com.github.jremoting.example.HelloServiceImpl"></bean>
</beans>

服务消费方(consumer)只依赖接口(HelloService)既可以通过rpc的方式调用远程provider的实现

/*consumer端自己定义异步版本接口*/
public interface AsyncHelloService {
/**
* 异步方法的签名规则是同步方法名加前缀 $, 返回结果为ListenableFuture
*/
ListenableFuture<String> $hello(String name);
} public class TestClient {
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client-context.xml"); HelloService helloService = context.getBean(HelloService.class); //同步调用
String result = helloService.hello("jremoting"); System.out.println(result); //异步调用
AsyncHelloService asyncHelloService = (AsyncHelloService)helloService; ListenableFuture<String> future = asyncHelloService.$hello("jremoting async invoke!"); System.out.println(future.get()); //异步listener方式调用,注意operationComplete是在jremoting-context.xml中配置的专门executor上执行的。也可以自己指定executor
future = asyncHelloService.$hello("jremoting async use future listener!"); future.addListener(new FutureListener<String>() { @Override
public void operationComplete(ListenableFuture<String> future) {
if(future.isSuccess()) {
System.out.println(future.result());
}
}
}); //如果consumer端,不想依赖provider定义的接口,也可以直接调用远程方法,不过要把复杂对象都用map来代替,返回结果也一样
RpcClient rpcClient = context.getBean(RpcClient.class);
ServiceConsumer consumer = new ServiceConsumer("com.github.jremoting.example.HelloService", "1.0", rpcClient).start(); Object obj = consumer.invoke("hello", new String[]{java.lang.String.class.getName()}, new Object[]{"generic invoke!"}); System.out.println(obj); //关闭容器退出
System.in.read();
context.close();
}
}
<?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.xsd"> <import resource="classpath:jremoting-context.xml"/> <bean id="helloService" class="com.github.jremoting.spring.JRemotingConsumerBean" init-method="start">
<constructor-arg name="interfaceName" value="com.github.jremoting.example.HelloService" />
<constructor-arg name="version" value="1.0" />
<constructor-arg name="rpcClient" ref="rpcClient" />
<property name="asyncInterfaceName" value="com.github.jremoting.example.AsyncHelloService"></property>
</bean>
</beans>

jremoting注册中心基于zookeeper实现的,测试hello world例子需要安装zookeeper. jremoting-context.xml中配置了jremoting运行 定义的bean对象 以及zookeeper连接地址,已经provider端连接的监听端口。另外还有jremoting的protocal,serializer,executor,invokefilter,registrywrapper等对象定义。用户可以自己定义每个组件的实现bean

<?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.xsd"> <!-- 定义serializer,默认提供json与hessian序列化 -->
<bean id="jsonSerializer" class="com.github.jremoting.serializer.JsonSerializer" ></bean>
<bean id="hessianSerializer" class="com.github.jremoting.serializer.HessianSerializer" ></bean> <!-- 定义服务注册中心,底层采用zookeeper来实现,服务的动态发现与配置的动态推送。除了CacheRegistryWrapper,与ZookeeperRegistry为必选,其他实现分组,路由,权重为可选 -->
<bean id="registry" class="com.github.jremoting.route.RouteRegistryWrapper">
<constructor-arg>
<bean class="com.github.jremoting.group.GroupRegistryWrapper">
<constructor-arg>
<bean class="com.github.jremoting.registry.CacheRegistryWrapper">
<constructor-arg>
<bean class="com.github.jremoting.registry.ZookeeperRegistry">
<constructor-arg name="zookeeperConnectionString" value="127.0.0.1:2181" />
</bean>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</constructor-arg>
</bean> <!-- 定义协议 ,并给协议关联序列化与注册中心-->
<bean id="jremotingProtocal" class="com.github.jremoting.protocal.JRemotingProtocal">
<constructor-arg name="registry" ref="registry" />
<constructor-arg name="serializers">
<array>
<ref bean="jsonSerializer"/>
<ref bean="hessianSerializer"/>
</array>
</constructor-arg>
</bean> <!-- 定义io线程池 底层netty处理io事件与ChannelHandler的线程池 -->
<bean id="eventExecutor" class="com.github.jremoting.transport.EventExecutor"></bean> <!-- 定义用户线程池,可用于consumer端的异步回调处理,或者provider端的服务方法处理-->
<bean id="executor" class="com.github.jremoting.util.concurrent.Executors" factory-method="newExecutor">
<constructor-arg name="corePoolSize" value="3"></constructor-arg>
<constructor-arg name="maxPoolSize" value="20"></constructor-arg>
<constructor-arg name="queueSize" value="20"></constructor-arg>
</bean> <!-- 定义rpc client对象,用户定制consumer端需要的各种组件,包括协议,默认序列化方式,异步调用线程池,调用拦截器 -->
<bean id="rpcClient" class="com.github.jremoting.transport.DefaultRpcClient">
<constructor-arg name="protocal" ref="jremotingProtocal" />
<constructor-arg name="defaultSerializer" ref="hessianSerializer" />
<constructor-arg name="eventExecutor" ref="eventExecutor" />
<constructor-arg name="asyncInvokeExecutor" ref="executor" />
<constructor-arg name="invokeFilters">
<list>
<!-- 实现重试功能的拦截器 -->
<bean class="com.github.jremoting.invoke.RetryInvokeFilter" />
<!-- 实现软负载与failover的拦截器 , 负载方式为在可用provider间随机调用-->
<bean class="com.github.jremoting.invoke.ClusterInvokeFilter" />
</list>
</constructor-arg>
</bean> <!-- 定义rpc server对象,定制server端的各种组件,包括协议,执行provider方法的线程池,调用拦截器 -->
<bean id="rpcServer" class="com.github.jremoting.transport.DefaultRpcServer">
<constructor-arg name="eventExecutor" ref="eventExecutor" />
<constructor-arg name="serviceExecutor" ref="executor" />
<constructor-arg name="protocal" ref="jremotingProtocal" />
<constructor-arg name="port" value="8687" />
<constructor-arg name="invokeFilters">
<list></list>
</constructor-arg>
</bean> <!-- 监听spring容器的关闭事件,同时关闭jremoting需要关闭的资源,包括监听,注册中心,线程池 -->
<bean id="jremmotingLifeCycle" class="com.github.jremoting.spring.JRemotingLifeCycleBean">
<property name="rpcClients">
<list>
<ref bean="rpcClient"/>
</list>
</property>
<property name="rpcServers">
<list>
<ref bean="rpcServer"/>
</list>
</property>
</bean> </beans>

java开源项目jremoting的更多相关文章

  1. 网上下载的 java开源项目 如何 打jar包

    目前很多java开源项目(例如qlexpress)只提供了源码,没有提供jar,下面提供maven打jar包的方法. 1.进入qlexpress下载后源代码所在的目录,此目录应包含pom.xml,如下 ...

  2. 用Java开源项目JOONE实现人工智能编程

    http://www.robotsky.com/ZhiN/MoS/2011-08-25/13142461416649.html 用Java开源项目JOONE实现人工智能编程 https://sourc ...

  3. 【我整理的java开源项目】

    摘要: 1. 整理出一些使用比较广或者个人觉得比较好的java开源项目和资料供参考. 2. 如果你觉得好但是我没有列出的开源项目请告诉我,方便我添加到列表里. 3. 如果你发现信息描述有误请联系我,我 ...

  4. 3月份GitHub上最热门的Java开源项目

    今天,我们来盘点3月份GitHub上最热门的Java项目的时候了,如果你每月都有关注猿妹发布的排行榜,那么本月的Java项目对你来说一定不陌生,这些都是曾经多次出现在榜单中的项目: 1 advance ...

  5. 硬核! 逛了4年Github ,一口气把我收藏的 Java 开源项目分享给你!

    Awsome Java Great Java project on Github(Github 上非常棒的 Java 开源项目). English Version 大家都知道 Github 是一个程序 ...

  6. 阿里巴巴的26款Java开源项目

    阿里巴巴的26款Java开源项目 开源展示了人类共同协作,成果分享的魅力.没有任何一家网络公司可以不使用开源技术,仅靠自身技术发展起来.“取之于开源,用之于开源,才能促进开源的良性发展”,阿里巴巴各个 ...

  7. java开源项目

    原文地址:http://blog.longjiazuo.com/archives/2625 1.整理出一些使用比较广或者个人觉得比较好的java开源项目和资料供参考.2.如果你觉得好但是我没有列出的开 ...

  8. 硬核! Github上 ,star超高的Java 开源项目分享给你!

    Awsome JavaGreat Java project on Github(Github 上非常棒的 Java 开源项目). English Version 大家都知道 Github 是一个程序员 ...

  9. 10个你能参与并学习的Java开源项目

    本文转载于:http://code.csdn.net/news/2822604 有很多备受关注的初创开源项目,下面列出十个项目是我觉得非常有趣的,涉及到的学习范围也很广.其中有些还在早期阶段,这对我们 ...

随机推荐

  1. 百度地图JavaScript如何清除指定类型的覆盖物

    由于一个地图中有很多种类型的覆盖物,由于某个覆盖物(一般是自定义)整个地图中只允许出现一次 那第一想到的就是,每次创建这个类型的覆盖物时先清除这一类型的覆盖物,比较简单判断覆盖物的类型 instanc ...

  2. 【python】python中__name__的使用

    Py1.py #!/usr/bin/env python def test(): print '__name__ = ',__name__ if __name__ == '__main__': tes ...

  3. BASIC-1_蓝桥杯_闰年判断

    正确代码: #include <stdio.h> int main(void){ int year = 0 ; scanf("%d",&year); if (y ...

  4. spring SOA architecture

    在谈这个之前,还得再说下SOA和平台.SOA做两件事情,一个是解耦并识别可重用的服务,一个是对服务进行灵活组装和编排满足业务需求,SOA核心是业务和技术的解耦,服务和能力的复用.而在IT领域的平台平台 ...

  5. linux 下 rpc python 实例之使用XML-RPC进行远程文件共享

    这是个不错的练习,使用python开发P2P程序,或许通过这个我们可以自己搞出来一个P2P下载工具,类似于迅雷.XML-RPC是一个远程过程调用(remote procedure call,RPC)的 ...

  6. API网关Kong系列(一)初识

    最近工作需要,加上国内Kong的文章相对缺乏(搜来搜去就那么两篇文章),而且官方文档在某些demo上也有一些过时的地方,遂提笔记录下这些,希望能有帮助. 先随大流介绍下KONG(主要参考官网): 官方 ...

  7. java的缓存框架

    1.java里面有一些开源的缓存框架,比如ecache,memcache,redis等缓存框架. 2.使用缓存框架的原理就是减少数据库端的压力,将缓存数据放在内存里面,存储成键值对的格式,这样可以不去 ...

  8. MySQL数据库Innodb储存引擎----储存页的结构

    上一篇博客回顾: 1:数据库拥有众多的储存引擎,现在主要使用的是Inoodb,这个储存引擎有Compact,Redundant,Dynamic,Compressed四种行格式 2:Compact行格式 ...

  9. python知识点, float不能用 != 判断

    python知识点链接:https://github.com/taizilongxu/interview_python 搜索:python最佳事件 书单:http://lucida.me/blog/d ...

  10. python中 cmp

    python3.*版本中取消了数值之间的比较:cmp(x, y).取而代之的是 (x > y) - (x < y) 因为python中 false 为 0, true 为 1.