前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Apache Dubbo] Spring Boot 整合 Dubbo 实战

[Apache Dubbo] Spring Boot 整合 Dubbo 实战

作者头像
架构探险之道
发布2020-02-11 17:47:10
1K0
发布2020-02-11 17:47:10
举报
文章被收录于专栏:架构探险之道架构探险之道

[Apache Dubbo] Spring Boot 整合 Dubbo 实战

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

平台

地址

CSDN

https://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进行申明)
代码语言:javascript
复制
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

代码语言:javascript
复制
public interface HelloService {

    String hello(String userName);
}

服务提供者

dubbo-demo-xml-provider

  • 启动类
代码语言:javascript
复制
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

代码语言:javascript
复制
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配置注册中心
代码语言:javascript
复制
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

代码语言:javascript
复制
<?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注册中心中数据

在这里插入图片描述

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

服务消费者

  • 启动类
代码语言:javascript
复制
/*
 * @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配置
代码语言:javascript
复制
<?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 配置
代码语言:javascript
复制
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
  • 编写测试消费代码
代码语言:javascript
复制
/*
 * @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);
    }
}
  • 查看执行后结果

测试用例日志输出

代码语言:javascript
复制
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的底层实现原理深入学习。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-01-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 架构探险之道 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [Apache Dubbo] Spring Boot 整合 Dubbo 实战
    • Dubbo 介绍
      • 准备工作
        • 项目开发思路
          • 公共业务接口定义
          • 服务提供者
          • 服务消费者
      相关产品与服务
      云数据库 Redis
      腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档