Dubbo集成Spring与Zookeeper实例

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
简介:

使用Dubbo结合Zookeeper和Spring,

是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和Spring的Demo工程。
代码已经上传到github,地址:https://github.com/bingyue/dubbodemo

1.集成Zookeeper

(1)安装Zookeeper

(2)启动Zookeeper服务

2.配置Dubbo的管理界面

Dubbo官方提供了几个管理工具,包括管理控制台dubbo-admin及简易监控中心等。
这里的Demo学习使用了最新的稳定的2.5.3版本。
官网指出在阿里巴巴内部广泛使用的GA版本为2.4.9,并且推荐此版本,
在实际项目中可以使用这一稳定版本。

(1)下载dubbo-admin,修改相关的配置

下载地址http://dubbo.io/Download-zh.htm,下载后解压war包,
找到dubbo.properties配置文件,修改其中的zookeeper地址为上面部署的zk地址。

(2)启动dubbo-admin工程

把工程文件夹放在Tomcat的工程目录下,启动后登录即可。

 

3.创建Demo项目,集成Spring

Demo项目主要是使用了CXF整合Spring创建WebService的技术,关于CXF可以参考:
WebService CXF学习(高级篇1):整合Spring框架

(1)创建服务提供接口项目

服务端接口需要单独打包,服务提供方和消费方共享,同时被提供端和消费端依赖。
1.新建Maven项目

创建dubbo-demo-api工程,pom文件如下:

1
2
3
4
5
< groupId >dubbo-demo</ groupId >
   < artifactId >dubbo-demo-api</ artifactId >
   < version >0.1</ version >
   < packaging >jar</ packaging >
   < name >dubbo-demo-api</ name >


2.编写服务接口

1
2
3
public  interface  TestService {
     public  String sayHello(String name);
}

 

(2)创建项目实现接口

1.创建具体接口实现项目

1
2
3
4
5
6
7
8
9
public  class  TestServiceImpl  implements  TestService {
     /**
      * 服务提供方实现接口:(对服务消费方隐藏实现)
      */
     public  String sayHello(String name) {
         System.out.println( "调用provider服务" );
         return  "hello" +name;
     }
}

添加api的依赖,添加Spring、dubbo和zookeeper的依赖,pom文件如下:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<groupId>dubbo-demo</groupId>
   <artifactId>dubbo-demo-provider</artifactId>
   <version> 0.1 </version>
   <packaging>jar</packaging>
 
   <name>dubbo-demo-provider</name>
 
   <properties>
     <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding>
   </properties>
   
   <dependencies>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version> 3.8 . 1 </version>
       <scope>test</scope>
     </dependency>
      <!-- 添加Dubbo服务接口的依赖 -->
     <dependency>
       <groupId>dubbo-demo</groupId>
       <artifactId>dubbo-demo-api</artifactId>
       <version> 0.1 </version>
     </dependency>
     <!-- 添加Spring context依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>org.springframework.context</artifactId>
      <version> 3.1 . 2 .RELEASE</version>
    </dependency>
     <!-- 引入Dubbo依赖 -->
     <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>dubbo</artifactId>
             <version> 2.5 . 3 </version>
         </dependency>
     <!-- zookeeper依赖 -->
     <dependency>
             <groupId>org.apache.zookeeper</groupId>
             <artifactId>zookeeper</artifactId>
             <version> 3.4 . 6 </version>
         </dependency>
     <!-- zookeeper客户端 zkclient -->
     <dependency>
          <groupId>com.github.sgroschupf</groupId>
          <artifactId>zkclient</artifactId>
          <version> 0.1 </version>
       </dependency>
   </dependencies>

2.暴露服务,整合Spring完成服务提供者

配置provider.xml:

1
2
3
4
5
6
7
8
9
10
<!-- 提供方应用信息,用于计算依赖关系 -->
     <dubbo:application name= "dubbo-demo-provider"   />
     <!-- 使用multicast广播注册中心暴露服务地址 -->
     <dubbo:registry address= "multicast://192.168.106.129:2182"  />
     <!-- 用dubbo协议在 20880 端口暴露服务 -->
     <dubbo:protocol name= "dubbo"  port= "20880"  />
     <!-- 声明需要暴露的服务接口 -->
     <dubbo:service  interface = "bingyue.dubbo.provider.TestService"  ref= "testService"  />
     <!-- 和本地bean一样实现服务 -->
     <bean id= "testService"  class = "bingyue.dubbo.provider.TestServiceImpl"  />

  

(3)创建服务消费端项目

1.创建项目和相关配置
在pom.xml中依赖接口项目:

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
< groupId >dubbo-demo</ groupId >
   < artifactId >dubbo-demo-consumer</ artifactId >
   < version >0.1</ version >
   < packaging >jar</ packaging >
   
   < dependencies >
   <!-- 添加Spring context依赖 -->
    < dependency >
      < groupId >org.springframework</ groupId >
      < artifactId >org.springframework.context</ artifactId >
      < version >3.1.2.RELEASE</ version >
    </ dependency >
    < dependency >
       < groupId >junit</ groupId >
       < artifactId >junit</ artifactId >
       < version >3.8.1</ version >
       < scope >test</ scope >
     </ dependency >
     <!-- 添加Dubbo服务接口的依赖 -->
     < dependency >
       < groupId >dubbo-demo</ groupId >
       < artifactId >dubbo-demo-api</ artifactId >
       < version >0.1</ version >
     </ dependency >
   </ dependencies >


配置consumer.xml:

1
2
3
4
5
6
7
8
9
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
     < dubbo:application  name="dubbo-demo-consumer"  />
  
     <!-- 使用multicast广播注册中心暴露发现服务地址 -->
     < dubbo:registry  address="multicast://192.168.106.129:2182" />
  
     <!-- 把接口的声明引入到本项目中,通过jar或者maven依赖都可以 -->
     <!-- 生成远程服务代理,可以和使用本地bean一样使用服务者提供的服务 -->
     < dubbo:reference  id="testService" interface="bingyue.dubbo.provider.TestService" />

  

2.配置和启动消费者服务
加载配置文件后,消费端调用服务端接口的方式有多种,可以从上下文中获取bean,也可以使用注解。

1
2
3
4
5
6
7
8
9
10
11
12
13
public  class  Tester {
     //使用注解或者使用远程服务代理取得接口实例
//  @Autowired TestService testService;
     
     public  static  void  main(String[] args){
         //加载同级目录下的消费者配置文件
         ClassPathXmlApplicationContext ctx= new  ClassPathXmlApplicationContext( "consumer.xml" );
         ctx.start();
         TestService testService = (TestService) ctx.getBean( "testService" );
         String result=testService.sayHello( "cnblogs" );
         System.out.println(result);
     }
}

  

3.使用Maven结合Dubbo

这种Dubbo使用方式可以不用考虑Maven如何单独对接口打包,
在顶层的服务提供方项目中编写接口,
消费端项目会依赖服务提供方项目的jar包,从中获得接口实例,
然后在服务方项目中新建单独的子项目去编写接口的实现类,
这样通过Maven的继承,将消费端项目和具体的接口实现类代码分离开。

 


本文转自邴越博客园博客,原文链接:http://www.cnblogs.com/binyue/p/3723460.html,如需转载请自行联系原作者

相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
2月前
|
数据可视化 Java API
Spring Boot与Swagger的集成
Spring Boot与Swagger的集成
|
2月前
|
Java API 开发者
在Spring Boot中集成Swagger API文档
在Spring Boot中集成Swagger API文档
|
1月前
|
XML Java 数据格式
Spring Cloud全解析:注册中心之zookeeper注册中心
使用ZooKeeper作为Spring Cloud的注册中心无需单独部署服务器,直接利用ZooKeeper服务端功能。项目通过`spring-cloud-starter-zookeeper-discovery`依赖实现服务注册与发现。配置文件指定连接地址,如`localhost:2181`。启动应用后,服务自动注册到ZooKeeper的`/services`路径下,形成临时节点,包含服务实例信息。
141 3
|
2月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14610 24
|
1月前
|
人工智能 Java API
JeecgBoot 低代码平台快速集成 Spring AI
Spring 通过 Spring AI 项目正式启用了 AI(人工智能)生成提示功能。本文将带你了解如何在 Jeecg Boot 应用中集成生成式 AI,以及 Spring AI 如何与模型互动,包含 RAG 功能。
93 3
|
27天前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
|
2月前
|
JSON 缓存 Java
Spring Boot集成 Swagger2 展现在线接口文档
本节课详细分析了 Swagger 的优点,以及 Spring Boot 如何集成 Swagger2,包括配置,相关注解的讲解,涉及到了实体类和接口类,以及如何使用。最后通过页面测试,体验了 Swagger 的强大之处,基本上是每个项目组中必备的工具之一,所以要掌握该工具的使用,也不难。
|
2月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
61 3
|
2月前
|
SQL Java 数据库
实时计算 Flink版产品使用问题之Spring Boot集成Flink可以通过什么方式实现通过接口启动和关闭Flink程序
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
2月前
|
Java API 开发工具
Spring Boot与Spring Cloud Config的集成
Spring Boot与Spring Cloud Config的集成