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 变量定义

    一.javascript中,变量定义的位置与写在哪个<script></script>标签对内无关,只分前后顺序,写在前面的后面就能够访问,写在后面的前面会提示“未定义”. 例 ...

  2. 复分析可视化方法:笔记:log(z)的可视化微分法

    当z转过θ时,我们来看看发生了什么: 左图中的空心箭头代表z的变化量,其长度为rδ,方向为pi/2+θ: 右图中的实心箭头代表log(z)的变化量,其长度为δ,方向为pi/2. 因此,从左图空心箭头到 ...

  3. Java\学习——字符串

    import java.util.Scanner; public class cys1 { /** * @param args */ public static void main(String[] ...

  4. [UE4]运行时创建Actor

  5. 关于json_encode()的使用注意

    json_encode($json_str,true)在一般情况下可以返回一个数组,但当$json_str的字符编码是GBK或其它时,返回的是一个 空数组,必须用iconv(‘gbk’,‘ut8//I ...

  6. Lua C++互传结构体实例

    转自:http://bbs.csdn.net/topics/350261649 =====main.cpp======= #include "stdio.h" extern &qu ...

  7. ETL,BPM与ESB三者的一些感悟

    1.ETL: 数据层之间,主要在数据库层面上进行数据抽取过程------数据库层 2.ESB 异构系统之间通过总线技术,实现系统交互---------------系统通信层 3.BPM 自动化流程处理 ...

  8. CCKS 2018 | 最佳论文:南京大学提出DSKG,将多层RNN用于知识图谱补全

    作者:Lingbing Guo.Qingheng Zhang.Weiyi Ge.Wei Hu.Yuzhong Qu 2018 年 8 月 14-17 日,主题为「知识计算与语言理解」的 2018 全国 ...

  9. 中国标准时间改为formatTime格式

    1.toLocaleDateString (根据本地时间把Date 对象的日期部分转换为字符串): var time = new Date(); var formatTime = time.toLoc ...

  10. linux教程

    linux视频教程:尚观 http://www.uplinux.com/shipin/linuxyong-hu-guan-li-zhi-yong-hu-guan-li-01 一,linux开机(cen ...