a high-performance, java based open source RPC framework.
Dubbo官网 源码 文档
local.xml:
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<bean id=“xxxAction” class=“com.xxx.XxxAction”>
<property name=“xxxService” ref=“xxxService” />
bean>
在本地服务的基础上,只需做简单配置,即可完成远程化:
local.xml
配置拆分成两份,将服务定义部分放在服务提供方 remote-provider.xml
,将服务引用部分放在服务消费方 remote-consumer.xml
。
,在消费方增加引用服务配置
。remote-provider.xml:
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<dubbo:service interface=“com.xxx.XxxService” ref=“xxxService” />
remote-consumer.xml:
<dubbo:reference id=“xxxService” interface=“com.xxx.XxxService” />
<bean id=“xxxAction” class=“com.xxx.XxxAction”>
<property name=“xxxService” ref=“xxxService” />
bean>
阿里官方demo
dubbo-spring-boot-project
Dubbo Spring Boot (v0.1.0) : https://github.com/dubbo/dubbo-spring-boot-project
Dubbo (v2.0.1) : https://github.com/alibaba/dubbo
Google group : http://groups.google.com/group/dubbo
官方给出的一些案例:dubbo-spring-boot-samples
将服务提供者注册到注册中心
1. 引入dubbo和zkclient组件依赖
2. 配置dubbo的扫描包和注册中心的地址
3. 使用@Service发布服务
<dependency>
<groupId>com.alibaba.spring.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.12version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.9version>
dependency>
TicketService
接口
package com.gqz.ticket.service;
/**
* @ClassName: TicketService
* @author: ganquanzhong
* @date: 2019/10/30 9:40
*/
public interface TicketService {
/** dubbo中的公共api
* 获取票
*/
public String getTicket(String name);
}
TicketServiceImpl
实现类
package com.gqz.ticket.service;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
/**
* @ClassName: TicketServiceImpl
* @author: ganquanzhong
* @date: 2019/10/30 9:43
*/
@Component
@Service(version = "1.0.0") //Dubbo的注解,将服务发布出去
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket(String name) {
return name+"成功订购《银河补习班》电影票";
}
}
ProviderTicketApplication
package com.gqz.ticket;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 1、 将服务提供者注册到注册中心
* 1.引入dubbo和zkclient组件依赖
* 2.配置dubbo的扫描包和注册中心的地址
* 3.使用@Service发布服务
*/
@EnableDubbo()
@DubboComponentScan()
@SpringBootApplication
public class ProviderTicketApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderTicketApplication.class, args);
}
}
#dubbo服务名称
dubbo.application.name =provider-ticket
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages = com.gqz.ticket.service
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
# Dubbo Protocol
# 通讯规则dubbo,hession...
dubbo.protocol.name=dubbo
#dubbo服务端口,我们无需知道dubbo服务运行在哪个端口,故也将其设为随机端口
dubbo.protocol.port=12345
## Dubbo Registry
#注册中心地址, 如果搭建是集群,所以用逗号分开 192.168.1.112:2181,192.168.1.112:2182,192.168.1.112:2183
dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.1.112:2181
到这里,服务提供者就配置完成了,可以启动应用将服务发布到注册中
调用注册中的服务,完成自己的业务
1.引入依赖Dubbo和zkClient
2.配置dubbo的注册中心地址
3.引用服务
<dependency>
<groupId>com.alibaba.spring.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.12version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.9version>
dependency>
TicketService
公共接口
package com.gqz.ticket.service;
/**
* @ClassName: TicketService
* @author: ganquanzhong
* @date: 2019/10/30 9:40
*/
public interface TicketService {
/** dubbo中的公共api
* 获取票
*/
public String getTicket(String name);
}
UserService
自身的业务,调用远程ticketService
服务
package com.gqz.user.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.gqz.ticket.service.TicketService;
import org.springframework.stereotype.Service;
/**
* 调用远程RPC Remote Procedure Call
* 调用com.gqz.ticket.service.TicketService中的服务
*
* @ClassName: UserService
* @author: ganquanzhong
* @date: 2019/10/30 9:48
*/
@Service
public class UserService {
/**
* @Reference注解 通过 @Reference 注入TicketService
* 是通过全类名引用
*/
@Reference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")
TicketService ticketService;
//调用远程接口的服务
public void orderTicket(String name){
System.out.println(ticketService.getTicket(name));
}
}
# 配置Dubbo
#dubbo服务名称
dubbo.application.name =consumer-user
#通讯规则
dubbo.protocol.name=dubbo
#注册中心地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.1.112:2181
ConsumerUserApplicationTests
测试
package com.gqz.user;
import com.gqz.user.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConsumerUserApplicationTests {
@Autowired
UserService userService;
@Test
void testRPC() {
System.out.println("调用RPC provider中的订票业务");
userService.orderTicket("甘全中-陈蓉");
}
}
其中经常遇到空指针异常,不能调用远程的服务主要是Dubbo配置、注解、版本等问题。严格参照官方的demo,不同版本可能出现一些架构方面的变更。
Dubbo Apache Dubbo™ 是一款高性能Java RPC框架。
Spring Boot 应用场景的开发。同时也整合了 Spring Boot 特性:
Apache Dubbo |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
您可以为您的工程引入最新 dubbo-spring-boot-starter
的发布,增加以下依赖到工程的 pom.xml
文件中:
<properties>
<spring-boot.version>2.1.1.RELEASEspring-boot.version>
<dubbo.version>2.7.3dubbo.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
<exclusion>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.3version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
dependencies>
如果您的工程遇到了依赖问题, 请尝试添加如下 Maven 参考到工程的 pom.xml
文件中:
<repositories>
<repository>
<id>apache.snapshots.httpsid>
<name>Apache Development Snapshot Repositoryname>
<url>https://repository.apache.org/content/repositories/snapshotsurl>
<releases>
<enabled>falseenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
如果您现在使用的Dubbo版本低于2.7.0,请使用如下对应版本的Dubbo Spring Boot:
Dubbo Spring Boot | Dubbo | Spring Boot |
---|---|---|
0.2.1.RELEASE | 2.6.5+ | 2.x |
0.1.2.RELEASE | 2.6.5+ | 1.x |
如果您需要尝试最新 dubbo-spring-boot-project
的特性,您可将当前工程手动 Maven install 到本地 Maven 仓库:
Maven install =
mvn install
如果您对 Dubbo 不是非常了解,耽误您几分钟访问 http://dubbo.apache.org/ 。了解后,如果您期望更深入的探讨,可以移步用户手册。
通常情况 , Dubbo 应用有两种使用场景 , 其一为 Dubbo 服务提供方 , 另外一个是 Dubbo 服务消费方,当然也允许两者混合,下面我们一起快速开始!
首先,我们假设存在一个 Dubbo RPC API ,由服务提供方为服务消费方暴露接口 :
public interface DemoService {
String sayHello(String name);
}
实现 DemoService
接口
@Service(version = "1.0.0")
public class DefaultDemoService implements DemoService {
/**
* The default value of ${dubbo.application.name} is ${spring.application.name}
*/
@Value("${dubbo.application.name}")
private String serviceName;
public String sayHello(String name) {
return String.format("[%s] : Hello, %s", serviceName, name);
}
}
编写 Spring Boot 引导程序
@EnableAutoConfiguration
public class DubboProviderDemo {
public static void main(String[] args) {
SpringApplication.run(DubboProviderDemo.class,args);
}
}
配置 application.properties
:
# Spring boot application
spring.application.name=dubbo-auto-configuration-provider-demo
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=org.apache.dubbo.spring.boot.demo.provider.service
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
## Dubbo Registry
dubbo.registry.address=N/A
通过 @Reference
注入 DemoService
:
@EnableAutoConfiguration
public class DubboAutoConfigurationConsumerBootstrap {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Reference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")
private DemoService demoService;
public static void main(String[] args) {
SpringApplication.run(DubboAutoConfigurationConsumerBootstrap.class).close();
}
@Bean
public ApplicationRunner runner() {
return args -> {
logger.info(demoService.sayHello("mercyblitz"));
};
}
}
配置 application.yml
:
spring:
application:
name: dubbo-auto-configure-consumer-sample
请确保 Dubbo 服务提供方服务可用, DubboProviderDemo
运行方可正常。
更多的实现细节,请参考 Dubbo 示例
如果您在使用 Dubbo Spring Boot 中遇到任何问题或者有什么建议? 我们非常需要您的支持!
Dubbo Spring Boot 采用多 Maven 模块工程 , 模块如下:
dubbo-spring-boot-parent 模块主要管理 Dubbo Spring Boot 工程的 Maven 依赖
dubbo-spring-boot-autoconfigure 模块提供 Spring Boot’s @EnableAutoConfiguration
的实现 - DubboAutoConfiguration
,
它简化了 Dubbo 核心组件的装配。
dubbo-spring-boot-actuator 提供 Production-Ready 特性:
dubbo-spring-boot-starter 模块为标准的 Spring Boot Starter ,
当您将它引入到工程后,dubbo-spring-boot-autoconfigure 模块会一同被间接依赖。
Dubbo Spring Boot 示例工程包括: