分布式学习系列【dubbo入门实践】

dubbo架构

  

  组成部分:provider,consumer,registry,monitor; provider,consumer注册,订阅类似于消息队列的注册订阅

  

一、环境安装

1、dubbo admin 管理控制台安装(Windows环境)

#下载dubbo-admin-2.5.3.war,部署到tomcat下,根据需要编辑WEB-INF/dubbo.properties文件:

  dubbo.registry.address=zookeeper://127.0.0.1:2181

  dubbo.admin.root.password=root

  dubbo.admin.guest.password=guest

  #dubbo admin 部署问题:版本2.5.3启动报错:Bean property 'URIType' is not writable or has an invalid

  原因:使用了JDK1.8;

  解决方法:将环境变量JAVA_HOME设置为JDK1.7;

2、zookeeper 安装

  dubbo对注册中心实现了抽象,实现的注册中心有:ZooKeeper,Redis,Multicast;这里采用zookeeper进行演示。

  下载zookeeper解压,在zookeeper->conf目录下复制一份zoo_sample.cfg为zoo.cfg,修改配置

  添加配置:(默认不会自动创建,需要手动添加)

  dataDir=D:\\zookeeper\\data   
  dataLogDir=D:\\zookeeper\\log

  下载后windows下直接进入doc,进入zookeeper->bin目录运行zkServer.cmd命令;

可以用netstat -ano|findstr "2181"检查是否启动成功  

   TCP 0.0.0.0: 0.0.0.0: LISTENING
  TCP 127.0.0.1: 127.0.0.1: ESTABLISHED
  TCP 127.0.0.1: 127.0.0.1: ESTABLISHED
  TCP [::]: [::]: LISTENING

也可用JPS查看:  

 SmartGit
   Jps
   QuorumPeerMain
   Bootstrap

3、启动tomcat访问:localhost:8080

 二、实践步骤

#创建服务接口  

public interface DemoService {
String sendMessage(String message);
}

#创建接口实现

public class DemoServiceImpl implements DemoService {
@Override
public String sendMessage(String message){
System.out.println("this demoService get message:"+message);
return "provirder return message success";
}
}

#创建提供方spring配置文件provider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-demo-test" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.demo.DemoService" />
</beans>

#创建提供方启动程序:DemoProvider

public class DemoProvider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.out.println("provider started ...");
System.in.read();
}
}

#创建消费方spring配置文件consumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-demo-test" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.demo.DemoService" />
</beans>

#创建消费方方启动程序:DemoConsumer   

public class DemoConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String returnMessage = demoService.sendMessage("consumer send message: hello my first dubbo service");
System.out.println("provider return message:"+returnMessage);
}
}

  

#添加项目maven依赖

分别加入spring IOC,zkClient,dubbo依赖

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

  

#启动提供方服务,消费方进行调用

运行启动提供方DemoProvider,启动后进入待服务状态

观察dubbo admin发现已经刚才的启动服务在zookeepr成功注册

这时消费方可以进行调用,运行DemoConsumer

此时服务端收到请求:

调用方收到响应:

附上示例代码:  http://files.cnblogs.com/files/yuxuan/dubbo-demo.rar

分布式学习系列【dubbo入门实践】的更多相关文章

  1. 分布式服务框架dubbo入门实例

    dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...

  2. 最火的分布式 HTAP 数据库 TiDB - 入门实践教程

    偶然在某篇博客看到了 TiDB,一个融合 OLTP 和 OLAP 的分布式开源数据库, GitHub 上 Star 很多,然后 watch 了,发现 commit 和 pull request 一直都 ...

  3. 阿里分布式开源框架DUBBO 入门+ 进阶+ 项目实战视频教程

    史诗级Java/JavaWeb学习资源免费分享 欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享各种Java学习资源,面试题,优质文章,以及企业级Java实战项目回 ...

  4. 分布式服务框架Dubbo入门案例和项目源码

    本项目源代码:http://download.csdn.net/detail/fansunion/9498406 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案, 是 ...

  5. MongoDB学习系列(1)--入门介绍

    MongoDB是一款为Web应用程序设计的面向文档结构的数据库系统. MongoDB贡献者是10gen公司.地址:http://www.10gen.com 1.MongoDB主要特性: 1.1文档数据 ...

  6. shell脚本学习系列之一---入门

    参考:http://me.52fhy.com/shell-book/ 待后续整理...

  7. SpringCloud系列之分布式配置中心极速入门与实践

    SpringCloud系列之分布式配置中心极速入门与实践 @ 目录 1.分布式配置中心简介 2.什么是SpringCloud Config? 3.例子实验环境准备 4.Config Server代码实 ...

  8. Dubbo学习系列之八(分布式事务之MQ方案)

    自从小王玩起了微服务,发现微服务果然很强大,好处真是太多,心中暗喜,然而,却也遇到了分布式中最棘手的问题:分布式事务.小王遍访各路神仙,也无个完美开源解决方案,当然,也有些实际可行的手法,虽不算完美, ...

  9. Dubbo学习系列之十五(Seata分布式事务方案TCC模式)

    上篇的续集. 工具: Idea201902/JDK11/Gradle5.6.2/Mysql8.0.11/Lombok0.27/Postman7.5.0/SpringBoot2.1.9/Nacos1.1 ...

随机推荐

  1. Asp.net Boilerplate之AbpSession扩展

    当前Abp版本1.2,项目类型为MVC5. 以属性的形式扩展AbpSession,并在"记住我"后,下次自动登录也能获取到扩展属性的值,版权归"角落的白板报"所 ...

  2. netcore - MVC的ActionFilter的使用

    经过一周的时间没有分享文章了,主要是在使用.netcore做一个小的项目,项目面向大众用户的增删改查都做的差不多了,打算本周在云服务器上部署试试,很期待,也希望上线后大家多多支持:以上纯属个人废话,来 ...

  3. 从netty-example分析Netty组件续

    上文我们从netty-example的Discard服务器端示例分析了netty的组件,今天我们从另一个简单的示例Echo客户端分析一下上个示例中没有出现的netty组件. 1. 服务端的连接处理,读 ...

  4. Tomcat常见问题及常用命令

    很长时间不用tomcat好多命令都忘记了,所以准备自己记录下来,以便参考.刚好也希望可以开始养成记博客的好习惯. 1.查看java的版本号 进入java的安装目录后,使用命令:java -versio ...

  5. ResponsibleChain(责任链模式)

    /** * 责任链模式 * @author TMAC-J * 老板讲任务交给CTO,CTO自然不会亲自去做,又把人物分配给项目经理,项目经理再把任务分配给组长,组长再分配给个人 * 如果中途哪个环节出 ...

  6. 使用github远程仓库

    经过几天对github的研究,终于把自己想完成的给解决了,发现google真的有很多解释,但是很多也会出现一些bug,对于初学者真的很多烦恼,所以整理一份,能给初识github的你有所帮助 一,首先, ...

  7. SEED实验系列文章目录

    美国雪城大学SEEDLabs实验列表 SEEDLabs是一套完整的信息安全实验,涵盖本科信息安全教学中的大部分基本原理.项目组2002年由杜文亮教授创建,目前开发了30个实验,几百所大学已采用.实验楼 ...

  8. Git的四个基本概念及 git的工作流程

  9. vs生成pro

    1.修改.vcxproj文件   <PropertyGroup Label="Globals">    <ProjectGuid>{AAAA4039-13B ...

  10. CentOs7 +Jexus 5.8.2部署Asp.Net Core WebApi 1.0生产环境

    Jexus 是一款运行于 Linux 平台,以支持  ASP.NET.PHP 为特色的集高安全性和高性能为一体的 WEB 服务器和反向代理服务器.最新版 5.8.2 已经发布,有如下更新: 1,现在大 ...