不仅简化了 Dubbo 基于 xml 配置的方式,也提高了日常开发效率,甚至提升了工作幸福感。

为了节省亲爱的读者您的时间,请根据以下2点提示来阅读本文,以提高您的阅读收获效率哦。

如果您只有简单的 Java 基础和 Maven 经验,而不熟悉 Dubbo,本文档将帮助您从零开始使用 Spring Boot 开发 Dubbo 服务,并使用 EDAS 服务注册中心实现服务注册与发现。

如果您熟悉 Dubbo,可以选择性地阅读相关章节。

为什么使用 Spring Boot 开发 Dubbo 应用

Spring Boot 使用极简的一些配置,就能快速搭建一个基于 Spring 的应用,提高的日常的开发效率。因此,如果您使用 Spring Boot 来开发基于 Dubbo 的应用,简化了 Bubbo 基于 xml 配置的方式,提高了日常开发效率,提升了工作幸福感。

为什么使用 EDAS 服务注册中心

EDAS 服务注册中心实现了 Dubbo 所提供的 SPI 标准的注册中心扩展,能够完整地支持 Dubbo 服务注册、路由规则配置规则功能

EDAS 服务注册中心能够完全代替 ZooKeeper 和 Redis,作为您 Dubbo 服务的注册中心。同时,与 ZooKeeper 和 Redis 相比,还具有以下优势:

  • EDAS 服务注册中心为共享组件,节省了您运维、部署 ZooKeeper 等组件的机器成本。
  • EDAS 服务注册中心在通信过程中增加了鉴权加密功能,为您的服务注册链路进行了安全加固。
  • EDAS 服务注册中心与 EDAS 其他组件紧密结合,为您提供一整套的微服务解决方案。

本地开发

准备工作

  • 下载、启动及配置轻量级配置中心。

为了便于本地开发,EDAS 提供了一个包含了 EDAS 服务注册中心基本功能的轻量级配置中心。基于轻量级配置中心开发的应用无需修改任何代码和配置就可以部署到云端的 EDAS 中。

请您参考 配置轻量级配置中心 进行下载、启动及配置。推荐使用最新版本。

  • 下载 Maven 并设置环境变量(本地已安装的可略过)。

创建服务提供者

  1. 创建一个 Spring Boot 工程,命名为 spring-boot-dubbo-provider。

    这里我们以 Spring Boot 2.0.6.RELEASE 为例,在 pom.xml 文件中加入如下内容。

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.6.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement> <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba.edas</groupId>
    <artifactId>edas-dubbo-extension</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    </dependency> </dependencies>

    如果您需要选择使用 Spring Boot 1.x 的版本,请使用 Spring Boot 1.5.x 版本,对应的 com.alibaba.boot:dubbo-spring-boot-starter 版本为 0.1.0。

    说明: Spring Boot 1.x 版本的生命周期即将在 2019 年 8 月 结束,推荐使用新版本开发您的应用。

  2. 开发 Dubbo 服务提供者

    2.1 Dubbo 中服务都是以接口的形式提供的。因此需要开发一个接口,例如这里的 IHelloService,接口里有若干个可被调用的方法,例如这里的 SayHello 方法。

    package com.alibaba.edas.boot;
    
    public interface IHelloService {
    String sayHello(String str);
    }

2.2 在服务提供方,需要实现所有以接口形式暴露的服务接口。例如这里实现 IHelloService 接口的类为 HelloServiceImpl。

   package com.alibaba.edas.boot;

 import com.alibaba.dubbo.config.annotation.Service;

 @Service
public class HelloServiceImpl implements IHelloService { public String sayHello(String name) {
return "Hello, " + name + " (from Dubbo with Spring Boot)";
} }

**说明:** 这里的 Service 注解式 Dubbo 提供的一个注解类,类的全名称为:**com.alibaba.dubbo.config.annotation.Service** 。

2.3 配置 Dubbo 服务。在 application.properties/application.yaml 配置文件中新增以下配置:

 # Base packages to scan Dubbo Components (e.g @Service , @Reference)
dubbo.scan.basePackages=com.alibaba.edas.boot
dubbo.application.name=dubbo-provider-demo
dubbo.registry.address=edas://127.0.0.1:8080

**说明:** * 以上三个配置没有默认值,必须要给出具体的配置。
* dubbo.scan.basePackages 的值是开发的代码中含有 com.alibaba.dubbo.config.annotation.Service 和 com.alibaba.dubbo.config.annotation.Reference 注解所在的包。多个包之间用逗号隔开。
* dubbo.registry.address 的值前缀必须是一个 **edas://** 开头,后面的ip地址和端口指的是轻量版配置中心
  1. 开发并启动 Spring Boot 入口类

    package com.alibaba.edas.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
    public class DubboProvider { public static void main(String[] args) { SpringApplication.run(DubboProvider.class, args);
    } }
  2. 登录轻量版配置中心控制台 http://127.0.0.1:8080,在左侧导航栏中单击服务列表 ,查看提供者列表。可以看到服务提供者里已经包含了 com.alibaba.edas.IHelloService,且可以查询该服务的服务分组和提供者 IP。

创建服务消费者

  1. 创建一个 Spring Boot 工程,命名为 spring-boot-dubbo-consumer。

    这里我们以 Spring Boot 2.0.6.RELEASE 为例,在 pom.xml 文件中加入如下内容。

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.6.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement> <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba.edas</groupId>
    <artifactId>edas-dubbo-extension</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    </dependency> </dependencies>

    如果您需要选择使用 Spring Boot 1.x 的版本,请使用 Spring Boot 1.5.x 版本,对应的 com.alibaba.boot:dubbo-spring-boot-starter 版本为 0.1.0。

    说明: Spring Boot 1.x 版本的生命周期即将在 2019 年 8 月 结束,推荐使用新版本开发您的应用。

  2. 开发 Dubbo 消费者

    2.1 在服务消费方,需要引入所有以接口形式暴露的服务接口。例如这里 IHelloService 接口。

      package com.alibaba.edas.boot;
    
    public interface IHelloService {
    String sayHello(String str);
    }

2.2 Dubbo 服务调用。例如需要在 Controller 中调用一次远程 Dubbo 服务,开发的代码如下所示:

package com.alibaba.edas.boot;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class DemoConsumerController { @Reference
private IHelloService demoService; @RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable String name) {
return demoService.sayHello(name);
}
}

说明:这里的 Reference 注解是 com.alibaba.dubbo.config.annotation.Reference 。

2.3 配置 Dubbo 服务。在 application.properties/application.yaml 配置文件中新增以下配置:

dubbo.application.name=dubbo-consumer-demo
dubbo.registry.address=edas://127.0.0.1:8080

**说明:** * 以上两个配置没有默认值,必须要给出具体的配置。
* dubbo.registry.address 的值前缀必须是一个 **edas://** 开头,后面的ip地址和端口指的是轻量版配置中心
  1. 开发并启动 Spring Boot 入口类

    package com.alibaba.edas.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
    public class DubboConsumer { public static void main(String[] args) { SpringApplication.run(DubboConsumer.class, args);
    } }
  2. 登录轻量版配置中心控制台 http://127.0.0.1:8080,在左侧导航栏中单击 服务列表 ,再在服务列表页面选择 调用者列表 ,可以看到包含了 com.alibaba.edas.IHelloService,且可以查看该服务的服务分组和调用者 IP。

结果验证

  • 本地结果验证

curl http://localhost:17080/sayHello/EDAS

Hello, EDAS (from Dubbo with Spring Boot)

  • EDAS 部署结果验证

curl http://localhost:8080/sayHello/EDAS

Hello, EDAS (from Dubbo with Spring Boot)

高效开发 Dubbo?用 Spring Boot 可得劲!的更多相关文章

  1. Running Dubbo On Spring Boot

    Dubbo(http://dubbo.io/) 是阿里的开源的一款分布式服务框架.而Spring Boot则是Spring社区这两年致力于打造的简化Java配置的微服务框架. 利用他们各自优势,配置到 ...

  2. zookeeper + dubbo + spring boot

    最近开始接触了分布式的一些东西,这里给自己作一个学习笔记. 这里只是做一个运行demo,具体的理论知识就不在这里阐述了. 1.zookeeper的安装与启动 下载地址:http://www.apach ...

  3. VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试)

    源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 安装扩展 安装如下两个主要扩展即可,这两个扩展已关联java项目开发主要使用的maven.spr ...

  4. [转]VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试)

    源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 安装扩展 安装如下两个主要扩展即可,这两个扩展已关联java项目开发主要使用的maven.spr ...

  5. 后端开发实践:Spring Boot项目模板

    在我的工作中,我从零开始搭建了不少软件项目,其中包含了基础代码框架和持续集成基础设施等,这些内容在敏捷开发中通常被称为"第0个迭代"要做的事情.但是,当项目运行了一段时间之后再来反 ...

  6. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  7. 玩转Spring Boot 集成Dubbo

    玩转Spring Boot 集成Dubbo 使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面 ...

  8. 居然仅用浏览器,就完成了Spring Boot应用的开发与部署!

    最近有幸试用了一下阿里云的一个新产品:云开发平台,体验一把全新的开发模式!虽然中间也碰到了一些问题,但整体的体验透露着未来感,因为整个过程都不需要使用我们最常用的IDEA,仅依靠浏览器就把一个Spri ...

  9. Dubbo在Spring和Spring Boot中的使用

    一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...

随机推荐

  1. Codeforces 1154B Make Them Equal

    题目链接:http://codeforces.com/problemset/problem/1154/B 题意:给定数组,可以给任意的的元素加上D 或者 减去D,如果能 使数组元素都相等,输出最小的D ...

  2. Bootstrap3的响应式缩略图幻灯轮播效果设计

    在线演示1 本地下载 HTML <div class="container">  <div class="col-md-12">  &l ...

  3. springboot项目大量打印debug日志问题

    目前,java下应用最广泛的日志系统主要就是两个系列: log4j和slf4j+logback . 其中,slf4j只包含日志的接口,logback只包括日志的具体实现,两者加起来才是一个完整的日志系 ...

  4. python学习5—一些关于基本数据结构的练习题

    python学习5—一些关于基本数据结构的练习题 # 1. use _ to connect entries in a list # if there are no numbers in list l ...

  5. Eclipse中普通java项目转成Web项目

    在eclipse导入一个myeclipse建的web项目后,在Eclipse中显示的还是java项目,按下面的步骤可以将其转换成web项目. 1.找到项目目录下的.project文件 2.编辑.pro ...

  6. 常见PID里面的像素大小

    因为tensorflow/models里faster R-cnn目前识别的好像是按照像素比上图片大小来识别的,所以在这里统计一下各个元件的像素大小的范围 DCS:70~200

  7. Flutter 集成到现有iOS工程

    前沿 由于我司已经有自己的App,flutter属于技术引进的一部分,也不太可能重新启动一个项目,因此目前我们是将flutter模块形式注入我们的App之中.即:将flutter模块集成到现在有iOS ...

  8. Linux 服务器 个人常用操作命令记录

    1.实时查看log:tail -f 日志文件名 2.查看Apache运行的用户组或用户名:ps aux | grep httpd 或者是: ps -ef | grep httpd 3.查看cronta ...

  9. 新建Application 报错android.app.Application cannot be cast

    我在开发APP的时候重新使用了一个类,继承了android.app.Application.但是在运行的时候提示java.lang.ClassCastException: android.app.Ap ...

  10. Google Projectsheet Planning 插件的WBS

    生成 WBS的序列號 在 Sldebar中的 "WBS" 按鈕: "< WBS" 取消下級目錄 "WBS >" 生成下級目錄 G ...