一、创建一个Maven工程,然后创建三个module模块

二、dubbo-api(maven模块)

创建一个api类,命名为ApiService.java

package com.example.service;

public interface ApiService  {
public String say(String hello);
}

三、dubbo-provider(springboot模块)

1.配置pom.xml,添加依赖

<!-- dubbo-api模块打成jar包,然后导入依赖 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 导入阿里的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<!-- 导入zookeeper的依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>

2.创建一个ProviderServiceImpl.java

package com.example.service.imp;

import com.example.service.ApiService;
import org.springframework.stereotype.Service; @Service
public class ProviderServiceImpl implements ApiService { @Override
public String say(String hello) {
System.err.println("provider调用成功");
return hello;
}
}

3.在src/main/java/resources下创建一个provider.xml并配置dubbo的端口

<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
<dubbo:application name="demotest-provider" owner="programmer" organization="dubbox" />
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper,这里配置的adress是集群模式,个人用修改为本地:localhost:2181-->
<dubbo:registry port="8086" protocol="zookeeper" address="172.30.0.177:2181,172.30.0.178:2181,172.30.0.180:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20878" />
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口,黑色字体部分修改为创建类的包目录 -->
<dubbo:service interface="com.example.service.ApiService" ref="apiService" protocol="dubbo" />
<!--具体实现该接口的 bean-->
<bean id="apiService" class="com.example.service.imp.ProviderServiceImpl"/> </beans>

4.在DubboProvider1Application内导入配置的xml文件,然后在运行

5.如果注册成功可以在DUBBO注册中心查看 http://172.30.0.82:4000/ ,账号:root  密码:root

四、dubbo-consumer(spring boot模块)

1.配置pom.xml

<!-- dubbo-api模块打成jar包,然后导入依赖 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 导入阿里的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<!-- 导入zookeeper的依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>

2.创建一个控制类ConsumerController.java

package com.example.controller;

import com.example.service.ApiService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; @RestController
public class ConsumerController {
@Resource
ApiService apiService; @GetMapping("/consumer")
public String consumer() {
return apiService.say("hello");
}
}

3.创建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="demotest-consumer"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
<dubbo:registry protocol="zookeeper" address="172.30.0.177:2181,172.30.0.178:2181,172.30.0.180:2181"/>
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
<dubbo:reference id="apiService" interface="com.example.service.ApiService"/>
</beans>

4.配置application.properties文件

#配置访问端口
server.port=8085

5.运行DubboConsumer1Application 

五、打开浏览器,输入 localhost:8085/consumer

访问成功,做完后,最好将api,provider做成一个项目,consumer做成另一个项目,consumer通过RPC的方式调用provider,这样感知dubbo更明显

搭建一个简单的本地的dubbo-demo案例的更多相关文章

  1. Vue学习——使用vue-cli搭建一个简单的本地vue项目

    前提 安装好node.js.npm.vue-cli.为什么要先安装这些,建议查看https://www.cnblogs.com/jixue/p/10673875.html,这个对于vue-cli理解很 ...

  2. 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务

    来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...

  3. 使用 SpringBoot+Dubbo 搭建一个简单分布式服务

    实战之前,先来看几个重要的概念 开始实战之前,我们先来简单的了解一下这样几个概念:Dubbo.RPC.分布式.由于本文的目的是带大家使用SpringBoot+Dubbo 搭建一个简单的分布式服务,所以 ...

  4. 使用gitblit搭建一个简单的局域网服务器

    使用gitblit搭建一个简单的局域网服务器 1.使用背景 现在很多使用github管理代码,但是github需要互联网的支持,而且私有的git库需要收费.有一些项目的代码不能外泄,所以,搭建一个局域 ...

  5. 初学Node(六)搭建一个简单的服务器

    搭建一个简单的服务器 通过下面的代码可以搭建一个简单的服务器: var http = require("http"); http.createServer(function(req ...

  6. Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

    原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf?   因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...

  7. Prism for WPF 搭建一个简单的模块化开发框架(一)

    原文:Prism for WPF 搭建一个简单的模块化开发框架(一) 最近闲来无事又想搞搞WPF..... 做个框架吧,可能又是半途而废....总是坚持不下来 不废话了, 先看一下工程结构 布局大概是 ...

  8. 用nodejs搭建一个简单的服务器

    使用nodejs搭建一个简单的服务器 nodejs优点:性能高(读写文件) 数据操作能力强 官网:www.nodejs.org 验证是否安装成功:cmd命令行中输入node -v 如果显示版本号表示安 ...

  9. 【netty】(2)---搭建一个简单服务器

    netty(2)---搭建一个简单服务器 说明:本篇博客是基于学习慕课网有关视频教学.效果:当用户访问:localhost:8088 后 服务器返回 "hello netty"; ...

随机推荐

  1. springboot~yml里的自定义配置

    主要介绍三种,字符串配置,数组配置和带默认值的配置 字符串配置 //yml setString: hello /** * 字符串. */ @Value("${setString}" ...

  2. docker (2) 通用/镜像命令

    Docker 的常用命令: (1)Docker  help 命令: 可以查看有关docker的所有操作命令: (2)docker COMMAND -–help 查看docker 的某项命令的帮助文档 ...

  3. 18-09-20,String 与 StringBuilder (StringBuffer)

    1.其一 在运行速度方面:StringBuilder > StringBuffer > String 上实例 class Program { static void Main(string ...

  4. nginx负载均衡指令least_conn的真正含义

    负载均衡指令least_conn的含义,按照nginx文档的说法: Specifies that a group should use a load balancing method where a ...

  5. Ajax常见面试题

    1,什么是ajax? 为什么要使用ajax? 1.ajax是"asynchornous javascript and xml "的缩写,指一种创建交互式网页应用的网页开发技术. 2 ...

  6. SQL Server使用sys.master_files计算tempdb大小不正确

    一直习惯使用sys.master_files来统计数据库的大小以及使用情况,但是发现sys.master_files不能准确统计tempdb的数据库大小信息.如下所示: SELECT       da ...

  7. SQL Server系统表sysobjects介绍

    SQL Server系统表sysobjects介绍 sysobjects 表结构: 列名 数据类型 描述 name sysname 对象名,常用列 id int 对象标识号 xtype char(2) ...

  8. Zabbix3.4-RHEL 7.4 X64 YUM联网安装

    OS准备 关闭selinux vi /etc/selinux/config setenforce 0 开启防火墙80端口访问 firewall-cmd --permanent --add-rich-r ...

  9. iOS中Safari浏览器select下拉列表文字太长被截断的处理方法

    网页中的select下拉列表,文字太长的话在iOS的Safari浏览器里会被自动截断,显示成下面这种: 安卓版的浏览器则没有这个问题. 如何让下拉列表中的文字在iOS的Safari浏览器里显示完整呢? ...

  10. java网络爬虫基础学习(二)

    正则表达式 正则表达式写法 含义 \d 代表0-9的任意数字 \D 代表任何非数字字符 \s 代表空格类字符 \S 代表非空格类字符 \p{Lower} 代表小写字母[a-z] \p{Upper} 代 ...