高效开发 Dubbo?用 Spring Boot 可得劲!
不仅简化了 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 并设置环境变量(本地已安装的可略过)。
创建服务提供者
创建一个 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 月 结束,推荐使用新版本开发您的应用。
开发 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地址和端口指的是轻量版配置中心
开发并启动 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);
} }登录轻量版配置中心控制台 http://127.0.0.1:8080,在左侧导航栏中单击服务列表 ,查看提供者列表。可以看到服务提供者里已经包含了 com.alibaba.edas.IHelloService,且可以查询该服务的服务分组和提供者 IP。
创建服务消费者
创建一个 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 月 结束,推荐使用新版本开发您的应用。
开发 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地址和端口指的是轻量版配置中心
开发并启动 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);
} }- 登录轻量版配置中心控制台 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 可得劲!的更多相关文章
- Running Dubbo On Spring Boot
Dubbo(http://dubbo.io/) 是阿里的开源的一款分布式服务框架.而Spring Boot则是Spring社区这两年致力于打造的简化Java配置的微服务框架. 利用他们各自优势,配置到 ...
- zookeeper + dubbo + spring boot
最近开始接触了分布式的一些东西,这里给自己作一个学习笔记. 这里只是做一个运行demo,具体的理论知识就不在这里阐述了. 1.zookeeper的安装与启动 下载地址:http://www.apach ...
- VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试)
源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 安装扩展 安装如下两个主要扩展即可,这两个扩展已关联java项目开发主要使用的maven.spr ...
- [转]VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试)
源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 安装扩展 安装如下两个主要扩展即可,这两个扩展已关联java项目开发主要使用的maven.spr ...
- 后端开发实践:Spring Boot项目模板
在我的工作中,我从零开始搭建了不少软件项目,其中包含了基础代码框架和持续集成基础设施等,这些内容在敏捷开发中通常被称为"第0个迭代"要做的事情.但是,当项目运行了一段时间之后再来反 ...
- Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)
Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- 玩转Spring Boot 集成Dubbo
玩转Spring Boot 集成Dubbo 使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面 ...
- 居然仅用浏览器,就完成了Spring Boot应用的开发与部署!
最近有幸试用了一下阿里云的一个新产品:云开发平台,体验一把全新的开发模式!虽然中间也碰到了一些问题,但整体的体验透露着未来感,因为整个过程都不需要使用我们最常用的IDEA,仅依靠浏览器就把一个Spri ...
- Dubbo在Spring和Spring Boot中的使用
一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...
随机推荐
- Devstack配置负载均衡,负载均衡版本V2
本文为minxihou的翻译文章,转载请注明出处Bob Hou: http://blog.csdn.net/minxihou JmilkFan:minxihou的技术博文方向是 算法&Open ...
- 学习笔记——CDQ分治
再次感谢这位大佬的博客:https://www.cnblogs.com/ljc20020730/p/10395866.html CDQ分治,是一种在分治合并中计算前面值对后面答案的贡献的一种算法.今天 ...
- log4j架构
Log4j API设计为分层结构,其中每一层提供了不同的对象,对象执行不同的任务.这使得设计灵活,根据将来需要来扩展. 有两种类型可用在Log4j的框架对象. 核心对象: 框架的强制对象和框架的使用. ...
- jquery操作html元素之( 尺寸)
jQuery 提供多个处理尺寸的重要方法: width() height() innerWidth() innerHeight() outerWidth() outerHeight() jQuery ...
- 21-4indexOf
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- shell 通配符,管道符,输入/输出重定向,命令置换
1. echo 输出 [echo 输出的内容 ]把内容输出到终端上 如果字符串使用双引号,echo命令原样输出 [ echo "hello world" ] ...
- NIO教程笔记
NIO操作文件部分详解 NIO——New IO,也可以理解为非阻塞IO(Non Blocking IO).可以替代旧IO,更高效的支持读写(文件读写,网络读写).但文件操作都是阻塞的.学习NIO首先要 ...
- 【hihocoder】Demo Day
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics startup. Today is your co ...
- Java 几种队列区别的简单说明
前言 队列,字面意思就可以明白. 是一种线性的数据暂存与管理工具. 也可以让各种业务功能进行逐个的队列运行. 此篇博客只说明一下Java有几种队列 未阻塞和阻塞队列的区别 未阻塞: 1.未阻塞的队列在 ...
- 10月23日——作业1——while循环练习
while循环'''此类编程题,注意带进去试一试1.九九乘法表row=1while row<=9: col=1 while col<=row: print(col,"*" ...