mini-dubbo
简介
迷你版dubbo实现,零配置、易于使用,支持redis、zookeeper注册中心,支持负载均衡、心跳机制、失败重试机制,轻量级ioc容器guice提供对象容器化支持
一、快速入门
1.添加依赖
<dependencies>
<dependency>
<groupId>com.github.applesline</groupId>
<artifactId>mini-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
2.业务接口定义
package org.applesline.api;
public interface GreetingService {
String sayHello(String name);
}
3.业务接口实现
package org.applesline;
import org.applesline.api.GreetingService;
public class GreetingServiceImpl implements GreetingService {
public String sayHello(String name) {
return "hi " + name;
}
}
4.接口与实现类进行绑定
package org.applesline;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import org.applesline.api.GreetingService;
public class ServerConfig extends AbstractModule {
public void configure() {
bind(GreetingService.class).to(GreetingServiceImpl.class).in(Singleton.class);
}
}
5.编写服务端程序并注册服务(guice中注册服务,相当于spring的ioc容器)
package org.applesline;
import org.applesline.mini.dubbo.context.RpcContext;
import org.applesline.mini.dubbo.protocol.Protocol;
public class ServiceProvider {
public static void main(String[] args) {
// 将服务注册到guice中,默认使用本地zookeeper作为注册中心(127.0.0.1:2181),需要确保本地安装并启动了zk
RpcContext.setConfiguration(new ServerConfig());
Protocol protocol = RpcContext.getBean(Protocol.class);
protocol.export();
}
}
6.编写客户端服务
package org.applesline;
import org.applesline.api.GreetingService;
import org.applesline.mini.dubbo.context.RpcContext;
import org.applesline.mini.dubbo.proxy.ProxyFactory;
public class ServiceConsumer {
public static void main(String[] args) {
// 默认使用本地zookeeper作为注册中心(127.0.0.1:2181),需要确保本地安装并启动了zk
RpcContext.initContext();
GreetingService greetingService = ProxyFactory.getProxyObject(GreetingService.class);
System.out.println(greetingService.sayHello("mini-dubbo"));
}
}
7.启动服务端
2020-06-15 12:50:09.020 [INFO]-[client.ZkEventThread 65]-[ZkClient-EventThread-12-127.0.0.1:2181] Starting ZkClient event thread.
2020-06-15 12:50:09.026 [INFO]-[.zookeeper.ZooKeeper 100]-[main] Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
... 省略部分日志
2020-06-15 12:50:09.042 [INFO]-[.zookeeper.ZooKeeper 100]-[main] Client environment:user.dir=D:\github\mini-dubbo-demo
2020-06-15 12:50:09.042 [INFO]-[.zookeeper.ZooKeeper 438]-[main] Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.I0Itec.zkclient.ZkClient@3d299e3
2020-06-15 12:50:09.174 [INFO]-[ec.zkclient.ZkClient 936]-[main] Waiting for keeper state SyncConnected
2020-06-15 12:50:09.189 [INFO]-[zookeeper.ClientCnxn 1032]-[main-SendThread(127.0.0.1:2181)] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2020-06-15 12:50:09.189 [INFO]-[zookeeper.ClientCnxn 876]-[main-SendThread(127.0.0.1:2181)] Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2020-06-15 12:50:09.221 [INFO]-[zookeeper.ClientCnxn 1299]-[main-SendThread(127.0.0.1:2181)] Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x10000002d170006, negotiated timeout = 30000
2020-06-15 12:50:09.224 [INFO]-[ec.zkclient.ZkClient 713]-[main-EventThread] zookeeper state changed (SyncConnected)
2020-06-15 12:50:10.975 [INFO]-[tocol.SimpleProtocol 23]-[main] tcp server started on 192.168.137.1:9658, elapsed time 3.089 s
8.启动客户端
2020-06-15 12:51:05.624 [INFO]-[.zookeeper.ZooKeeper 100]-[main] Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2020-06-15 12:51:05.639 [INFO]-[.zookeeper.ZooKeeper 100]-[main] Client environment:host.name=LAPTOP-L1B3O8F3.mshome.net
... 省略部分日志
2020-06-15 12:51:05.655 [INFO]-[.zookeeper.ZooKeeper 100]-[main] Client environment:user.dir=D:\github\mini-dubbo-demo
2020-06-15 12:51:05.624 [INFO]-[client.ZkEventThread 65]-[ZkClient-EventThread-12-127.0.0.1:2181] Starting ZkClient event thread.
2020-06-15 12:51:05.655 [INFO]-[.zookeeper.ZooKeeper 438]-[main] Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.I0Itec.zkclient.ZkClient@2a3b5b47
2020-06-15 12:51:05.688 [INFO]-[ec.zkclient.ZkClient 936]-[main] Waiting for keeper state SyncConnected
2020-06-15 12:51:05.708 [INFO]-[zookeeper.ClientCnxn 1032]-[main-SendThread(127.0.0.1:2181)] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2020-06-15 12:51:05.716 [INFO]-[zookeeper.ClientCnxn 876]-[main-SendThread(127.0.0.1:2181)] Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2020-06-15 12:51:05.738 [INFO]-[zookeeper.ClientCnxn 1299]-[main-SendThread(127.0.0.1:2181)] Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x10000002d170007, negotiated timeout = 30000
2020-06-15 12:51:05.738 [INFO]-[ec.zkclient.ZkClient 713]-[main-EventThread] zookeeper state changed (SyncConnected)
2020-06-15 12:51:05.769 [INFO]-[er.netty.NettyClient 74]-[main] try to connect 192.168.137.1:9658
2020-06-15 12:51:07.089 [INFO]-[o.invoker.RpcInvoker 14]-[main] 客户端发起rpc调用请求:Invocation{interfaceName='org.applesline.api.GreetingService', methodName='sayHello', parameterType=[java.lang.String], arguments=[mini-dubbo]}
2020-06-15 12:51:07.251 [INFO]-[ler.RpcResultHandler 33]-[nioEventLoopGroup-2-1] 客户端接收rpc结果:Result{returnType='java.lang.String', data=hi mini-dubbo}
hi mini-dubbo
二、 完整示例代码
点击下载:mini-dubbo-demo
三、源码地址
https://github.com/applesline/mini-dubbo.git
四、姐妹篇