目录
1. 槽点
Spring Boot 与 Dubbo 整合的依赖有3个版本。
第一个是 apache推出的依赖:
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
另一个是 alibaba 推出的
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
还有一个是个人开发者推出的
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
这三个依赖的配置好像都不太一样,有些依赖还得添加额外的依赖。所以你在看博客的时候一定要看清楚依赖,尤其是第一个和第二个。
这篇文章使用的是个人开发者推出的依赖,如果你不想使用这个版本的依赖,就不用再往下看了。
2. 创建项目
还记得我们在第一篇文章中说的,RPC 调用主要有三个东西。服务提供者(生产者)、服务调用者(消费者)、服务注册中心。
创建一个名称为 duboot 的项目。然后在这个项目里创建三个 spring boot 模块。
整个项目的目录结构为:
duboot
|-duboot-api
|-duboot-user-provider
|-duboot-web
一个非常简单的 RPC 调用的项目框架。其中 duboot-api 定义需要发布的接口。或者叫需要发布的服务。duboot-user-provider 负责实现对应的服务。即服务提供者(生产者),duboot-web 使用服务,即服务的调用者(消费者)。
注意:user与web模块都是 Spring Boot 项目
3. 添加依赖
这里使用的依赖是个人推出的依赖包,而且user-provider模块和web模块的依赖是一样的。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zdran.duboot</groupId>
<artifactId>user-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.zdran.duboot</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--dubbo-springBoot依赖-->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意:这里依赖了 duboot-api模块,以及zookeeper相关的东西
4. 添加配置
在 application.yml 里添加相关的配置。
注意: user-provider模块与web模块的配置基本一样,只需要修改一下 server.port、application.name、scan这几个值就可以了
server:
port: 8086
spring:
dubbo:
#应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
application:
name: duboot-user-provider
#注册中心配置,用于配置连接注册中心相关信息。
registry:
address: zookeeper://127.0.0.1:2181
#协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
protocol:
name: dubbo
port: 20880
scan: com.zdran.duboot.user.provider #服务暴露与发现消费所在的package
注册中心可以使用我们上一篇文章中搭建好的 zookeeper 就可以了 。
5. 实现服务
我们首先在 duboot-api模块 里创建一个接口,或者说定义一个服务:
package com.zdran.duboot.api.service;
/**
* Create by ranzd on 2018/12/15
*
* @author cm.zdran@gmail.com
*/
public interface HelloDubbo {
String sayHello(String name);
}
然后在 duboot-user-provider模块 里实现这个接口,也叫实现服务
package com.zdran.duboot.user.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.zdran.duboot.api.service.HelloDubbo;
/**
* Create by ranzd on 2018/12/15
*
* @author ranzd@chinaunicom.cn
*/
@Service(version = "1.0", timeout = 50000)
public class HelloDubboImpl implements HelloDubbo {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
这里的 Service 注解是dubbo包里的,不是spring 包里的
然后就可以在 web模块 发起调用了。我们在 web模块 实现一个controller,然后让这个controller去调用刚刚实现好的服务。
package com.zdran.duboot.web.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zdran.duboot.api.service.HelloDubbo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* Create by ranzd on 2018/12/16
*
* @author cm.zdran@gmail.com
*/
@RestController
public class HelloController {
@Reference(version = "1.0")
private HelloDubbo helloDubbo;
@GetMapping("/duboot/hello/{name}")
public String hello(@PathVariable("name") String name) {
return helloDubbo.sayHello(name);
}
}
访问一下 http://localhost:8085/duboot/hello/dubbo
试试看吧。
6. 整体说明:
可以分别将服务提供者和服务消费者部署到不同的两台机子上。
优点:
服务提供者和服务消费者只需要知道注册中心即可,它们之间打交道需通过注册中心这个第三方,只要是注册中心中已经注册的服务,我们均可以使用,实现了服务提供者和服务消费者间的解耦。
主要依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.0.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>5.2.0</version>
</dependency>
provider(服务提供者)
自定义配置文件:dubbo.properties
dubbo.application.name=provider-test
# 注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 使用协议
dubbo.protocol.name=dubbo
# 服务通信端口
dubbo.protocol.port=20881
dubbo.provider.token=true
服务接口
// 消费接口需要与提供者的接口全路径相同
public interface HelloService {
String send();
}
@DubboService(version = "1.0.0")
public class HelloServiceImpl implements HelloService {
@Override
public String send() {
String uuid = UUID.randomUUID().toString();
return "dubbo-" + uuid;
}
}
配置类
@Configuration
//服务接口实现所在包
@EnableDubbo(scanBasePackages = "com.example.api")
@PropertySource("classpath:/dubbo.properties")
public class DubboProviderConfig {
}
启动类
public class ProviderTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboProviderConfig.class);
context.start();
while (true);
}
}
consumer(服务消费者)
自定义配置文件:dubbo.properties
dubbo.application.name=consumer-test
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 超时时间
dubbo.consumer.timeout=3000
# 重试次数
dubbo.consumer.retries=2
# 启动时没有提供者时不报错
dubbo.consumer.check=false
#关闭注册中心启动时检查
dubbo.registry.check=false
服务接口
// 消费接口需要与提供者的接口全路径相同
public interface HelloService {
String send();
}
服务调用
@Service
public class RpcService {
// 对于服务提供的version
@DubboReference(version = "1.0.0")
private HelloService helloService;
public void get(){
String res = helloService.send();
System.out.println(res);
}
}
配置类
@Configuration
//开启基于注解的dubbo功能
@EnableDubbo(scanBasePackages = "com.example.api")
@PropertySource("classpath:/dubbo.properties")
// 扫描@DubboReference所在包
@ComponentScan(value = {"com.example.consumertest.service"})
public class DubboConsumerConfig {
}
启动类
public class ConsumerTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboConsumerConfig.class);
context.start();
context.getBean(RpcService.class).get();
}
}