Dubbo进阶(二) —— 搭建Dubbo开发环境

举报
SHQ5785 发表于 2020/12/30 00:48:54 2020/12/30
【摘要】 #Dubbo进阶(二) —— 搭建Dubbo开发环境     本文是基于maven的,在开发Dubbo应用之前,先安装maven。     dubbo是一个分布式服务框架,提供一个SOA的解决方案。简单的说,dubbo就像在生产者和消费者中间架起了一座桥梁,使之能透明交互。     本文旨在搭建一个可供使用和测试的dubbo环境,使用了spring框架;使用了zooke...

#Dubbo进阶(二) —— 搭建Dubbo开发环境
    本文是基于maven的,在开发Dubbo应用之前,先安装maven。
    dubbo是一个分布式服务框架,提供一个SOA的解决方案。简单的说,dubbo就像在生产者和消费者中间架起了一座桥梁,使之能透明交互。
    本文旨在搭建一个可供使用和测试的dubbo环境,使用了spring框架;使用了zookeeper和dubbo服务。
##准备
    zookeeper:点击下载
    dubbo-monitor:点击下载,下载后解压并放到tomcat的webapps目录下,修改WEB-INF/dubbo.properties中属性如下:

#zookeeper的地址和端口
dubbo.registry.address=zookeeper://127.0.0.1:2181
#登录dubbo管理页面时的root用户和guest用户的密码
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

##代码
    如果你清楚生产者-消费者模型,那么将会很容易理解dubbo的使用步骤。
    一个生产者-消费者模型的代码实现需要3部分:生产者代码,消费者代码,中间接口。
##1. 中间接口
创建一个mavan项目,然后添加一个接口如下:

Public interface ITest {
public void sayHello(String message);
}

  
 
  • 1
  • 2
  • 3

##2. 生产者代码
    创建一个mavan项目,并引入spring依赖、中间接口依赖、zookeeper依赖及dubbo依赖如下:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

<!-- 中间接口,根据你的情况进行修改 -->
<dependency>
<groupId>com.dubbo.demo</groupId>
<artifactId>DubboIServiceDemo</artifactId>
<version>0.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>

<!-- 连接zookeeper的客户端 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

    编写中间接口的实现,代码如下,注意这里通过注解的方式将此类标记为spring服务:

@Service("springservice")
public class Test implements ITest {
@Override
public void sayHello(String message) {
System.out.println("service say:" +message);
}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

    编写spring配置文件,注意需要引入dubbo的schema:

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false">
 
<bean id="springservice"class="com.dubbo.iservice.impl.Test">
</bean>
<!--提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:applicationname="dubbo_provider"></dubbo:application>
<!--使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"subscribe="false
" register=""></dubbo:registry>
<!--要暴露的服务接口 -->
<dubbo:service interface="com.dubbo.iservice.ITest" ref="springservice"/>
</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

编写测试类:

public class Demo {
public static void main(String[] args) {
//这里注意spring配置文件的名字和路径
ApplicationContext applicationContext = newClassPathXmlApplicationContext(new String[] {"classpath*:config/applicationContext-commons.xml" });
while(true);
}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

##3. 消费者代码
    创建一个mavan项目,并引入spring依赖、中间接口依赖、zookeeper依赖及dubbo依赖,这里和生产者是完全相同的。
编写spring配置文件,注意需要引入dubbo的schema:

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false">
<dubbo:applicationname="dubbo_consumer"></dubbo:application>
<!--使用zookeeper注册中心暴露服务地址 -->
<dubbo:registryaddress="zookeeper://127.0.0.1:2181"
check="false"></dubbo:registry>
<!--要引用的服务 -->
<dubbo:reference interface="com.dubbo.iservice.ITest"
id="springservice"></dubbo:reference>
</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

编写测试类:

public class ClientDemo {
public static void main(String[] args) {
//这里注意spring配置文件的名字和路径
ApplicationContext applicationContext = newClassPathXmlApplicationContext(new String[] {"classpath*:config/applicationContext-commons.xml" });
ITest test = (ITest) applicationContext.getBean("springservice");
test.sayHello("abcdefg");
while(true);
}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

开始测试:

  1. 启动zookeeper。执行zookeeper目录下:bin/zkServer.cmd启动服务
  2. 启动dubbo服务。启动dubbo所在tomcat(详见本文开头),dubbo启动后,可通过http://127.0.0.1:8080/dubbo-admin来查看dubbo服务状态
  3. 启动生产者服务。运行生产者测试类Client.Java
  4. 启动消费者服务。运行消费者服务代码ClientDemo.java,此时可以在生产者的控制台看到服务被调用了。

    此时整个dubbo测试已经完成。同时我们可以在dubbo的web端看到生产者、消费者的状态以及各个服务的调用情况。

    为了方便大家,本测试用例以及需要的工具都可以在这里下载:
zookeeper下载:点击下载

Dubbo-admin 下载地址: 点我下载

dubbo-monitor下载(配置文件已修改,可直接解压后放到tomcat中使用):点击下载

![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/e415b30dbbbc5ab4a2c1175b0e899fa7.png)
![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/5ea7f92a4b50d8465587c45e4b34108a.png)
![这里写图片描述](https://img-blog.csdnimg.cn/img_convert/f26b6c802951d54cd92c22204011ed16.png)

文章来源: shq5785.blog.csdn.net,作者:No Silver Bullet,版权归原作者所有,如需转载,请联系作者。

原文链接:shq5785.blog.csdn.net/article/details/80141428

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。