嗨客网搜索
Dubbo入门案例

Dubbo入门教程

介绍

Apache Dubbo 是一款高性能的 Java RPC 框架,可以和 Spring 框架无缝集成。(RPC Remote Process Callable 表示远程调用的意思)

官网

http://dubbo.apache.org/zh-cn/index.html

要素

要素 描述
Provider 服务提供方。
Consumer 服务消费方。
Registry 注册中心。
Monitor 监控中心。

源码下载

链接:https://pan.baidu.com/s/1dLMVkXHua8Lcboj7t_G8hg 提取码:m3ed

前期准备

安装Zookeeper

确保 jdk 环境已经安装,命令如下:

java -version

执行如下图:

01_Dubbo入门案例.png

提前将 Zookeeper 安装包上传到 Linux,然后进入到该目录下:

# 进入目录 cd /usr/software # 查看目录下文件 ls

执行如下图:

02_Dubbo入门案例.png

解压 Zookeeper 文件,命令如下:

# 解压文件 tar -zxvf zookeeper-3.4.6.tar.gz # 查看目录下文件 ls

执行如下图:

03_Dubbo入门案例.png

进入 Zookeeper 目录,命令如下:

# 进入 Zookeeper 目录 cd /usr/software/zookeeper-3.4.6 # 查看目录下文件 ls

执行如下图:

04_Dubbo入门案例.png

进入 conf 目录,命令如下:

# 进入 conf 目录 cd /usr/software/zookeeper-3.4.6/conf # 查看目录下文件 ls

05_Dubbo入门案例.png

把 zoo_sample.cfg 改名为 zoo.cfg,命令如下:

# 文件改名 mv zoo_sample.cfg zoo.cfg # 查看目录下文件 ls

06_Dubbo入门案例.png

修改 zoo.cfg 文件,输入以下命令:

# 修改 zoo.cfg 文件 vim zoo.cfg

修改 dataDir 属性,如下图:

07_Dubbo入门案例.png

进入 bin 目录,启动服务,命令如下:

# 进入bin目录 cd /usr/software/zookeeper-3.4.6/bin # 启动服务 ./zkServer.sh start # 查看服务状态 ./zkServer.sh status

执行结果如下图:

08_Dubbo入门案例.png

可以开启防火墙的 2181 端口,此处我选择关闭防火墙,命令如下:

# 关闭防火墙 systemctl stop firewalld # 查看防火墙状态 systemctl status firewalld

09_Dubbo入门案例.png

创建服务提供者

实现步骤

  1. 创建 Maven 项目:Dubbo_Provider。
  2. 添加依赖。
  3. 编写 web.xml。
  4. 编写 dubbo-provider.xml。
  5. 编写服务接口。
  6. 编写服务实现。
  7. 启动 tomcat,发布项目。

项目结构

创建 Maven 项目,项目结构如下:

10_Dubbo入门案例.png

代码实现

pom 文件:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.haicoder</groupId> <artifactId>Dubbo_Provider</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring.version>5.0.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- dubbo相关 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.6</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.32.Final</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> </project>

web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!--Spring监听器--> <!--读取spirng整合dubbo配置的地址--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo-provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>

dubbo-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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 定义服务的名称 --> <dubbo:application name="dubbodemo_provider" /> <!-- 定义zookeeper连接地址 --> <dubbo:registry address="zookeeper://192.168.245.99:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 定义dubbo注解扫描包 --> <dubbo:annotation package="net.haicoder.service"/> </beans>

log4j.properties 文件:

log4j.rootLogger=debug, stdout, logfile log4j.category.org.springframework=info #log4j.category.org.apache=INFO log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=e:\\log\\myweb.log log4j.appender.logfile.MaxFileSize=1KB log4j.appender.logfile.MaxBackupIndex=5 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

编写服务接口,代码如下:

package net.haicoder.service; public interface HelloService { public String sayHello(String name); }

编写服务实现类,代码如下:

package net.haicoder.service.impl; import com.alibaba.dubbo.config.annotation.Service; import net.haicoder.service.HelloService; @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello " + name; } }

创建服务消费者

实现步骤

  1. 创建 maven 项目: Dubbo_Consumer。
  2. 添加依赖。
  3. 编写 web.xml。
  4. 编写 dubbo-consumer.xml。
  5. 编写服务接口。
  6. 编写控制器,调用服务。

项目结构

创建 web 项目,项目结构如下:

11_Dubbo入门案例.png

代码实现

pom 文件:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.haicoder</groupId> <artifactId>Dubbo_Consumer</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring.version>5.0.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- dubbo相关 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.6</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.32.Final</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> </project>

web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!--核心控制器,扫描dubbo的配置--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo-consumer.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

dubbo-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:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--springmvc配置--> <context:component-scan base-package="net.haicoder.controller"/> <mvc:annotation-driven/> <!--1.服务名称--> <dubbo:application name="dubbo_consumer"></dubbo:application> <!-- 2.zookeeper连接 address:协议 + Linux ip + zookeeper端口 --> <dubbo:registry address="zookeeper://192.168.245.99:2181"></dubbo:registry> <!--3.dubbo注解扫描--> <dubbo:annotation package="net.haicoder.controller"></dubbo:annotation> </beans>

log4j.properties 文件:

log4j.rootLogger=debug, stdout, logfile log4j.category.org.springframework=info #log4j.category.org.apache=INFO log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=e:\\log\\myweb.log log4j.appender.logfile.MaxFileSize=1KB log4j.appender.logfile.MaxBackupIndex=5 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

服务接口,代码如下:

package net.haicoder.service; public interface HelloService { public String sayHello(String name); }

控制器代码如下:

package net.haicoder.controller; import com.alibaba.dubbo.config.annotation.Reference; import net.haicoder.service.HelloService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { //引入服务接口 @Reference private HelloService helloService; @RequestMapping(value="/hello", produces = "application/json;charset=utf-8") @ResponseBody public String hello(){ String haicoderStr = "嗨客网(www.haicoder.net) " + helloService.sayHello("HaiCoder"); return haicoderStr; } }

部署及运行

部署服务提供者到 Tomcat 服务器,服务器名称为 Provider_Tomcat,如下图:

12_Dubbo入门案例.png

部署服务器到 Tomcat 服务器,服务器名称为 Consumer_Tomcat,如下图:

注意:端口号与 Provider_Tomcat 不可冲突

13_Dubbo入门案例.png

启动 Provider_Tomcat,如下图:

14_Dubbo入门案例.png

启动 Consumer_Tomcat,如下图:

15_Dubbo入门案例.png

浏览器访问服务,如下图:

16_Dubbo入门案例.png

Dubbo入门案例总结

以上案例运用了 Zookeeper 作为注册中心,使用 Spring 整合 Dubbo 框架,搭建了一个简易的 SOA 架构项目。(注意:Zookeeper 启动后不可关闭)


赞助商链接:


嗨客网顶部