分布式服务框架Dubbo入门实例

男娘i 2022-09-23 03:56 323阅读 0赞

Dubbo的官网中是这样介绍Dubbo的。DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

本文是对Dubbo的框架的一次学习步骤记录。

所需环境:Centos7 x64,Win7 x64,eclipse,maven,zookeeper,Dubbo

1.zookeeper安装与配置

先到 zookeeper官网下载一个zookeeper安装文件下来,我这里下载3.4.8稳定版。

在Linux系统中运行:

wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz

解压:

#tar zxvf zookeeper-3.4.8.tar.gz

配置:

切换到解压目录,进入conf目录,复制默认的zoo_sample.cfg文件,改名为zoo.cfg

#cp zoo_sample.cfg zoo.cfg

  1. [root@localhost conf]# ll
  2. 总用量 16
  3. -rw-rw-r-- 1 jk145 jk145 535 2 6 11:46 configuration.xsl
  4. -rw-rw-r-- 1 jk145 jk145 2161 2 6 11:46 log4j.properties
  5. -rw-r--r-- 1 root root 1056 7 3 21:21 zoo.cfg
  6. -rw-rw-r-- 1 jk145 jk145 922 2 6 11:46 zoo_sample.cfg
  7. [root@localhost conf]#

之后切换到zookeeper的bin 目录,运行zookeeper就好。

在bin目录下,执行./zkServer.sh start

  1. [root@localhost bin]# pwd
  2. /usr/local/zookeeper/zookeeper-3.4.8/bin
  3. [root@localhost bin]# ./zkServer.sh start
  4. ZooKeeper JMX enabled by default
  5. Using config: /usr/local/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg
  6. Starting zookeeper ... STARTED
  7. [root@localhost bin]#

2.Dubbo服务端搭建

新建立一个manven工程,可以是jar也可以是war工程。

在pom中加入依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.haiyang</groupId>
  5. <artifactId>dubbo</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <properties>
  9. <spring.version>4.1.3.RELEASE</spring.version>
  10. <dubbo.version>2.5.3</dubbo.version>
  11. <zookeeper.version>3.4.8</zookeeper.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>com.alibaba</groupId>
  16. <artifactId>dubbo</artifactId>
  17. <version>${dubbo.version}</version>
  18. <exclusions>
  19. <exclusion>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring</artifactId>
  22. </exclusion>
  23. </exclusions>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.zookeeper</groupId>
  27. <artifactId>zookeeper</artifactId>
  28. <version>${zookeeper.version}</version>
  29. </dependency>
  30. <!-- Spring -->
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-beans</artifactId>
  34. <version>${spring.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-webmvc</artifactId>
  39. <version>${spring.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-aspects</artifactId>
  44. <version>${spring.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>com.github.sgroschupf</groupId>
  48. <artifactId>zkclient</artifactId>
  49. <version>0.1</version>
  50. </dependency>
  51. </dependencies>
  52. <build>
  53. <plugins>
  54. <!-- java编译插件 -->
  55. <plugin>
  56. <groupId>org.apache.maven.plugins</groupId>
  57. <artifactId>maven-compiler-plugin</artifactId>
  58. <version>3.2</version>
  59. <configuration>
  60. <source>1.7</source>
  61. <target>1.7</target>
  62. <encoding>UTF-8</encoding>
  63. </configuration>
  64. </plugin>
  65. </plugins>
  66. </build>
  67. </project>

3.Dubbo服务端服务代码

服务接口类:

  1. package com.haiyang.service;
  2. public interface HelloService {
  3. String sayHello(String str);
  4. }

服务实现类:

  1. package com.haiyang.service.impl;
  2. import org.springframework.stereotype.Service;
  3. import com.haiyang.service.HelloService;
  4. @Service("helloService")
  5. public class HelloServiceImpl implements HelloService {
  6. public String sayHello(String str) {
  7. return "Hello " + str;
  8. }
  9. }

4.Dubbo服务端Spring文件配置

Spring-context.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  6. http://www.springframework.org/schema/aop
  7. http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.2.xsd"
  12. default-autowire="byName" default-lazy-init="false">
  13. <!-- 采用注释的方式配置bean -->
  14. <context:annotation-config />
  15. <!-- 配置要扫描的包 -->
  16. <context:component-scan base-package="com.haiyang" />
  17. <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->
  18. <aop:aspectj-autoproxy proxy-target-class="true" />
  19. <import resource="dubbo-provider.xml" />
  20. </beans>

dubbo-provider.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://code.alibabatech.com/schema/dubbo
  7. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  8. <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
  9. <dubbo:application name="test_provider" />
  10. <!-- 使用zookeeper注册中心暴露服务地址 -->
  11. <dubbo:registry protocol="zookeeper"
  12. address="zookeeper://192.168.138.129:2181" check="false" subscribe="false"
  13. register=""></dubbo:registry>
  14. <!-- 用dubbo协议在20880端口暴露服务 -->
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <!-- 用户服务接口 -->
  17. <dubbo:service interface="com.haiyang.service.HelloService"
  18. ref="helloService" />
  19. </beans>

5.Dubbo服务端测试类

  1. package com.haiyang;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class DubboProvider {
  4. public static void main(String[] args) {
  5. try {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
  7. context.start();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. synchronized (DubboProvider.class) {
  12. while (true) {
  13. try {
  14. DubboProvider.class.wait();
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20. }
  21. }

5.Dubbo客户端搭建

和上面的Dubbo服务端一样,加入相同的jar文件,另外,再加入上一个工程,将它作为一个依赖,同时记得将上面的Dubbo服务工程用maven install下

pom清单如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.haiyang</groupId>
  5. <artifactId>dubbo-use</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <properties>
  9. <spring.version>4.1.3.RELEASE</spring.version>
  10. <dubbo.version>2.5.3</dubbo.version>
  11. <zookeeper.version>3.4.8</zookeeper.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>com.alibaba</groupId>
  16. <artifactId>dubbo</artifactId>
  17. <version>${dubbo.version}</version>
  18. <exclusions>
  19. <exclusion>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring</artifactId>
  22. </exclusion>
  23. </exclusions>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.zookeeper</groupId>
  27. <artifactId>zookeeper</artifactId>
  28. <version>${zookeeper.version}</version>
  29. </dependency>
  30. <!-- Spring -->
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-beans</artifactId>
  34. <version>${spring.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-webmvc</artifactId>
  39. <version>${spring.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-aspects</artifactId>
  44. <version>${spring.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>com.github.sgroschupf</groupId>
  48. <artifactId>zkclient</artifactId>
  49. <version>0.1</version>
  50. </dependency>
  51. <!-- 服务类 -->
  52. <dependency>
  53. <groupId>com.haiyang</groupId>
  54. <artifactId>dubbo</artifactId>
  55. <version>0.0.1-SNAPSHOT</version>
  56. </dependency>
  57. </dependencies>
  58. <build>
  59. <plugins>
  60. <!-- java编译插件 -->
  61. <plugin>
  62. <groupId>org.apache.maven.plugins</groupId>
  63. <artifactId>maven-compiler-plugin</artifactId>
  64. <version>3.2</version>
  65. <configuration>
  66. <source>1.7</source>
  67. <target>1.7</target>
  68. <encoding>UTF-8</encoding>
  69. </configuration>
  70. </plugin>
  71. </plugins>
  72. </build>
  73. </project>

6.Dubbo客户端Spring配置

spring-context.xml配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  6. http://www.springframework.org/schema/aop
  7. http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.2.xsd"
  12. default-autowire="byName" default-lazy-init="false">
  13. <!-- 采用注释的方式配置bean -->
  14. <context:annotation-config />
  15. <!-- 配置要扫描的包 -->
  16. <context:component-scan base-package="com.haiyang" />
  17. <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->
  18. <aop:aspectj-autoproxy proxy-target-class="true" />
  19. <import resource="dubbo-consumer.xml" />
  20. </beans>

dubbo-consumer.xml配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://code.alibabatech.com/schema/dubbo
  7. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  8. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  9. <dubbo:application name="dubbo-use" />
  10. <!-- 使用zookeeper注册中心暴露服务地址 -->
  11. <!-- 注册中心地址 -->
  12. <dubbo:registry protocol="zookeeper" address="192.168.138.129:2181" />
  13. <!-- 用户服务接口,和服务类名对应 -->
  14. <dubbo:reference interface="com.haiyang.service.HelloService"
  15. id="helloService" />
  16. </beans>

7.Dubbo客户端测试类

  1. package com.haiyang.test;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.haiyang.service.HelloService;
  4. public class HaiTest {
  5. public static void main(String[] args) {
  6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  7. new String[] { "spring/dubbo-consumer.xml" });
  8. context.start();
  9. HelloService demoService = (HelloService) context.getBean("helloService");
  10. System.out.println(demoService.sayHello("world"));
  11. synchronized (DubboTest.class) {
  12. while (true) {
  13. try {
  14. DubboTest.class.wait();
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20. }
  21. }

8.Dubbo整体测试

为了观察方便,先在zookeeper的服务器上查看日志文件,对应目录为:

{path}/zookeeper/bin/zookeeper.out

用tail -f命令监听此日志文件

[ping@javaEE bin]$ tail -f zookeeper.out

同时,为了观察方便,区别下,将之前服务端HelloServiceImpl这个类的return “Hello:” + str;改为return “Hello Dubbo:” + str;

运行Dubbo服务端测试类(DubboProvider.java)

Center

运行Dubbo客户端测试类(HaiTest.java)

Center 1

从这里可以看出,成功调用了服务端的方法

同时,zookeeper监听的日志文件也记录到了日志信息。

Center 2

9.Dubbo管理控制台安装

同时,为了Dubbo也自带了管理控制台,在这里,也顺便安装下。

在百度上搜一下dubbo-admin-2.5.4.war ,将它下下来,再传到Linux中去.

在crt中输入rz,选择dubbo-admin-2.5.4.war文件,上传,并将它解压到linux的tomcat的webapps目录下。

运行unzip -d解压,将它解压,并将名称改为ROOT

$ unzip dubbo-admin-2.5.4.war -d ROOT

再进入解压后的ROOT目录下,修改配置文件

$ vim ROOT/WEB-INF/dubbo.properties

dubbo.registry.address=zookeeper://192.168.138.129

dubbo.admin.root.password=root

dubbo.admin.guest.password=guest

保存:x

最后再启动此Tomcat就可以了!

最后在浏览器里连接下

Center 3

输入刚刚配置的密码和用户名:root root

登录

Center 4

能成功进入了,并且也能检测到目前连上的服务。

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读