343 lines
11 KiB
Markdown
343 lines
11 KiB
Markdown
# spring 整合 dubbo(注解方式)
|
||
|
||
## 目录<br/>
|
||
<a href="#一-项目结构说明">一、 项目结构说明</a><br/>
|
||
<a href="#二项目依赖">二、项目依赖</a><br/>
|
||
<a href="#三公共模块dubbo-ano-common">三、公共模块(dubbo-ano-common)</a><br/>
|
||
<a href="#四-服务提供者dubbo-ano-provider">四、 服务提供者(dubbo-ano-provider)</a><br/>
|
||
<a href="#41-提供方配置">4.1 提供方配置</a><br/>
|
||
<a href="#42--使用注解@Service暴露服务">4.2 使用注解@Service暴露服务</a><br/>
|
||
<a href="#五服务消费者dubbo-ano-consumer">五、服务消费者(dubbo-ano-consumer)</a><br/>
|
||
<a href="#1消费方的配置">1.消费方的配置</a><br/>
|
||
<a href="#2使用注解@Reference引用远程服务">2.使用注解@Reference引用远程服务</a><br/>
|
||
<a href="#六项目构建的说明">六、项目构建的说明</a><br/>
|
||
<a href="#七关于dubbo新版本管理控制台的安装说明">七、关于dubbo新版本管理控制台的安装说明</a><br/>
|
||
## 正文<br/>
|
||
|
||
|
||
## 一、 项目结构说明
|
||
|
||
1.1 按照 dubbo 文档推荐的服务最佳实践,建议将服务接口、服务模型、服务异常等均放在 API 包中,所以项目采用 maven 多模块的构建方式,在 spring-dubbo-annotation 下构建三个子模块:
|
||
|
||
1. dubbo-ano-common 是公共模块,用于存放公共的接口和 bean,被 dubbo-ano-provider 和 dubbo-ano-provider 在 pom.xml 中引用;
|
||
2. dubbo-ano-provider 是服务的提供者,提供商品的查询服务;
|
||
3. dubbo-ano-provider 是服务的消费者,调用 provider 提供的查询服务。
|
||
|
||
1.2 本项目 dubbo 的搭建采用 zookeeper 作为注册中心, 关于 zookeeper 的安装和基本操作可以参见我的手记[Zookeeper 基础命令与 Java 客户端](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/ZooKeeper/ZooKeeper%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%E4%B8%8EJava%E5%AE%A2%E6%88%B7%E7%AB%AF.md)
|
||
|
||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-dubbo.png"/> </div>
|
||
|
||
|
||
|
||
## 二、项目依赖
|
||
|
||
**在父工程的项目中统一导入依赖 dubbo 依赖的的 jar 包**
|
||
|
||
这里需要注意的是 ZooKeeper 3.5.x 和 ZooKeeper 3.4.x 是存在不兼容的情况 详见官网解释[ZooKeeper Version Compatibility](https://curator.apache.org/zk-compatibility.html), zookeeper 3.5 目前是 beta 版本,所以 zookeeper 我选择的版本是 zookeeper-3.4.9 作为服务端。但默认情况下 curator-framework 自动引用的最新的 3.5 的版本客户端,会出现 KeeperException$UnimplementedException 异常
|
||
|
||
```xml
|
||
<!--dubbo 依赖-->
|
||
<groupId>com.alibaba</groupId>
|
||
<artifactId>dubbo</artifactId>
|
||
<version>2.6.2</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>org.apache.curator</groupId>
|
||
<artifactId>curator-framework</artifactId>
|
||
<version>4.0.0</version>
|
||
<exclusions>
|
||
<exclusion>
|
||
<groupId>org.apache.zookeeper</groupId>
|
||
<artifactId>zookeeper</artifactId>
|
||
</exclusion>
|
||
</exclusions>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>org.apache.zookeeper</groupId>
|
||
<artifactId>zookeeper</artifactId>
|
||
<version>3.4.13</version>
|
||
</dependency>
|
||
```
|
||
|
||
|
||
|
||
## 三、公共模块(dubbo-ano-common)
|
||
|
||
- api 下为公共的调用接口;
|
||
- bean 下为公共的实体类。
|
||
|
||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-ano-common.png"/> </div>
|
||
|
||
## 四、 服务提供者(dubbo-ano-provider)
|
||
|
||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-ano-provider.png"/> </div>
|
||
|
||
#### 4.1 提供方配置
|
||
|
||
```java
|
||
@Configuration
|
||
public class DubboConfiguration {
|
||
|
||
/**
|
||
* 提供方应用信息,用于计算依赖关系
|
||
*/
|
||
@Bean
|
||
public ApplicationConfig applicationConfig() {
|
||
ApplicationConfig applicationConfig = new ApplicationConfig();
|
||
applicationConfig.setName("dubbo-ano-provider");
|
||
return applicationConfig;
|
||
}
|
||
|
||
/**
|
||
* 使用 zookeeper 注册中心暴露服务地址
|
||
*/
|
||
@Bean
|
||
public RegistryConfig registryConfig() {
|
||
RegistryConfig registryConfig = new RegistryConfig();
|
||
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
|
||
registryConfig.setClient("curator");
|
||
return registryConfig;
|
||
}
|
||
|
||
/**
|
||
* 用 dubbo 协议在 20880 端口暴露服务
|
||
*/
|
||
@Bean
|
||
public ProtocolConfig protocolConfig() {
|
||
ProtocolConfig protocolConfig = new ProtocolConfig();
|
||
protocolConfig.setName("dubbo");
|
||
protocolConfig.setPort(20880);
|
||
return protocolConfig;
|
||
}
|
||
}
|
||
```
|
||
|
||
#### 4.2 使用注解@Service暴露服务
|
||
|
||
需要注意的是这里的@Service 注解不是 spring 的注解,而是 dubbo 的注解 com.alibaba.dubbo.config.annotation.Service
|
||
|
||
```java
|
||
package com.heibaiying.service;
|
||
|
||
import com.alibaba.dubbo.config.annotation.Service;
|
||
import com.heibaiying.api.IProductService;
|
||
import com.heibaiying.bean.Product;
|
||
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @author : heibaiying
|
||
* @description : 产品提供接口实现类
|
||
*/
|
||
@Service(timeout = 5000)
|
||
public class ProductService implements IProductService {
|
||
|
||
private static List<Product> productList = new ArrayList<>();
|
||
|
||
static {
|
||
for (int i = 0; i < 20; i++) {
|
||
productList.add(new Product(i, "产品" + i, i / 2 == 0, new Date(), 66.66f * i));
|
||
}
|
||
}
|
||
|
||
public Product queryProductById(int id) {
|
||
for (Product product : productList) {
|
||
if (product.getId() == id) {
|
||
return product;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public List<Product> queryAllProducts() {
|
||
return productList;
|
||
}
|
||
}
|
||
```
|
||
|
||
|
||
|
||
## 五、服务消费者(dubbo-ano-consumer)
|
||
|
||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dubbo-ano-consumer.png"/> </div>
|
||
|
||
#### 1.消费方的配置
|
||
|
||
```java
|
||
@Configuration
|
||
public class DubboConfiguration {
|
||
|
||
/**
|
||
* 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样
|
||
*/
|
||
@Bean
|
||
public ApplicationConfig applicationConfig() {
|
||
ApplicationConfig applicationConfig = new ApplicationConfig();
|
||
applicationConfig.setName("dubbo-ano-consumer");
|
||
return applicationConfig;
|
||
}
|
||
|
||
/**
|
||
* 设置调用服务超时时间
|
||
* 关闭所有服务的启动时检查
|
||
*/
|
||
@Bean
|
||
public ConsumerConfig consumerConfig() {
|
||
ConsumerConfig consumerConfig = new ConsumerConfig();
|
||
consumerConfig.setTimeout(3000);
|
||
consumerConfig.setCheck(false);
|
||
return consumerConfig;
|
||
}
|
||
|
||
/**
|
||
* 使用 zookeeper 注册中心暴露发现服务地址
|
||
*/
|
||
@Bean
|
||
public RegistryConfig registryConfig() {
|
||
RegistryConfig registryConfig = new RegistryConfig();
|
||
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
|
||
registryConfig.setClient("curator");
|
||
return registryConfig;
|
||
}
|
||
|
||
}
|
||
```
|
||
|
||
#### 2.使用注解@Reference引用远程服务
|
||
|
||
```java
|
||
package com.heibaiying.controller;
|
||
|
||
import com.alibaba.dubbo.config.annotation.Reference;
|
||
import com.heibaiying.api.IProductService;
|
||
import com.heibaiying.bean.Product;
|
||
import org.springframework.stereotype.Controller;
|
||
import org.springframework.ui.Model;
|
||
import org.springframework.web.bind.annotation.PathVariable;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
|
||
import java.util.List;
|
||
|
||
@Controller
|
||
@RequestMapping("sell")
|
||
public class SellController {
|
||
|
||
// dubbo 远程引用注解
|
||
@Reference
|
||
private IProductService productService;
|
||
|
||
@RequestMapping
|
||
public String productList(Model model) {
|
||
List<Product> products = productService.queryAllProducts();
|
||
model.addAttribute("products", products);
|
||
return "products";
|
||
}
|
||
|
||
@RequestMapping("product/{id}")
|
||
public String productDetail(@PathVariable int id, Model model) {
|
||
Product product = productService.queryProductById(id);
|
||
model.addAttribute("product", product);
|
||
return "product";
|
||
}
|
||
}
|
||
|
||
```
|
||
|
||
## 六、项目构建的说明
|
||
|
||
因为在项目中,consumer 和 provider 模块均依赖公共模块,所以在构建 consumer 和 provider 项目前需要将 common 模块安装到本地仓库,**依次**对**父工程**和**common 模块**执行:
|
||
|
||
```shell
|
||
mvn install -Dmaven.test.skip = true
|
||
```
|
||
|
||
consumer 中 pom.xml 如下
|
||
|
||
```xml
|
||
<?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">
|
||
<parent>
|
||
<artifactId>spring-dubbo-annotation</artifactId>
|
||
<groupId>com.heibaiying</groupId>
|
||
<version>1.0-SNAPSHOT</version>
|
||
</parent>
|
||
<modelVersion>4.0.0</modelVersion>
|
||
|
||
<artifactId>dubbo-ano-consumer</artifactId>
|
||
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>com.heibaiying</groupId>
|
||
<artifactId>dubbo-ano-common</artifactId>
|
||
<version>1.0-SNAPSHOT</version>
|
||
<scope>compile</scope>
|
||
</dependency>
|
||
</dependencies>
|
||
|
||
</project>
|
||
```
|
||
|
||
provider 中 pom.xml 如下
|
||
|
||
```xml
|
||
<?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">
|
||
<parent>
|
||
<artifactId>spring-dubbo-annotation</artifactId>
|
||
<groupId>com.heibaiying</groupId>
|
||
<version>1.0-SNAPSHOT</version>
|
||
</parent>
|
||
<modelVersion>4.0.0</modelVersion>
|
||
|
||
<artifactId>dubbo-ano-provider</artifactId>
|
||
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>com.heibaiying</groupId>
|
||
<artifactId>dubbo-ano-common</artifactId>
|
||
<version>1.0-SNAPSHOT</version>
|
||
<scope>compile</scope>
|
||
</dependency>
|
||
</dependencies>
|
||
|
||
</project>
|
||
```
|
||
|
||
## 七、关于dubbo新版本管理控制台的安装说明
|
||
|
||
安装:
|
||
|
||
```sh
|
||
git clone https://github.com/apache/incubator-dubbo-ops.git /var/tmp/dubbo-ops
|
||
cd /var/tmp/dubbo-ops
|
||
mvn clean package
|
||
```
|
||
|
||
配置:
|
||
|
||
```sh
|
||
配置文件为:
|
||
dubbo-admin-backend/src/main/resources/application.properties
|
||
主要的配置有 默认的配置就是 127.0.0.1:2181:
|
||
dubbo.registry.address=zookeeper://127.0.0.1:2181
|
||
```
|
||
|
||
启动:
|
||
|
||
```sh
|
||
mvn --projects dubbo-admin-backend spring-boot:run
|
||
```
|
||
|
||
访问:
|
||
|
||
```
|
||
http://127.0.0.1:8080
|
||
```
|