最近想学习dubbo,就利用周末写了一个特别简单的demo,不知道有没有用,先记录一下。

1、安装zookeeper并启动(安装看我上一篇博客https://www.cnblogs.com/huangzhang/p/9219319.html)

2、下载dubbo源码,安装dubbo-admin(安装看我之前的博客https://www.cnblogs.com/huangzhang/p/9219296.html)

这些准备工作做好之后开始写demo代码:

这里用的开发工具是Intellij IDEA,所以利用idea的智能生成一个spring boot项目,这里就不细说了。

搭建好spring boot项目之后,pom.xml文件中引入依赖

<!--引入dubbot-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.10</version>
</dependency>
<!--引入zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入zookeeper客户端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

这里是生产者

写测试接口:

package com.example.demo01.service;
/**
* @description 声明服务接口
* @date 2018/06/23
* @author huangzhang
*/
public interface DubboServiceProvider {
//声明服务方法
public String sayHello(String name);
}

实现该接口:

package com.example.demo01.service.Impl;

import com.example.demo01.service.DubboServiceProvider;
import org.springframework.stereotype.Component; /**
* @author huangzhang
* @description
* @date Created in 2018/6/23 18:13
*/
@Component("dubboServiceProvider")
public class DubboServiceProviderImpl implements DubboServiceProvider {
@Override
public String sayHello(String name) {
return "------hello " + name + "------";
}
}

在resource目录下面加入一个dubbo.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="dubboproviderhello" />
<!--配置服务注册中心,dubbo不仅仅支持zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--声明对外暴露的服务-->
<dubbo:service interface="com.example.demo01.service.DubboServiceProvider" ref="dubboServiceProvider" />
<bean id="demoService" class="com.example.demo01.service.Impl.DubboServiceProviderImpl"></bean>
</beans>

启动类加入注解:

package com.example.demo01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource; @SpringBootApplication
@ImportResource("classpath:/dubbo.xml")
public class Demo01Application { public static void main(String[] args) {
SpringApplication.run(Demo01Application.class, args);
}
}

编写controller:

package com.example.demo01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class HelloController {
@RequestMapping("/index")
@ResponseBody
public String index(){
return "hello world";
}
}

这里项目默认端口是8080,zookeeper默认端口也是8080,所以到application.properties中配置端口

server.port: 8082

然后启动项目,Dubbo-admin管理平台中的注册列表中显示有一个生产者以注册,但是没有消费者,如下图

生产者完成了,然后就是消费者

搭建一个spring boot项目,pom.xml、启动类、service接口都跟生产这一样,在application.properties中配置端口8081

这里配置dubbo.xml的时候有点不一样,不用注入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"
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="dubboproviderhello" />
<!--配置服务注册中心,dubbo不仅仅支持zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--声明服务引用,与服务声明接口类型一致-->
<dubbo:reference interface="com.example.demo01.service.DubboServiceProvider" id="dubboServiceProvider" />
</beans>

编写controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; /**
* @author huangzhang
* @description
* @date Created in 2018/6/23 20:13
*/
@Controller
public class HelloController {
@Resource
private DubboServiceProvider dubboServiceProvider;
@RequestMapping("/index")
@ResponseBody
public String index(){return dubboServiceProvider.sayHello("tom");
}
}

之后Dubbo-admin管理平台就这样了,有了一个消费者

启动消费者,访问消费者提供的index接口,浏览器直接调用就可以了http://localhost:8081/index

这样一个简单的生产者-消费者demo就搭建完成了,这里消费者通过RPC调用了生产者在Dubbo-admin管理平台注册的服务接口。

springboot+Zookeeper+Dubbo入门的更多相关文章

  1. IDEA上创建 Maven SpringBoot + zookeeper +dubbo 实例

    概述 首先声明,本文是学习总结类型的博客内容,如有雷同纯属学习.本位主要结合zookeeper和dubbo做个简单实例.目前来说,一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越 ...

  2. 基于springboot构建dubbo的入门demo

    之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...

  3. SpringBoot+Dubbo+ZooKeeper+Maven入门实践

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11798626.html 注*** 本实例为仅适合初学者,关于dubbo和springboot以 ...

  4. springboot整合dubbo+zookeeper最新详细

    引入 最近和小伙伴做一个比赛,处于开发阶段,因为涉及的服务比较多,且服务需要分开部署在不同的服务器上,讨论之后,打算采用分布式来做,之前学习springboot的时候,部分章节涉及到了springbo ...

  5. springboot整合dubbo\zookeeper做注册中心

    springboot整合dubbo发布服务,zookeeper做注册中心.前期的安装zookeeper以及启动zookeeper集群就不说了. dubbo-admin-2.5.4.war:dubbo服 ...

  6. springboot与dubbo整合入门(三种方式)

    Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...

  7. dubbo入门学习(三)-----dubbo整合springboot

    springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo 一.创建boot-user-service-provider 本篇博文基于上篇 ...

  8. Springboot整合Dubbo和Zookeeper

    Dubbo是一款由阿里巴巴开发的远程服务调用框架(RPC),其可以透明化的调用远程服务,就像调用本地服务一样简单.截至目前,Dubbo发布了基于Spring Boot构建的版本,版本号为0.2.0,这 ...

  9. springboot使用dubbo和zookeeper

    2019-11-17 yls 创建服务接口模块 接口工程只提供接口,不提供实现,在后面的提供者和消费者中使用 在使用接口的模块中只需要写具体实现类,避免了在每个模块中重复编写接口 在接口中引入依赖包 ...

随机推荐

  1. Session.Abandon-Session.Clear-Session.RemoveAll

    System.Web.UI.Page.Session属性和System.Web.HttpContext.Session属性 都是System.Web.SessionState.HttpSessionS ...

  2. javascript总结1:js常见页面消息输出方式 alert confirm console prompt document

    .1 js常见的输出方法: 1-1 alert 警告框 alert("js语法总结"); 1-2 confirm 确认方法 confirm("js语法总结"); ...

  3. Java 集合框架必记框架图

  4. C#多线程编程实战1.7前台线程和后台线程

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. Android SDK下载和更新慢或失败的解决办法

    下载完Android SDK后发现无法更新,原因是我们被墙了,所以需要使用代理来更新,或者直接把dl-ssl.google.com解析的IP改一下就可以了 用文本编辑器打开文件C:\Windows\S ...

  6. 《Servlet和jsp学习指南》 笔记2

    chapter 13 请求和响应的装饰 初步了解Decorator模式: 在不修改一个对象的类的源码的情况下,装饰这个对象的行为. chapter 14 异步处理 异步Servlet和Filter,只 ...

  7. 多线程《七》信号量,Event,定时器

    一 信号量 信号量也是一把锁,可以指定信号量为5,对比互斥锁同一时间只能有一个任务抢到锁去执行,信号量同一时间可以有5个任务拿到锁去执行,如果说互斥锁是合租房屋的人去抢一个厕所,那么信号量就相当于一群 ...

  8. Django的文件上传以及预览、存储

    思路: 文件上传通过前端的input标签,input设置display:none属性. 内容显示需要让前端通过<img>标签读取图片内容,可以通过<label>标签连接< ...

  9. 【bzoj4817】树点涂色 LCT+线段树+dfs序

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  10. python操作RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...