搭建一个简单的本地的dubbo-demo案例
一、创建一个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案例的更多相关文章
- Vue学习——使用vue-cli搭建一个简单的本地vue项目
前提 安装好node.js.npm.vue-cli.为什么要先安装这些,建议查看https://www.cnblogs.com/jixue/p/10673875.html,这个对于vue-cli理解很 ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
- 使用 SpringBoot+Dubbo 搭建一个简单分布式服务
实战之前,先来看几个重要的概念 开始实战之前,我们先来简单的了解一下这样几个概念:Dubbo.RPC.分布式.由于本文的目的是带大家使用SpringBoot+Dubbo 搭建一个简单的分布式服务,所以 ...
- 使用gitblit搭建一个简单的局域网服务器
使用gitblit搭建一个简单的局域网服务器 1.使用背景 现在很多使用github管理代码,但是github需要互联网的支持,而且私有的git库需要收费.有一些项目的代码不能外泄,所以,搭建一个局域 ...
- 初学Node(六)搭建一个简单的服务器
搭建一个简单的服务器 通过下面的代码可以搭建一个简单的服务器: var http = require("http"); http.createServer(function(req ...
- Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token
原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf? 因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...
- Prism for WPF 搭建一个简单的模块化开发框架(一)
原文:Prism for WPF 搭建一个简单的模块化开发框架(一) 最近闲来无事又想搞搞WPF..... 做个框架吧,可能又是半途而废....总是坚持不下来 不废话了, 先看一下工程结构 布局大概是 ...
- 用nodejs搭建一个简单的服务器
使用nodejs搭建一个简单的服务器 nodejs优点:性能高(读写文件) 数据操作能力强 官网:www.nodejs.org 验证是否安装成功:cmd命令行中输入node -v 如果显示版本号表示安 ...
- 【netty】(2)---搭建一个简单服务器
netty(2)---搭建一个简单服务器 说明:本篇博客是基于学习慕课网有关视频教学.效果:当用户访问:localhost:8088 后 服务器返回 "hello netty"; ...
随机推荐
- LindDotNetCore~职责链模式的应用
回到目录 职责链模式 它是一种设计模块,主要将操作流程与具体操作解耦,让每个操作都可以设置自己的操作流程,这对于工作流应用是一个不错的选择! 下面是官方标准的定义:责任链模式是一种设计模式.在责任链模 ...
- Java基础知识回顾之五 ----- 多线程
前言 在上一篇文章中,回顾了Java的集合.而在本篇文章中主要介绍多线程的相关知识.主要介绍的知识点为线程的介绍.多线程的使用.以及在多线程中使用的一些方法. 线程和进程 线程 表示进程中负责程序执行 ...
- Docker进阶之一:Docker介绍与体系结构
一. Docker是什么 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源,基于Linux内核的cgroup,namespace,Union FS 等技术, ...
- DSAPI.网络.网卡信息属性表
DSAPI.网络.网卡信息属性表 其中,带有ReadOnly的属性只可读不可改,不带ReadOnly的属性即可读也可直接修改,如IP地址,Mac地址等 丢弃接收数据包数: 0 丢弃发送数据包数: 0 ...
- I/O输出流基础之FileOutputStream
OutputStream:是所有字节输出流的父类,其作用是:用这个流把网络数据(getOutputStream()),或者内存中的字节数组数据写到文件系统中文件系统(FileOutputStream) ...
- MySQL分组查询与连接查询
一,分组查询 使用ORDER BY子句将表中的数据分成若干组(还是按行显示) 语法: SELECT 字段名[,聚集函数] FROM 表名 [WHERE子句] GROUP BY 要分组的字段名 [ORD ...
- FineReport数据库连接(oracle+plsql)(1)
一. 数据库建表 数据库是Oracle12c,工具是plsql.具体操作百度即可,此处不赘述.(图1) 图1 二. FineReport中建立数据库连接 在上方选项卡中单击服务器,选择定义数据连接 ...
- Handler,Looper,MessageQueue流程梳理
目的:handle的出现主要是为了解决线程间通讯. 举个例子,android是不允许在主线程中访问网络,因为这样会阻塞主线程,影响性能,所以访问网络都是放在子线程中执行,对于网络返回的结果则需要显示在 ...
- MongoDB自学(2)
条件操作符: gt(大于),gte(大于等于),lt(小于),lte(小于等于)E.G:db.People.find({age:{$gt:100}})//查找集合里age大于100的文档 注意:str ...
- 关掉那些windows上因权限无法关闭的服务
ProcessExplorer 下载地址 :https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer 以管理员身 ...