8 快速入门 dubbo

所需资料

注册中心 Zookeeper

安装 zookeeper

  • 官方推荐使用 zookeeper 注册中心;
  • 注册中心负责服务地址的注册与查找,相当于目录服务;
  • 服务提供者和消费者只在启动时与注册中心交互,注册中不转发请求,压力较小;
  • Zookeeper 是 apache hadoop 的子项目,是一个树形的目录服务,支持变更推送,适合作为

    dubbo 的服务注册中心,工业强度较高,可用于生产环境;

入门 demo 的架构

注意

如果在粘贴这些web.xml 或者pom.xml里面build里面的标签发送这样的报错提示,注意哦 这可能不是错误,不会影响到我们项目的运行。

服务提供者

1、一个空的maven项目

2、提供一个服务接口即可

项目目录结构

图中红框的需要我们创建

提供方的pom.xml

各种依赖请严格按照下面的版本

  • 记得要更新pom文件哦!
  • 当dependencies中出现依赖的版本信息 说明依赖引入成功了
    <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.6.RELEASE</spring.version>
</properties> <packaging>war</packaging> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8001</port>
<path>/</path>
</configuration>
<executions>
<execution>
<!-- 打包完成后,运行服务 -->
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
提供方接口
public interface HelloService {
String sayHello(String name);
}
暴露的提供方实现

@Service 这个注解不是spring的哦

@com.alibaba.dubbo.config.annotation.Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello," + name + "!!!";
}
}
服务提供方的配置文件spring.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">
<!--1.服务提供方在zookeeper中的“别名”-->
<dubbo:application name="dubbo-server"/>
<!--2.注册中心的地址-->
<dubbo:registry address="zookeeper://192.168.77.132:2181"/>
<!-- 让监控 去注册中心 自动找服务 -->
<dubbo:monitor protocol="registry"/>
<!--3.扫描类(将什么包下的类作为服务提供类)-->
<dubbo:annotation package="service.impl"/>
</beans>
提供方的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--使用上下文监听器-初始化项目环境-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring.xml</param-value>
</context-param>
</web-app>

服务消费方

项目目录结构

消费方的pom.xml

与服务方一致,只需要修改tomcat的端口为8002

消费方的Controller

因为我们是通过浏览器去访问的,所以要创建controller层,提供对外访问的接口

@RestController
public class HelloAction {
@com.alibaba.dubbo.config.annotation.Reference
private HelloService hs; @RequestMapping("hello/{name}")
@ResponseBody
public String hello(@PathVariable String name) {
return hs.sayHello(name);
}
}
消费方的接口

注意:

  • controller中要依赖HelloService,所以我们创建一个接口;
  • 这里是消费方,不需要实现,因为实现会让服务方为我们搞定!跟8001服务提供方的线程远程通信
public interface HelloService {
String sayHello(String name);
}
消费方的springmvc.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的应用名称,通常使用项目名 -->
<dubbo:application name="dubbo-consumer"/>
<!--配置Dubbo的注册中心地址 -->
<dubbo:registry address="zookeeper://192.168.77.132:2181"/>
<!-- 让监控 去注册中心 自动找服务 -->
<dubbo:monitor protocol="registry"/>
<!--配置Dubbo扫描类,将这个类作为服务进行发布 -->
<dubbo:annotation package="controller"/>
</beans>
消费方的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

启动测试

还是跟是实现zookeeper实现分布式锁一样启动哦

  • 我们在这里配置了打包之后直接运行
  • 所以我们执行打包命令即可,如果出错,先执行clean在执行package

如果执行出错,或者访问失败可以去查找以下问题

  • @service注解是否是dubbo的
  • linux服务器的防火墙是否关闭
  • zookeeper的注册地址是否有误 linux查看本机ip 【ip address】
  • 如果想看到自己的服务,可以先搭建dubbo的可视化工具,文章地址

当出现端口地址 且上面没有报错信息时,说明启动成功

可以看到我们的请求成功了

# 8 快速入门 dubbo的更多相关文章

  1. Spring Boot 2.0 的快速入门(图文教程)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ...

  2. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  3. SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)

     SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...

  4. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)

    今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...

  5. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  6. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  7. Mybatis框架 的快速入门

    MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...

  8. grunt快速入门

    快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...

  9. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

随机推荐

  1. Jenkins Build step 'Execute shell' marked build as failure

    问题出现: Jenkins一直都构建成功,今天突然报错:Jenkins Build step 'Execute shell' marked build as failure 问题原因: By defa ...

  2. Hadoop(四)C#连接Hive

    Hive Hive将HiveQL(类sql语言)转为MapReduce,完成数据的查询与分析,减少了编写MapReduce的复杂度.它有以下优点: 学习成本低:熟悉sql就能使用 良好的数据分析:底层 ...

  3. WPF中的依赖属性

    1. WPF中的依赖属性 依赖属性是专门基于WPF创建的.在WPF库实现中,依赖属性使用普通的C#属性进行了包装,使用方法与普通的属性是相同的. 1.1 依赖属性提供的属性功能 资源 数据绑定 样式 ...

  4. MySQL闪回工具之binlog2sql

    一.binlog2sql 1.1 安装binlog2sql git clone https://github.com/danfengcao/binlog2sql.git && cd b ...

  5. Django序列化组件与数据批量操作与简单使用Forms组件

    目录 SweetAlert前端插件 Django自带的序列化组件 批量数据操作 分页器与推导流程 Forms组件之创建 Forms组件之数据校验 Forms组件之渲染标签 Forms组件之信息展示 S ...

  6. break、continue、return中选择一个,我们结束掉它

      在平时的开发过程中,经常会用到循环,在写循环的过程中会有很多判断条件及逻辑,你知道如何结束一个循环吗?在java中有break.continue.reture三个关键字都可以结束循环,我们看下他们 ...

  7. SeataAT模式原理

    Seata架构 Seata将分布式事务理解为一个全局事务,它由若干个分支事务组成,一个分支事务就是一个满足ACID的本地事务. Seata架构中有三个角色: TC (Transaction Coord ...

  8. 腾讯云Redis全面升级,性能提升400%,可用性高达5个9

    2022年6月,腾讯云Redis全新升级,发布高性能版本,单节点可提供50W+吞吐,性能是原生Redis的4倍.同时,腾讯云Redis推出全球复制功能,解决原生Redis诸多痛点问题,可用性升级高达9 ...

  9. QT5 QSS QML界面美化视频课程系列 QT原理 项目实战 C++1X STL

    QT5 QSS QML界面美化视频课程系列 QT原理 项目实战 C++1X STL 课程1   C语言程序设计高级实用速成课程 基础+进阶+自学 课程2   C语言程序设计Windows GDI图形绘 ...

  10. 『忘了再学』Shell流程控制 — 36、for循环介绍

    目录 1.for循环介绍 2.示例 语法一举例: 语法二举例: 3.for循环总结 4.练习:批量解压缩脚本 方式一:批量解压缩 方式二:批量解压缩 1.for循环介绍 for循环是固定循环,也就是在 ...