Eagle分布式rpc调用,借助Zookeeper实现服务注册和发现,基于AQS实现高性能连接池,支持监控、过载保护等配置。
github地址:https://github.com/fang-yan-peng/eagle。 github上提供完整的例子和源代码。
特点
- 借助Zookeeper实现服务注册和发现。
- 基于AQS实现高性能连接池。
- 支持同步和异步回调两种机制。
- 提供接口方法请求时间、tps等监控信息。
- 提供和自定义服务端过载保护策略。
jmh基准测试结果
运行基准测试步骤:
- cd eagle-benchmark
- mvn clean install
- cd eagle-benchmark-server/target
- tar -zxvf eagle-benchmark-server-1.0-assembly.tar.gz
- cd eagle-benchmark-server-1.0
- bin/start.sh
- cd eagle-benchmark/eagle-benchmark-client
- 在linux上运行 sh benchmark.sh,在window上运行 benchmark.cmd
例子
eagle支持spring、springboot框架,配置使用十分简单,以下以同步调用为例,具体的例子在github上都有。
同步调用
-
添加依赖.
<dependency> <groupId>org.jfaster.eagle</groupId> <artifactId>eagle-core</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.jfaster.eagle</groupId> <artifactId>eagle-registry-zookeeper</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.jfaster.eagle</groupId> <artifactId>eagle-transport-netty</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.jfaster.eagle</groupId> <artifactId>eagle-spring-support</artifactId> <version>1.0</version> </dependency>
-
创建一个接口类。
src/main/java/eagle/jfaster/org/service/Calculate.java
package eagle.jfaster.org.service; public interface Calculate { int add(int a,int b); int sub(int a,int b); }
-
实现接口,并暴露服务。
src/main/java/eagle/jfaster/org/service/impl/CalculateImpl.java
package eagle.jfaster.org.service.impl; import eagle.jfaster.org.service.Calculate; import org.springframework.stereotype.Service; @Service("calculate") public class CalculateImpl implements Calculate { public int add(int a, int b) { return a+b; } public int sub(int a, int b) { return a-b; } }
src/main/resources/server.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:eagle="http://eagle.jfaster.org/schema/eagle" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://eagle.jfaster.org/schema/eagle http://eagle.jfaster.org/schema/eagle/eagle.xsd "> <context:component-scan base-package="eagle.jfaster.org" /> <context:annotation-config/> <!--注册中心配置可以多个--> <eagle:registry name="regCenter" protocol="zookeeper" address="127.0.0.1:4181" namespace="eagle" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3"/> <!--协议配置--> <eagle:protocol id="proto" name="eagle" serialization="kryo" use-default="true" max-content-length="16777216" max-server-connection="20000" core-worker-thread="20" max-worker-thread="200" worker-queue-size="10"/> <eagle:base-service id="baseService" group="eagleGroup" export="proto:9200" registry="regCenter"/> <eagle:service interface="eagle.jfaster.org.service.Calculate" ref="calculate" base-service="baseService" export="proto:9300,proto:9400"/>
src/main/java/eagle/jfaster/org/Server.java
package eagle.jfaster.org.server; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.concurrent.CountDownLatch; public class Server { public static void main(String[] args) throws InterruptedException { EmbedZookeeperServer.start(4181); ApplicationContext appCtx = new ClassPathXmlApplicationContext("server.xml"); CountDownLatch latch = new CountDownLatch(1); latch.await(); } }
执行main方法,就会在9300和9400端口发布服务。同时eagle还提供了eagle.jfaster.org.container.Main类,会跟据环境变量eagle.container的设置启动不同的容器。 如果没有配置会默认启动SpringContainer,会加载classpath*:META-INF/spring/*.xml的所有spring配置文件。
-
创建和启动客户端
src/main/resources/client_sync.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:eagle="http://eagle.jfaster.org/schema/eagle" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://eagle.jfaster.org/schema/eagle http://eagle.jfaster.org/schema/eagle/eagle.xsd "> <context:component-scan base-package="eagle.jfaster.org" /> <context:annotation-config/> <!--注册中心配置可以多个--> <eagle:registry name="regCenter" protocol="zookeeper" address="127.0.0.1:4181" namespace="eagle" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3"/> <!--协议配置--> <eagle:protocol name="eagle" serialization="kryo" use-default="true" cluster="eagle" max-content-length="16777216"/> <eagle:base-refer id="baseRefer" request-timeout="300" actives="20000" actives-wait="300" loadbalance="roundrobin" ha-strategy="failfast" protocol="eagle" registry="regCenter" compress="false" group="eagleGroup" connect-timeout="10000"/> <eagle:refer id="cal" interface="eagle.jfaster.org.service.Calculate" base-refer="baseRefer" max-invoke-error="10" max-client-connection="200" />
src/main/java/eagle/jfaster/org/client/SyncClient.java
package eagle.jfaster.org.client; import eagle.jfaster.org.service.Calculate; import eagle.jfaster.org.service.HelloWorld; import eagle.jfaster.org.service.Notify; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SyncClient { public static void main(String[] args) { ApplicationContext appCtx = new ClassPathXmlApplicationContext("client_sync.xml"); Calculate calculate = appCtx.getBean("cal",Calculate.class); System.out.println(calculate.add(1,3)); System.out.println(calculate.sub(8,3)); } }
执行main方法,就会在控制台打印出信息。