我是靠谱客的博主 害羞八宝粥,这篇文章主要介绍[Apache Dubbo] Spring Boot 整合 Dubbo 实战[Apache Dubbo] Spring Boot 整合 Dubbo 实战,现在分享给大家,希望可以做个参考。

[Apache Dubbo] Spring Boot 整合 Dubbo 实战

目录

  • [Apache Dubbo] Spring Boot 整合 Dubbo 实战
    • Dubbo 介绍
    • 准备工作
    • 项目开发思路
      • 公共业务接口定义
      • 服务提供者
      • 服务消费者
    • 更多

手机用户请横屏获取最佳阅读体验,REFERENCES中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。

平台地址
CSDNhttps://blog.csdn.net/sinat_28690417
简书https://www.jianshu.com/u/3032cc862300
个人博客https://yiyuery.github.io/NoteBooks/

正文

随着业务复杂度的逐渐提升,很多服务被拆分为多个微服务,服务的管理和之间的通信问题亟待解决,很多RPC框架应运而生。其中,最出名的莫过于Appache Dubbo项目。
本文主要介绍最新版Apache Dubbo 和 Spring Boot 的整合,并进行实战开发。

Dubbo 介绍

  • Dubbo 官网 http://dubbo.apache.org/zh-cn/index.html

网上资料很多,不再赘述,提下主要的特点

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

准备工作

  • 鉴于最近在学习Redis,加上对应的Redis的Docker容器环境直接远程拉取一份即可,环境构建速度更快,所以注册中心本次使用Redis(不熟悉Docker的可以通过redis官网下载对应软件即可))。

在这里插入图片描述

  • 搭建项目结构

在这里插入图片描述

  1. dubbo-demo-interface 公共业务接口定义
  2. dubbo-demo-xml xml配置dubbo,下面分别对应两个子module,实现消费者和服务提供者
  • jar 依赖(使用简约的gradle进行申明)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
dependencies { //redis相关 implementation 'org.springframework.boot:spring-boot-starter-data-redis' compile 'org.apache.commons:commons-pool2:2.0' compile group: 'redis.clients', name: 'jedis', version: '3.2.0' //如果用zookeeper的话切换到这个 //compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.5.6' //dubbo spring boot starter compile group: 'org.apache.dubbo', name: 'dubbo-spring-boot-starter', version: '2.7.5' compile group: 'org.apache.dubbo', name: 'dubbo', version: '2.7.5' compile project(':dubbo-example:dubbo-demo-interface') //.... }
  • Dubbo目前是支持注解配置的,本文先从最基本的xml配置开始,后续会继续深入介绍

项目开发思路

  • 公共接口定义,服务提供方和消费方可以同时依赖,避免出现定义不一致的地方
  • 服务消费者向服务注册中心获取服务
  • 服务提供者向服务注册中心注册服务
  • 服务的发现和管理在注册中心中进行维护
  • 服务间通过rpc通信,实现远程服务调用

公共业务接口定义

dubbo-demo-interface

com.example.dubbo.business.HelloService

复制代码
1
2
3
4
5
public interface HelloService { String hello(String userName); }

服务提供者

dubbo-demo-xml-provider

  • 启动类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.dubbo.api.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource({"classpath:dubbo/dubbo-provider.xml"}) @ComponentScan(basePackages = {"com.example"}) public class DubboDemoApiProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboDemoApiProviderApplication.class, args); } }
  • 业务接口实现类

com.example.dubbo.api.provider.business.impl.HelloServiceImpl

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.dubbo.api.provider.business.impl; import com.example.dubbo.business.HelloService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Slf4j @Service("helloService") public class HelloServiceImpl implements HelloService { @Value("${dubbo.application.name}") private String remoteServiceName; @Override public String hello(String userName) { log.info("HelloServiceImpl:hello,param:userName" + userName); return "Hello," + userName + "<<<<from remoteServiceName:" + remoteServiceName; } }
  • yml配置注册中心
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring: redis: port: 6379 host: 127.0.0.1 dubbo: application: id: dubbo-demo-api-provider name: dubbo-demo-api-provider scan: base-packages: com.example.dubbo.api.provider.business protocol: name: dubbo port: 20880 registry: address: redis://127.0.0.1:6379 protocol: redis
  • xml配置服务提供方式

dubbo/dubbo-provider.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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="dubbo-demo-xml-provider" /> <!-- 使用redis广播注册中心暴露服务地址 --> <dubbo:registry address="redis://127.0.0.1:6379" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.example.dubbo.business.HelloService" ref="helloService" /> </beans>
  • 启动服务提供者

在这里插入图片描述

  • 观察redis注册中心中数据

在这里插入图片描述

可以在注册中心看到对应的服务信息。

服务消费者

  • 启动类
复制代码
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
/* * @ProjectName: 编程学习 * @Copyright: 2019 HangZhou Ashe Dev, Ltd. All Right Reserved. * @address: https://yiyuery.github.io/NoteBooks/ * @date: 2020/1/5 8:56 下午 * @description: 本内容仅限于编程技术学习使用,转发请注明出处. */ package com.example.dubbo.api.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; /** * <p> * * </p> * * @author Ashe * @version V1.0.0 * @date 2020/1/5 8:56 下午 * @modificationHistory=========================逻辑或功能性重大变更记录 * @modify By: {修改人} 2020/1/5 * @modify reason: {方法名}:{原因} * ... */ @SpringBootApplication @ComponentScan(basePackages = {"com.example"}) @ImportResource({"classpath:dubbo/dubbo-consumer.xml"}) public class DubboDemoXmlConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboDemoXmlConsumerApplication.class, args); } }
  • 服务消费者xml配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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="dubbo-demo-xml-consumer" /> <!-- 使用redis注册中心暴露发现服务地址 --> <dubbo:registry address="redis://127.0.0.1:6379" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="helloService" check="false" interface="com.example.dubbo.business.HelloService" /> </beans>
  • boot application YML 配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring: redis: port: 6379 host: 127.0.0.1 dubbo: application: id: dubbo-demo-xml-consumer name: dubbo-demo-xml-consumer registry: address: redis://127.0.0.1:6379 protocol: redis
  • 编写测试消费代码
复制代码
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
/* * @ProjectName: 编程学习 * @Copyright: 2019 HangZhou Ashe Dev, Ltd. All Right Reserved. * @address: https://yiyuery.github.io/NoteBooks/ * @date: 2020/1/5 9:07 下午 * @description: 本内容仅限于编程技术学习使用,转发请注明出处. */ package com.example.dubbo.api.consumer; import com.example.dubbo.business.HelloService; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; /** * <p> * * </p> * * @author Ashe * @version V1.0.0 * @date 2020/1/5 9:07 下午 * @modificationHistory=========================逻辑或功能性重大变更记录 * @modify By: {修改人} 2020/1/5 * @modify reason: {方法名}:{原因} * ... */ @SpringBootTest @Slf4j public class DubboXmlConsumerTest { @Resource private HelloService helloService; @Test void remoteHelloServiceRequestTest() { String resultMsg = helloService.hello("Yiyuery"); log.info("dubbo remote response: "+resultMsg); } }
  • 查看执行后结果

测试用例日志输出

复制代码
1
2
3
4
5
2020-01-05 22:34:56.757 ERROR 12440 --- [lientWorker-4-1] o.a.d.remoting.transport.CodecSupport : [DUBBO] Serialization extension org.apache.dubbo.common.serialize.protobuf.support.GenericProtobufJsonSerialization has duplicate id to Serialization extension org.apache.dubbo.common.serialize.protobuf.support.GenericProtobufSerialization, ignore this Serialization extension, dubbo version: 2.7.5, current host: 192.168.1.108 2020-01-05 22:34:56.840 INFO 12440 --- [ main] c.e.d.api.consumer.DubboXmlConsumerTest : dubbo remote response: Hello,Yiyuery<<<<from remoteServiceName:dubbo-demo-api-provider 2020-01-05 22:34:56.848 INFO 12440 --- [bboShutdownHook] o.apache.dubbo.config.DubboShutdownHook : [DUBBO] Run shutdown hook now., dubbo version: 2.7.5, current host: 192.168.1.108

服务提供方日志输出

在这里插入图片描述

可以看到已经完成了项目的初步搭建,后续将基于这个进行Dubbo的底层实现原理深入学习。


更多

扫码关注架构探险之道,回复『源码』,获取本文相关源码和资源链接

在这里插入图片描述

知识星球(扫码加入获取历史源码和文章资源链接)

在这里插入图片描述

复制代码
1

最后

以上就是害羞八宝粥最近收集整理的关于[Apache Dubbo] Spring Boot 整合 Dubbo 实战[Apache Dubbo] Spring Boot 整合 Dubbo 实战的全部内容,更多相关[Apache内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部