从零开始写一个微型dubbo框架 -- 体验篇

3 篇文章 0 订阅
2 篇文章 0 订阅
本文介绍了如何从零开始构建一个微型的Dubbo框架,该框架具有零配置特性,支持Redis和Zookeeper作为注册中心,内置负载均衡、心跳检测和失败重试机制。使用Guice作为轻量级IOC容器。文章详细讲解了添加依赖、定义接口、实现接口、服务注册、客户端编写等步骤,并提供了完整示例代码和源码地址。
摘要由CSDN通过智能技术生成

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

四、姐妹篇

《从零开始写一个微型dubbo框架 – 进阶篇》
《从零开始写一个微型dubbo框架 – 实战篇》
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值