java开源项目jremoting
https://github.com/jremoting/jremoting
jremoting是一个类似dubbo的rpc服务治理框架,并且可以与dubbo相互调用。jremoting的实现是参考了dubbo的许多概念,但是代码相比 dubbo更简洁,模型也更精简更灵活。底层nio通信是基于netty 4.x实现的。注册中心同开源版本的dubbo一样基于zookeeper实现。
主要功能包括
- 透明方式的rpc调用,支持consumer端异步调用与provider的异步实现
- 服务的动态发现
- 负载均衡+ failover
- 动态路由
- 动态分组
- 服务限流(开发中)
如何使用:
服务提供方(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的更多相关文章
- 网上下载的 java开源项目 如何 打jar包
目前很多java开源项目(例如qlexpress)只提供了源码,没有提供jar,下面提供maven打jar包的方法. 1.进入qlexpress下载后源代码所在的目录,此目录应包含pom.xml,如下 ...
- 用Java开源项目JOONE实现人工智能编程
http://www.robotsky.com/ZhiN/MoS/2011-08-25/13142461416649.html 用Java开源项目JOONE实现人工智能编程 https://sourc ...
- 【我整理的java开源项目】
摘要: 1. 整理出一些使用比较广或者个人觉得比较好的java开源项目和资料供参考. 2. 如果你觉得好但是我没有列出的开源项目请告诉我,方便我添加到列表里. 3. 如果你发现信息描述有误请联系我,我 ...
- 3月份GitHub上最热门的Java开源项目
今天,我们来盘点3月份GitHub上最热门的Java项目的时候了,如果你每月都有关注猿妹发布的排行榜,那么本月的Java项目对你来说一定不陌生,这些都是曾经多次出现在榜单中的项目: 1 advance ...
- 硬核! 逛了4年Github ,一口气把我收藏的 Java 开源项目分享给你!
Awsome Java Great Java project on Github(Github 上非常棒的 Java 开源项目). English Version 大家都知道 Github 是一个程序 ...
- 阿里巴巴的26款Java开源项目
阿里巴巴的26款Java开源项目 开源展示了人类共同协作,成果分享的魅力.没有任何一家网络公司可以不使用开源技术,仅靠自身技术发展起来.“取之于开源,用之于开源,才能促进开源的良性发展”,阿里巴巴各个 ...
- java开源项目
原文地址:http://blog.longjiazuo.com/archives/2625 1.整理出一些使用比较广或者个人觉得比较好的java开源项目和资料供参考.2.如果你觉得好但是我没有列出的开 ...
- 硬核! Github上 ,star超高的Java 开源项目分享给你!
Awsome JavaGreat Java project on Github(Github 上非常棒的 Java 开源项目). English Version 大家都知道 Github 是一个程序员 ...
- 10个你能参与并学习的Java开源项目
本文转载于:http://code.csdn.net/news/2822604 有很多备受关注的初创开源项目,下面列出十个项目是我觉得非常有趣的,涉及到的学习范围也很广.其中有些还在早期阶段,这对我们 ...
随机推荐
- Spring MVC 3.0 深入及对注解的详细讲解[转载]
http://blog.csdn.net/jzhf2012/article/details/8463783 核心原理 1. 用户发送请求给服务器.url:user.do 2. ...
- ML(2): 术语及算法分类汇总
机器学习术语 归纳总结机器学习相关的基本术语,以一批西瓜的数据为例,例如:(色泽=青绿:根蒂=蜷缩:敲声=浊响),(色泽=乌黑:根蒂=稍蜷:敲声=沉闷),(色泽=浅白:根蒂=硬挺:敲声=清脆)... ...
- .net与com组件
在使用.net中注册组件,有三种方式: 1.手动注册: win9x/NT/2000系统提供一个用于注册进程内组件的实用工具RegSvr32.exe,如regsvr32 c:\test.dll:在.NE ...
- 【Spring-AOP-学习笔记-5】@AfterReturning增强处理简单示例
项目结构 业务代码 @Component("hello") public class HelloImpl implements Hello { // 定义一个简单方法,模拟 ...
- mysql分区表之一:分区原理和优缺点【转】
1.分区表的原理 分区表是由多个相关的底层表实现,这些底层表也是由句柄对象表示,所以我们也可以直接访问各个分区,存储引擎管理分区的各个底层表和管理普通表一样(所有的底层表都必须使用相同的存储引擎),分 ...
- 致Python初学者:Anaconda入门使用指南
http://python.jobbole.com/87522/ Anaconda使用总结 pasting
- python 使用selenium和requests爬取页面数据
目的:获取某网站某用户下市场大于1000秒的视频信息 1.本想通过接口获得结果,但是使用post发送信息到接口,提示服务端错误. 2.通过requests获取页面结果,使用html解析工具,发现麻烦而 ...
- JIRA 的字段配置
默认字段(Default Field Configuration)配置,最好都是非必填. 项目的字段关联字段方案. 字段方案针对不同问题类型,设置不同的字段配置策略. 在每个字段配置策略中去设置自定义 ...
- CGLib缺少jar出现 java.lang.ClassNotFoundException: org.objectweb.asm.Type
CGLib实现动态代理区别于JDK动态代理,不需要目标类实现任何接口,是通过生成代理类子类的方式,而且据说速度要快于JDK动态代理.所以我想要试验一下CGlib的动态代理,网上找了些例子,自己动手写了 ...
- redhat 7安装CentOS 7 yum源
http://www.bubuko.com/infodetail-2004218.html http://www.bubuko.com/infodetail-2004218.html ******** ...